Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing createReactClass usage in 3 files. #1893

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

catandthemachines
Copy link
Member

@catandthemachines catandthemachines commented Nov 20, 2024

Summary:

Removing createReactClass usage in the following three files:

  • drag-target.tsx
  • sortable.tsx
  • stateful-editor-page.tsx

Issue: LEMS-365

Test plan:

Run "yarn test" and confirm no errors or regressions are introduced.
Run "yarn start" and look and view if there are any issues with any Perseus components.

@catandthemachines catandthemachines self-assigned this Nov 20, 2024
@khan-actions-bot khan-actions-bot requested a review from a team November 20, 2024 19:40
@khan-actions-bot
Copy link
Contributor

khan-actions-bot commented Nov 20, 2024

Gerald

Required Reviewers
  • @Khan/perseus for changes to .changeset/khaki-keys-approve.md, packages/perseus-editor/src/editor.tsx, packages/perseus-editor/src/stateful-editor-page.tsx, packages/perseus-editor/src/components/drag-target.tsx, packages/perseus-editor/src/components/sortable.tsx, packages/perseus-editor/src/widgets/expression-editor.tsx

Don't want to be involved in this pull request? Comment #removeme and we won't notify you of further changes.

Copy link
Contributor

github-actions bot commented Nov 20, 2024

Size Change: -197 B (-0.02%)

Total Size: 1.29 MB

Filename Size Change
packages/perseus-editor/dist/es/index.js 698 kB -197 B (-0.03%)
ℹ️ View Unchanged
Filename Size
packages/kas/dist/es/index.js 39 kB
packages/keypad-context/dist/es/index.js 760 B
packages/kmath/dist/es/index.js 4.27 kB
packages/math-input/dist/es/index.js 77.9 kB
packages/math-input/dist/es/strings.js 1.79 kB
packages/perseus-core/dist/es/index.js 1.48 kB
packages/perseus-linter/dist/es/index.js 22.2 kB
packages/perseus/dist/es/index.js 424 kB
packages/perseus/dist/es/strings.js 3.57 kB
packages/pure-markdown/dist/es/index.js 3.66 kB
packages/simple-markdown/dist/es/index.js 12.5 kB

compressed-size-action

Copy link
Collaborator

@jeremywiebe jeremywiebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of this is great. 🎉

Requesting changes for the comment about default props not being necessary for the SortableItem changes.

}

render() {
const opacity = this.dragHover ? {opacity: 0.3} : {};
const Component = this.props.component;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this and the next few lines can be accomplished in ES6 more idiomatically.

Suggested change
const Component = this.props.component;
const {component: Component, shouldDragHighlight, ...forwardProps } = this.props.component;

You won't need to delete props off of the object then.

style: Props["style"];
};

export class DragTarget extends React.Component<Props> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example of a component that is so simple it could be converted all the way to a functional component. It consists of rendering a component and some callback handlers. These are all easily modelled in a functional component. :)

Not required for this PR, but if you're feeling adventurous, go for it.

@@ -1,69 +1,91 @@
import {css, StyleSheet} from "aphrodite";
import createReactClass from "create-react-class";
import PropTypes from "prop-types";
import * as React from "react";
import ReactDOM from "react-dom";

const PT = PropTypes;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete this and the prop-types import now, I think.

Comment on lines +51 to +53
getInitialState() {
return {dragHover: false};
},
handleDrop: function (e) {
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When initial state doesn't depend on anything, I typically avoid the function getInitialState() and just define state like this:

    state: State = {dragHover: false};

style: Props["style"];
};

export class DragTarget extends React.Component<Props> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use state in this component, so it'd be a good idea to provide a State generic argument.

Suggested change
export class DragTarget extends React.Component<Props> {
export class DragTarget extends React.Component<Props, State> {


type ItemProps = {
area: any;
component: any;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we type this as React.ReactNode? As far as I can see there are no constraints on what this is except that it is something React can render.

handleDragStart: function (e) {
export class SortableItem extends React.Component<ItemProps> {
static defaultProps: DefaultItemProps = {
area: "",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be better to not provide all these defaults. The Sortable that renders these items looks like it provides all of these props. By making defaults for these props, we make the prop optional, and force this component to have a good default. In the case of area, the default is of the wrong type as it should be an object that provides three callback functions: onDragEnter, onDragStart, and onDrop.

import * as React from "react";
import _ from "underscore";

import EditorPage from "./editor-page";

type Props = {
componentClass: any;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can type this as React.ComponentType.

}

render() {
const opacity = this.dragHover ? {opacity: 0.3} : {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be this.state.dragHover. Then you don't need the dragHover: any; instance variable above (which is never set).

Suggested change
const opacity = this.dragHover ? {opacity: 0.3} : {};
const opacity = this.state.dragHover ? {opacity: 0.3} : {};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure DragTarget isn't dead code? I only see one usage, in perseus-editor/src/editor.tsx. That usage doesn't pass the component prop, so it seems like that DragTarget would throw if it were ever rendered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants