-
-
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 all 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, | ||
|
|
@@ -159,23 +159,6 @@ class ArrayField extends Component { | |
| autofocus: false, | ||
| }; | ||
|
|
||
| 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) || [] | ||
| }; | ||
| } | ||
|
|
||
| shouldComponentUpdate(nextProps, nextState) { | ||
| return shouldRender(this, nextProps, nextState); | ||
| } | ||
|
|
@@ -189,36 +172,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), | ||
| {validate: true} // refs #195 | ||
| ); | ||
| }; | ||
| }; | ||
|
|
||
|
|
@@ -228,33 +204,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() { | ||
|
|
@@ -275,6 +253,7 @@ class ArrayField extends Component { | |
| const { | ||
| schema, | ||
| uiSchema, | ||
| formData, | ||
| errorSchema, | ||
| idSchema, | ||
| name, | ||
|
|
@@ -287,26 +266,25 @@ class ArrayField extends Component { | |
| onBlur | ||
| } = this.props; | ||
| const title = (schema.title === undefined) ? name : schema.title; | ||
| const {items = []} = this.state; | ||
| const {ArrayFieldTemplate, definitions, fields} = registry; | ||
| const {TitleField, DescriptionField} = fields; | ||
| const itemsSchema = retrieveSchema(schema.items, definitions); | ||
| const {addable=true} = getUiOptions(uiSchema); | ||
|
|
||
| const arrayProps = { | ||
| canAdd: addable, | ||
| items: items.map((item, index) => { | ||
| items: formData.map((item, index) => { | ||
| const itemErrorSchema = errorSchema ? errorSchema[index] : undefined; | ||
| const itemIdPrefix = idSchema.$id + "_" + index; | ||
| const itemIdSchema = toIdSchema(itemsSchema, itemIdPrefix, definitions); | ||
| return this.renderArrayFieldItem({ | ||
| index, | ||
| canMoveUp: index > 0, | ||
| canMoveDown: index < items.length - 1, | ||
| canMoveDown: index < formData.length - 1, | ||
| itemSchema: itemsSchema, | ||
| itemIdSchema, | ||
| itemErrorSchema, | ||
| itemData: items[index], | ||
| itemData: formData[index], | ||
| itemUiSchema: uiSchema.items, | ||
| autofocus: autofocus && index === 0, | ||
| onBlur | ||
|
|
@@ -332,7 +310,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 +335,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 +371,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 => | ||
|
|
||
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.