Skip to content

Commit cca6c75

Browse files
authored
Merge pull request #58 from DouglasNeuroInformatics/dev
fix: rename beforeSubmit to onBeforeSubmit (forgot to follow convention)
2 parents 7ea436e + 7a120df commit cca6c75

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/components/Form/Form.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ export const WithError: StoryObj<typeof Form> = {
540540
variant: 'input'
541541
}
542542
},
543-
beforeSubmit: (data) => {
543+
onBeforeSubmit: (data) => {
544544
if (data.name === 'Winston') {
545545
return { success: true };
546546
}

src/components/Form/Form.test.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,13 @@ describe('Form', () => {
116116
});
117117
});
118118

119-
describe('custom beforeSubmit error', () => {
120-
let beforeSubmit: Mock;
119+
describe('custom onBeforeSubmit error', () => {
120+
let onBeforeSubmit: Mock;
121121

122122
beforeEach(() => {
123-
beforeSubmit = vi.fn();
123+
onBeforeSubmit = vi.fn();
124124
render(
125125
<Form
126-
beforeSubmit={beforeSubmit}
127126
content={{
128127
value: {
129128
kind: 'number',
@@ -135,6 +134,7 @@ describe('Form', () => {
135134
validationSchema={z.object({
136135
value: z.number({ message: 'Please enter a number' })
137136
})}
137+
onBeforeSubmit={onBeforeSubmit}
138138
onError={onError}
139139
onSubmit={onSubmit}
140140
/>
@@ -156,12 +156,12 @@ describe('Form', () => {
156156
'Please enter a number'
157157
])
158158
);
159-
expect(beforeSubmit).not.toHaveBeenCalled();
159+
expect(onBeforeSubmit).not.toHaveBeenCalled();
160160
expect(onSubmit).not.toHaveBeenCalled();
161161
});
162162

163-
it('should not allow submitting the form with the beforeSubmit error', async () => {
164-
beforeSubmit.mockResolvedValueOnce({ errorMessage: 'Invalid!', success: false });
163+
it('should not allow submitting the form with the onBeforeSubmit error', async () => {
164+
onBeforeSubmit.mockResolvedValueOnce({ errorMessage: 'Invalid!', success: false });
165165
const field: HTMLInputElement = screen.getByLabelText('Value');
166166
await userEvent.type(field, '-1');
167167
fireEvent.submit(screen.getByTestId(testid));
@@ -171,8 +171,8 @@ describe('Form', () => {
171171
expect(onSubmit).not.toHaveBeenCalled();
172172
});
173173

174-
it('should allow submitting the form if beforeSubmit returns true', async () => {
175-
beforeSubmit.mockResolvedValueOnce({ success: true });
174+
it('should allow submitting the form if onBeforeSubmit returns true', async () => {
175+
onBeforeSubmit.mockResolvedValueOnce({ success: true });
176176
const field: HTMLInputElement = screen.getByLabelText('Value');
177177
await userEvent.type(field, '-1');
178178
fireEvent.submit(screen.getByTestId(testid));

src/components/Form/Form.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ type FormProps<TSchema extends z.ZodType<FormDataType>, TData extends z.TypeOf<T
2929
left?: React.ReactNode;
3030
right?: React.ReactNode;
3131
};
32-
beforeSubmit?: (data: NoInfer<TData>) => Promisable<{ errorMessage: string; success: false } | { success: true }>;
3332
className?: string;
3433
content: FormContent<TData>;
3534
customStyles?: {
@@ -39,6 +38,7 @@ type FormProps<TSchema extends z.ZodType<FormDataType>, TData extends z.TypeOf<T
3938
fieldsFooter?: React.ReactNode;
4039
id?: string;
4140
initialValues?: PartialNullableFormDataType<NoInfer<TData>>;
41+
onBeforeSubmit?: (data: NoInfer<TData>) => Promisable<{ errorMessage: string; success: false } | { success: true }>;
4242
onError?: (error: z.ZodError<NoInfer<TData>>) => void;
4343
onSubmit: (data: NoInfer<TData>) => Promisable<void>;
4444
preventResetValuesOnReset?: boolean;
@@ -52,13 +52,13 @@ type FormProps<TSchema extends z.ZodType<FormDataType>, TData extends z.TypeOf<T
5252

5353
const Form = <TSchema extends z.ZodType<FormDataType>, TData extends z.TypeOf<TSchema> = z.TypeOf<TSchema>>({
5454
additionalButtons,
55-
beforeSubmit,
5655
className,
5756
content,
5857
customStyles,
5958
fieldsFooter,
6059
id,
6160
initialValues,
61+
onBeforeSubmit,
6262
onError,
6363
onSubmit,
6464
preventResetValuesOnReset,
@@ -116,8 +116,8 @@ const Form = <TSchema extends z.ZodType<FormDataType>, TData extends z.TypeOf<TS
116116
handleError(result.error);
117117
return;
118118
}
119-
if (beforeSubmit) {
120-
const beforeSubmitResult = await beforeSubmit(result.data);
119+
if (onBeforeSubmit) {
120+
const beforeSubmitResult = await onBeforeSubmit(result.data);
121121
if (!beforeSubmitResult.success) {
122122
setErrors({});
123123
setRootErrors([beforeSubmitResult.errorMessage]);

0 commit comments

Comments
 (0)