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

refactor(react): handle input, blur, reset by react event #510

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/conform-react/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { FormMetadata, FieldMetadata, Metadata, Pretty } from './context';
import {
type FormMetadata,
type FieldMetadata,
type Metadata,
type Pretty,
getWrappedFormContext,
} from './context';

type FormControlProps = {
key: string | undefined;
Expand Down Expand Up @@ -177,9 +183,16 @@ export function getFormProps<Schema extends Record<string, any>, FormError>(
metadata: FormMetadata<Schema, FormError>,
options?: FormControlOptions,
) {
const { onInput, onBlur, onReset } = getWrappedFormContext(
// TODO: fix type
metadata.context as any,
);
return simplify({
id: metadata.id,
onSubmit: metadata.onSubmit,
onInput: (e: React.FormEvent<HTMLFormElement>) => onInput(e.nativeEvent),
onBlur: (e: React.FocusEvent<HTMLFormElement>) => onBlur(e.nativeEvent),
onReset: (e: React.FormEvent<HTMLFormElement>) => onReset(e.nativeEvent),
noValidate: metadata.noValidate,
...getAriaAttributes(metadata, options),
});
Expand Down
12 changes: 0 additions & 12 deletions packages/conform-react/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,6 @@ export function useForm<
createFormContext({ ...formConfig, formId }),
);

useSafeLayoutEffect(() => {
document.addEventListener('input', context.onInput);
document.addEventListener('focusout', context.onBlur);
document.addEventListener('reset', context.onReset);

return () => {
document.removeEventListener('input', context.onInput);
document.removeEventListener('focusout', context.onBlur);
document.removeEventListener('reset', context.onReset);
};
}, [context]);

useSafeLayoutEffect(() => {
context.onUpdate({ ...formConfig, formId });
});
Expand Down