react-component / react-component/field-form

Why no onFieldsChange in FormInstance?

Open
#174 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
1k
Forks
286
Avg merge
4d 16h
Merged PRs (30d)
1

Description

It is very limiting to have onFieldsChange only on <Form> instead of on FormInstance. I would like to be able to add a listener to form so other components can react to fields changing.

My current use case is automatically disabling the buttons in <Modal> if no fields are dirty:

/** 
 * Don't want to repeat logic over and over, want one Modal component that automatically
 * changes ok/cancel buttons based on form state
 */
function MyFormModal() {
	const [form] = Form.useForm()
	return (
		<Modal form={form} visible>
			<Form form={form}>
				...
			</Form>
		</Modal>
	)
}

// disable okButton if dirty
// only show cancelButton if dirty
const Modal = ({form, onFinish, ...props}) => {
	const [dirty, setDirty] = useState(false)
	if(form) {
                 // Really need this!!
		form.onFieldsChange(() => {
			setDirty(form.isFieldTouched())
		})
	}
	const buttonsProps = {
		cancelButtonProps: dirty ? cancelButtonProps : {...cancelButtonProps, style: {display: 'none'}},
		okButtonProps: dirty ? okButtonProps : {...okButtonProps, disabled: true}
	}
	return <AntModal {buttonsProps} {...props}/>
}

How can I achieve this currently?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue identifies FormInstance and

as the relevant entry points; start by reading their current field-change APIs and subscription behavior. Clarify listener lifecycle and dirty-state behavior from the modal example. Done means a FormInstance listener can observe field changes reliably and the behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.