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

fix(@uform/core): fix init visible can not remove value #492

Merged
merged 9 commits into from
Dec 10, 2019
1 change: 1 addition & 0 deletions packages/antd/src/components/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const Submit = ({ showLoading, onSubmit, ...props }: ISubmitProps) => {
return (
<Button
type="primary"
htmlType="submit"
onClick={() => form.submit(onSubmit)}
disabled={showLoading ? state.submitting : undefined}
{...props}
Expand Down
29 changes: 16 additions & 13 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export function createForm<FieldProps, VirtualFieldProps>(
const mountedChanged = field.isDirty('mounted')
const initializedChanged = field.isDirty('initialized')
const warningsChanged = field.isDirty('warnings')
const errorsChanges = field.isDirty('errors')
const errorsChanged = field.isDirty('errors')
const userUpdateFieldPath =
env.userUpdateFields[env.userUpdateFields.length - 1]
if (initializedChanged) {
Expand All @@ -194,6 +194,16 @@ export function createForm<FieldProps, VirtualFieldProps>(
}, true)
}
}
if (valueChanged) {
userUpdating(field, () => {
setFormValuesIn(path, published.value)
})
heart.publish(LifeCycleTypes.ON_FIELD_VALUE_CHANGE, field)
}
if (initialValueChanged) {
setFormInitialValuesIn(path, published.initialValue)
heart.publish(LifeCycleTypes.ON_FIELD_INITIAL_VALUE_CHANGE, field)
}
if (displayChanged || visibleChanged) {
if (visibleChanged) {
if (!published.visible) {
Expand Down Expand Up @@ -228,18 +238,8 @@ export function createForm<FieldProps, VirtualFieldProps>(
if (mountedChanged && published.mounted) {
heart.publish(LifeCycleTypes.ON_FIELD_MOUNT, field)
}
if (valueChanged) {
userUpdating(field, () => {
setFormValuesIn(path, published.value)
})
heart.publish(LifeCycleTypes.ON_FIELD_VALUE_CHANGE, field)
}
if (initialValueChanged) {
setFormInitialValuesIn(path, published.initialValue)
heart.publish(LifeCycleTypes.ON_FIELD_INITIAL_VALUE_CHANGE, field)
}

if (errorsChanges) {
if (errorsChanged) {
syncFormMessages('errors', published.name, published.errors)
}

Expand Down Expand Up @@ -703,7 +703,10 @@ export function createForm<FieldProps, VirtualFieldProps>(
return arr
},
validate(opts?: IFormExtendedValidateFieldOptions) {
return validate(field.getSourceState(state => state.path), opts)
return validate(
field.getSourceState(state => state.path),
opts
)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/components/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const Submit = ({ showLoading, onSubmit, ...props }: ISubmitProps) => {
return (
<Button
type="primary"
htmlType="submit"
onClick={async() => {
try {
await form.submit(onSubmit)
Expand Down
22 changes: 13 additions & 9 deletions packages/react-schema-renderer/src/hooks/useSchemaForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,21 @@ const useInternalSchemaForm = (props: ISchemaFormProps) => {
const { implementActions } = useEva({
actions
})
const formSchema = useMemo(() => {
const result = new Schema(schema)
implementActions({
getSchema: deprecate(() => result, 'Please use the getFormSchema.'),
getFormSchema: () => result
})
return result
}, [schema])
const registry = getRegistry()
return {
form: useForm(props),
formComponentProps,
formComponentProps:{
...formComponentProps,
...formSchema.getExtendsComponentProps()
},
fields: lowercaseKeys({
...registry.fields,
...fields
Expand All @@ -57,14 +68,7 @@ const useInternalSchemaForm = (props: ISchemaFormProps) => {
formItemComponent: formItemComponent
? formItemComponent
: registry.formItemComponent,
schema: useMemo(() => {
const result = new Schema(schema)
implementActions({
getSchema: deprecate(() => result, 'Please use the getFormSchema.'),
getFormSchema: () => result
})
return result
}, [schema]),
schema: formSchema,
children
}
}
Expand Down