Skip to content

Commit

Permalink
fix: validatedClass propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
schummar committed Jan 28, 2025
1 parent 6718e54 commit b3f0a06
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion examples/form/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ const { Form, Field } = createForm<{ a: string; b: string }>({
required: (value) => !!value,
},
},
validatedClass: 'valid-foo',
});

export default function App() {
return (
<Form onSubmit={(_e, form) => form.reset()}>
<Form onSubmit={(_e, form) => form.reset()} validatedClass="valid-bar">
<Content />
</Form>
);
Expand Down
4 changes: 2 additions & 2 deletions examples/form/src/index.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
form.validated input:invalid,
form.validated input:invalid ~ fieldset {
form.valid-bar input:invalid,
form.valid-bar input:invalid ~ fieldset {
box-shadow: 0 0 5px red;
}
17 changes: 15 additions & 2 deletions src/react/form/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function FormContainer({
{...formProps}
className={[
formProps.className,
hasTriggeredValidations ? (form.options.validatedClass ?? 'validated') : undefined,
hasTriggeredValidations ? (formInstance.options.validatedClass ?? 'validated') : undefined,
]
.filter(Boolean)
.join(' ')}
Expand Down Expand Up @@ -391,6 +391,7 @@ export class Form<TDraft, TOriginal extends TDraft = TDraft> {
urlState,
autoSave,
transform,
validatedClass,
...formProps
}: {
original?: TOriginal;
Expand All @@ -406,6 +407,7 @@ export class Form<TDraft, TOriginal extends TDraft = TDraft> {
localizeError: localizeError ?? this.options.localizeError,
autoSave: autoSave ?? this.options.autoSave,
transform: transform ?? this.options.transform,
validatedClass: validatedClass ?? this.options.validatedClass,
};

const formState = useMemo(() => {
Expand Down Expand Up @@ -488,7 +490,18 @@ export class Form<TDraft, TOriginal extends TDraft = TDraft> {
formState.set('hasTriggeredValidations', false);
},
} satisfies FormContext<TDraft, TOriginal>;
}, [formState, derivedState, original, defaultValue, validations, localizeError, urlState]);
}, [
formState,
derivedState,
original,
defaultValue,
validations,
localizeError,
urlState,
autoSave,
transform,
validatedClass,
]);

useEffect(() => {
if (urlState) {
Expand Down

0 comments on commit b3f0a06

Please sign in to comment.