Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Update form after prop changes
dominik-zeglen2c85a334c5b2
diff --git a/saleor/static/dashboard-next/components/Form/Form.tsx b/saleor/static/dashboard-next/components/Form/Form.tsxindex 626fd4b4c9..09af981c00 100644--- a/saleor/static/dashboard-next/components/Form/Form.tsx+++ b/saleor/static/dashboard-next/components/Form/Form.tsx@@ -2,40 +2,82 @@ import * as React from "react";import { UserError } from "../../types";export interface FormProps<T extends {}> {- children:- | ((- props: {- data: T;- hasChanged: boolean;- errors: { [key: string]: string };- change(event: React.ChangeEvent<any>);- submit(event?: React.FormEvent<any>);- }- ) => React.ReactElement<any>)- | React.ReactNode;+ children: ((+ props: {+ data: T;+ hasChanged: boolean;+ errors: { [key: string]: string };+ change(event: React.ChangeEvent<any>);+ submit(event?: React.FormEvent<any>);+ }+ ) => React.ReactElement<any>);errors?: UserError[];initial?: T;useForm?: boolean;onSubmit?(data: T);}+interface FormState<T extends {}> {+ initial: T;+ fields: T;+}-class Form<T extends {} = {}> extends React.Component<FormProps<T>, T> {- state: T = this.props.initial;+class Form<T extends {} = {}> extends React.Component<+ FormProps<T>,+ FormState<T>+> {+ static getDerivedStateFromProps<T extends {} = {}>(+ nextProps: FormProps<T>,+ prevState: FormState<T>+ ): FormState<T> {+ const changedFields = Object.keys(nextProps.initial).filter(+ nextFieldName =>+ nextProps.initial[nextFieldName] !== prevState.initial[nextFieldName]+ );+ if (changedFields.length > 0) {+ const swapFields = changedFields.reduce((prev, curr) => {+ prev[curr] = nextProps.initial[curr];+ return prev;+ }, {});++ return {+ fields: {+ ...(prevState.fields as any),+ ...swapFields+ },+ initial: {+ ...(prevState.initial as any),+ ...swapFields+ }+ };+ }+ return null;+ }++ state: FormState<T> = {+ fields: this.props.initial,+ initial: this.props.initial+ };handleChange = (event: React.ChangeEvent<any>) => {const { target } = event;- if (!(target.name in this.state)) {+ if (!(target.name in this.state.fields)) {console.error(`Unknown form field: ${target.name}`);return;}- this.setState(({ [target.name]: target.value } as any) as Pick<T, keyof T>);++ this.setState({+ fields: {+ ...(this.state.fields as any),+ [target.name]: target.value+ }+ });};handleKeyDown = (event: React.KeyboardEvent<any>) => {switch (event.keyCode) {// Entercase 13:- this.props.onSubmit(this.state);+ this.props.onSubmit(this.state.fields);break;}};@@ -47,33 +89,30 @@ class Form<T extends {} = {}> extends React.Component<FormProps<T>, T> {event.preventDefault();}if (onSubmit !== undefined) {- onSubmit(this.state);+ onSubmit(this.state.fields);}};render() {const { children, errors, useForm = true } = this.props;- let contents = children;-- if (typeof children === "function") {- contents = children({- change: this.handleChange,- data: this.state,- errors: errors- ? errors.reduce(- (prev, curr) => ({- ...prev,- [curr.field.split(":")[0]]: curr.message- }),- {}- )- : {},- hasChanged:- JSON.stringify(this.props.initial) !== JSON.stringify(this.state),- submit: this.handleSubmit- });- }+ const contents = children({+ change: this.handleChange,+ data: this.state.fields,+ errors: errors+ ? errors.reduce(+ (prev, curr) => ({+ ...prev,+ [curr.field.split(":")[0]]: curr.message+ }),+ {}+ )+ : {},+ hasChanged:+ JSON.stringify(this.state.initial) !==+ JSON.stringify(this.state.fields),+ submit: this.handleSubmit+ });return useForm ? (<form onSubmit={this.handleSubmit}>{contents}</form>