-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Remove componentWillReceiveProps from Form.jsx #2010
Changes from 3 commits
a9d8e05
2d35019
33aee98
4532e45
f26788b
28c697c
092aab0
ed89981
181f0da
a40ce16
ed618c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import React from "react"; | |
import * as ReactIs from "react-is"; | ||
import mergeAllOf from "json-schema-merge-allof"; | ||
import fill from "core-js/library/fn/array/fill"; | ||
import validateFormData, { isValid } from "./validate"; | ||
import validateFormData, { isValid, toErrorList } from "./validate"; | ||
import union from "lodash/union"; | ||
import jsonpointer from "jsonpointer"; | ||
|
||
|
@@ -1243,3 +1243,115 @@ export function schemaRequiresTrueValue(schema) { | |
|
||
return false; | ||
} | ||
|
||
export function getRegistry(props) { | ||
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. We should move all of these new functions in utils.js to just Form.js (because they're quite Form-specific) 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. can we just make them static functions (such as |
||
const { fields, widgets } = getDefaultRegistry(); | ||
return { | ||
fields: { ...fields, ...props.fields }, | ||
widgets: { ...widgets, ...props.widgets }, | ||
ArrayFieldTemplate: props.ArrayFieldTemplate, | ||
ObjectFieldTemplate: props.ObjectFieldTemplate, | ||
FieldTemplate: props.FieldTemplate, | ||
definitions: props.schema.definitions || {}, | ||
rootSchema: props.schema, | ||
formContext: props.formContext || {}, | ||
}; | ||
} | ||
|
||
export function validate( | ||
formData, | ||
props, | ||
schema = props.schema, | ||
additionalMetaSchemas = props.additionalMetaSchemas, | ||
customFormats = props.customFormats | ||
) { | ||
const { validate, transformErrors } = props; | ||
const { rootSchema } = getRegistry(props); | ||
const resolvedSchema = retrieveSchema(schema, rootSchema, formData); | ||
return validateFormData( | ||
formData, | ||
resolvedSchema, | ||
validate, | ||
transformErrors, | ||
additionalMetaSchemas, | ||
customFormats | ||
); | ||
} | ||
|
||
export function getStateFromProps(props, inputFormData, state = {}) { | ||
const edit = typeof inputFormData !== "undefined"; | ||
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. We should figure out -- why does this omit several lines from the original 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 lines omitted are mostly just variable declarations that I pick up directly from the prop object instead |
||
const mustValidate = edit && !props.noValidate && props.liveValidate; | ||
const formData = getDefaultFormState( | ||
props.schema, | ||
inputFormData, | ||
props.schema | ||
); | ||
const retrievedSchema = retrieveSchema(props.schema, props.schema, formData); | ||
|
||
const getCurrentErrors = () => { | ||
if (props.noValidate) { | ||
return { errors: [], errorSchema: {} }; | ||
} else if (!props.liveValidate) { | ||
return { | ||
errors: state.schemaValidationErrors || [], | ||
errorSchema: state.schemaValidationErrorSchema || {}, | ||
}; | ||
} | ||
return { | ||
errors: state.errors || [], | ||
errorSchema: state.errorSchema || {}, | ||
}; | ||
}; | ||
|
||
let errors, errorSchema, schemaValidationErrors, schemaValidationErrorSchema; | ||
if (mustValidate) { | ||
const schemaValidation = validate( | ||
formData, | ||
props, | ||
props.schema, | ||
props.additionalMetaSchemas, | ||
props.customFormats | ||
); | ||
errors = schemaValidation.errors; | ||
errorSchema = schemaValidation.errorSchema; | ||
schemaValidationErrors = errors; | ||
schemaValidationErrorSchema = errorSchema; | ||
} else { | ||
const currentErrors = getCurrentErrors(); | ||
errors = currentErrors.errors; | ||
errorSchema = currentErrors.errorSchema; | ||
schemaValidationErrors = state.schemaValidationErrors; | ||
schemaValidationErrorSchema = state.schemaValidationErrorSchema; | ||
} | ||
if (props.extraErrors) { | ||
errorSchema = mergeObjects( | ||
errorSchema, | ||
props.extraErrors, | ||
!!"concat arrays" | ||
); | ||
errors = toErrorList(errorSchema); | ||
} | ||
const idSchema = toIdSchema( | ||
retrievedSchema, | ||
props.uiSchema["ui:rootFieldId"], | ||
props.schema, | ||
formData, | ||
props.idPrefix | ||
); | ||
const nextState = { | ||
schema: props.schema, | ||
uiSchema: props.uiSchema, | ||
idSchema, | ||
formData, | ||
edit, | ||
errors, | ||
errorSchema, | ||
additionalMetaSchemas: props.additionalMetaSchemas, | ||
lastProps: props, | ||
}; | ||
if (schemaValidationErrors) { | ||
nextState.schemaValidationErrors = schemaValidationErrors; | ||
nextState.schemaValidationErrorSchema = schemaValidationErrorSchema; | ||
} | ||
return nextState; | ||
} |
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.
Figure out -- why do we need
lastProps
?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.
On line 62 we need to compare prop changes to determine if the consumer has updated their props.
If the consumer has updated the props, we update the internal state based on the props.
We could previously do this in componentWillReceiveProps since that method supports both viewing previous and new props. But since getDerivedStateFromProps only shows the new prop updates, we need to store the old props in the state.
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.
And the reason why we cannot always derive state from props, is due to us supporting the mixed controlled/uncontrolled behavior we discussed on our last meeting this Friday.