-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Remove setState in ObjectField and ArrayField #480
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,8 +12,7 @@ import { | |
| retrieveSchema, | ||
| toIdSchema, | ||
| shouldRender, | ||
| getDefaultRegistry, | ||
| setState | ||
| getDefaultRegistry | ||
| } from "../../utils"; | ||
|
|
||
| function ArrayFieldTitle({TitleField, idSchema, title, required}) { | ||
|
|
@@ -151,6 +150,7 @@ function DefaultNormalArrayFieldTemplate(props) { | |
| class ArrayField extends Component { | ||
| static defaultProps = { | ||
| uiSchema: {}, | ||
| formData: [], | ||
| idSchema: {}, | ||
| registry: getDefaultRegistry(), | ||
| required: false, | ||
|
|
@@ -161,19 +161,6 @@ class ArrayField extends Component { | |
|
|
||
| constructor(props) { | ||
| super(props); | ||
| this.state = this.getStateFromProps(props); | ||
| } | ||
|
|
||
| componentWillReceiveProps(nextProps) { | ||
| this.setState(this.getStateFromProps(nextProps)); | ||
| } | ||
|
|
||
| getStateFromProps(props) { | ||
| const formData = Array.isArray(props.formData) ? props.formData : null; | ||
| const {definitions} = this.props.registry; | ||
| return { | ||
| items: getDefaultFormState(props.schema, formData, definitions) || [] | ||
| }; | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The constructor is now unnecessary. Also that means we don't have local state anymore, which means we should use a functional stateless component instead of a component class. Which is so nice. |
||
| shouldComponentUpdate(nextProps, nextState) { | ||
|
|
@@ -189,36 +176,29 @@ class ArrayField extends Component { | |
| return itemsSchema.type === "string" && itemsSchema.minLength > 0; | ||
| } | ||
|
|
||
| asyncSetState(state, options={validate: false}) { | ||
| setState(this, state, () => { | ||
| this.props.onChange(this.state.items, options); | ||
| }); | ||
| } | ||
|
|
||
| onAddClick = (event) => { | ||
| event.preventDefault(); | ||
| const {items} = this.state; | ||
| const {schema, registry} = this.props; | ||
| const {schema, registry, formData} = this.props; | ||
| const {definitions} = registry; | ||
| let itemSchema = schema.items; | ||
| if (isFixedItems(schema) && allowAdditionalItems(schema)) { | ||
| itemSchema = schema.additionalItems; | ||
| } | ||
| this.asyncSetState({ | ||
| items: items.concat([ | ||
| getDefaultFormState(itemSchema, undefined, definitions) | ||
| ]) | ||
| }); | ||
| this.props.onChange([ | ||
| ...formData, | ||
| getDefaultFormState(itemSchema, undefined, definitions) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be worth renaming
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure whether it is appropriate to do it at the moment because getDefaultFormState is used by the root Form component as well. |
||
| ], {validate: false}); | ||
| }; | ||
|
|
||
| onDropIndexClick = (index) => { | ||
| return (event) => { | ||
| if (event) { | ||
| event.preventDefault(); | ||
| } | ||
| this.asyncSetState({ | ||
| items: this.state.items.filter((_, i) => i !== index) | ||
| }, {validate: true}); // refs #195 | ||
| this.props.onChange( | ||
| this.props.formData.filter((_, i)=> i !== index), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: missing space before |
||
| {validate: true} // refs #195 | ||
| ); | ||
| }; | ||
| }; | ||
|
|
||
|
|
@@ -228,33 +208,35 @@ class ArrayField extends Component { | |
| event.preventDefault(); | ||
| event.target.blur(); | ||
| } | ||
| const {items} = this.state; | ||
| this.asyncSetState({ | ||
| items: items.map((item, i) => { | ||
| const items = this.props.formData; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this can alternatively be written as |
||
| this.props.onChange( | ||
| items.map((item, i) => { | ||
| if (i === newIndex) { | ||
| return items[index]; | ||
| } else if (i === index) { | ||
| return items[newIndex]; | ||
| } else { | ||
| return item; | ||
| } | ||
| }) | ||
| }, {validate: true}); | ||
| }), | ||
| {validate: true} | ||
| ); | ||
| }; | ||
| }; | ||
|
|
||
| onChangeForIndex = (index) => { | ||
| return (value) => { | ||
| this.asyncSetState({ | ||
| items: this.state.items.map((item, i) => { | ||
| this.props.onChange( | ||
| this.props.formData.map((item, i) => { | ||
| return index === i ? value : item; | ||
| }) | ||
| }); | ||
| }), | ||
| {validate: false} | ||
| ); | ||
| }; | ||
| }; | ||
|
|
||
| onSelectChange = (value) => { | ||
| this.asyncSetState({items: value}); | ||
| this.props.onChange(value, {validate: false}); | ||
| }; | ||
|
|
||
| render() { | ||
|
|
@@ -287,7 +269,7 @@ class ArrayField extends Component { | |
| onBlur | ||
| } = this.props; | ||
| const title = (schema.title === undefined) ? name : schema.title; | ||
| const {items = []} = this.state; | ||
| const items = this.props.formData; | ||
| const {ArrayFieldTemplate, definitions, fields} = registry; | ||
| const {TitleField, DescriptionField} = fields; | ||
| const itemsSchema = retrieveSchema(schema.items, definitions); | ||
|
|
@@ -332,7 +314,7 @@ class ArrayField extends Component { | |
|
|
||
| renderMultiSelect() { | ||
| const {schema, idSchema, uiSchema, disabled, readonly, autofocus, onBlur} = this.props; | ||
| const {items} = this.state; | ||
| const items = this.props.formData; | ||
| const {widgets, definitions, formContext} = this.props.registry; | ||
| const itemsSchema = retrieveSchema(schema.items, definitions); | ||
| const enumOptions = optionsList(itemsSchema); | ||
|
|
@@ -357,7 +339,7 @@ class ArrayField extends Component { | |
| renderFiles() { | ||
| const {schema, uiSchema, idSchema, name, disabled, readonly, autofocus, onBlur} = this.props; | ||
| const title = schema.title || name; | ||
| const {items} = this.state; | ||
| const items = this.props.formData; | ||
| const {widgets, formContext} = this.props.registry; | ||
| const {widget="files", ...options} = getUiOptions(uiSchema); | ||
| const Widget = getWidget(schema, widget, widgets); | ||
|
|
@@ -393,7 +375,7 @@ class ArrayField extends Component { | |
| onBlur | ||
| } = this.props; | ||
| const title = schema.title || name; | ||
| let {items} = this.state; | ||
| let items = this.props.formData; | ||
| const {ArrayFieldTemplate, definitions, fields} = registry; | ||
| const {TitleField} = fields; | ||
| const itemSchemas = schema.items.map(item => | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,16 @@ | ||
| import React, {Component, PropTypes} from "react"; | ||
|
|
||
| import {deepEquals} from "../../utils"; | ||
|
|
||
|
|
||
| import { | ||
| getDefaultFormState, | ||
| orderProperties, | ||
| retrieveSchema, | ||
| shouldRender, | ||
| getDefaultRegistry, | ||
| setState | ||
| getDefaultRegistry | ||
| } from "../../utils"; | ||
|
|
||
|
|
||
| function objectKeysHaveChanged(formData, state) { | ||
| // for performance, first check for lengths | ||
| const newKeys = Object.keys(formData); | ||
| const oldKeys = Object.keys(state); | ||
| if (newKeys.length < oldKeys.length) { | ||
| return true; | ||
| } | ||
| // deep check on sorted keys | ||
| if (!deepEquals(newKeys.sort(), oldKeys.sort())) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| class ObjectField extends Component { | ||
| static defaultProps = { | ||
| uiSchema: {}, | ||
| formData: {}, | ||
| errorSchema: {}, | ||
| idSchema: {}, | ||
| registry: getDefaultRegistry(), | ||
|
|
@@ -40,25 +21,6 @@ class ObjectField extends Component { | |
|
|
||
| constructor(props) { | ||
| super(props); | ||
| this.state = this.getStateFromProps(props); | ||
| } | ||
|
|
||
| componentWillReceiveProps(nextProps) { | ||
| const state = this.getStateFromProps(nextProps); | ||
| const {formData} = nextProps; | ||
| if (formData && objectKeysHaveChanged(formData, this.state)) { | ||
| // We *need* to replace state entirely here has we have received formData | ||
| // holding different keys (so with some removed). | ||
| this.state = state; | ||
| this.forceUpdate(); | ||
| } else { | ||
| this.setState(state); | ||
| } | ||
| } | ||
|
|
||
| getStateFromProps(props) { | ||
| const {schema, formData, registry} = props; | ||
| return getDefaultFormState(schema, formData, registry.definitions) || {}; | ||
| } | ||
|
|
||
| shouldComponentUpdate(nextProps, nextState) { | ||
|
|
@@ -71,21 +33,21 @@ class ObjectField extends Component { | |
| schema.required.indexOf(name) !== -1; | ||
| } | ||
|
|
||
| asyncSetState(state, options={validate: false}) { | ||
| setState(this, state, () => { | ||
| this.props.onChange(this.state, options); | ||
| }); | ||
| } | ||
|
|
||
| onPropertyChange = (name) => { | ||
| return (value, options) => { | ||
| this.asyncSetState({[name]: value}, options); | ||
| const newFormData = Object.assign( | ||
| {}, | ||
| this.props.formData, | ||
| {[name]: value} | ||
| ); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can do: const newFormData = {...this.props.formData, [name]: value}; |
||
| this.props.onChange(newFormData, options); | ||
| }; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same remark as with |
||
| }; | ||
|
|
||
| render() { | ||
| const { | ||
| uiSchema, | ||
| formData, | ||
| errorSchema, | ||
| idSchema, | ||
| name, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm so happy seeing this going away 👍
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Me too :) |
||
|
|
@@ -135,7 +97,7 @@ class ObjectField extends Component { | |
| uiSchema={uiSchema[name]} | ||
| errorSchema={errorSchema[name]} | ||
| idSchema={idSchema[name]} | ||
| formData={this.state[name]} | ||
| formData={formData[name]} | ||
| onChange={this.onPropertyChange(name)} | ||
| onBlur={onBlur} | ||
| registry={this.props.registry} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should check that this is still needed elsewhere. If not, we should remove that helper function, which is now obsolete.