Skip to content

Commit

Permalink
Form: Re-add onBeforeSubmit
Browse files Browse the repository at this point in the history
  • Loading branch information
drublic committed Dec 3, 2020
1 parent 6251214 commit 67b4960
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 27 deletions.
28 changes: 14 additions & 14 deletions src/Form/__tests__/__snapshots__/Form.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports[`renders correctly 1`] = `
"entries": Array [
Object {
"hash": "",
"key": "stas8q",
"key": "ws74gx",
"pathname": "/",
"search": "",
"state": undefined,
Expand All @@ -25,7 +25,7 @@ exports[`renders correctly 1`] = `
"listen": [Function],
"location": Object {
"hash": "",
"key": "stas8q",
"key": "ws74gx",
"pathname": "/",
"search": "",
"state": undefined,
Expand Down Expand Up @@ -130,7 +130,6 @@ exports[`renders correctly 1`] = `
},
Object {
"id": "email",
"onBeforeSubmit": [Function],
"title": "Email",
"type": "email",
"width": "mid",
Expand All @@ -144,6 +143,7 @@ exports[`renders correctly 1`] = `
Object {
"helperText": "Should be URL with trailing slash",
"id": "url",
"onBeforeSubmit": [Function],
"title": "URL",
"type": "url",
"validators": Array [
Expand All @@ -167,7 +167,7 @@ exports[`renders correctly 1`] = `
Object {
"data": Array [
Object {
"format": "DD.MM.YYYY",
"format": "dd.MM.yyyy",
"id": "date",
"title": "Date",
"type": "date",
Expand All @@ -189,7 +189,7 @@ exports[`renders correctly 1`] = `
"width": "small",
},
Object {
"format": "DD.MM.YYYY, hh:mm a",
"format": "dd.MM.yyyy, hh:mm a",
"id": "datetime",
"title": "Datetime",
"type": "datetime",
Expand Down Expand Up @@ -240,8 +240,8 @@ exports[`renders correctly 1`] = `
"width": "small",
},
Object {
"id": "switch",
"title": "Switch - Prefilld",
"id": "switch-refilled",
"title": "Switch - Prefilled",
"type": "switch",
"value": true,
"width": "small",
Expand Down Expand Up @@ -276,7 +276,7 @@ exports[`renders correctly with content 1`] = `
"entries": Array [
Object {
"hash": "",
"key": "f4lvuf",
"key": "ur51sp",
"pathname": "/",
"search": "",
"state": undefined,
Expand All @@ -290,7 +290,7 @@ exports[`renders correctly with content 1`] = `
"listen": [Function],
"location": Object {
"hash": "",
"key": "f4lvuf",
"key": "ur51sp",
"pathname": "/",
"search": "",
"state": undefined,
Expand Down Expand Up @@ -395,7 +395,6 @@ exports[`renders correctly with content 1`] = `
},
Object {
"id": "email",
"onBeforeSubmit": [Function],
"title": "Email",
"type": "email",
"width": "mid",
Expand All @@ -409,6 +408,7 @@ exports[`renders correctly with content 1`] = `
Object {
"helperText": "Should be URL with trailing slash",
"id": "url",
"onBeforeSubmit": [Function],
"title": "URL",
"type": "url",
"validators": Array [
Expand All @@ -432,7 +432,7 @@ exports[`renders correctly with content 1`] = `
Object {
"data": Array [
Object {
"format": "DD.MM.YYYY",
"format": "dd.MM.yyyy",
"id": "date",
"title": "Date",
"type": "date",
Expand All @@ -454,7 +454,7 @@ exports[`renders correctly with content 1`] = `
"width": "small",
},
Object {
"format": "DD.MM.YYYY, hh:mm a",
"format": "dd.MM.yyyy, hh:mm a",
"id": "datetime",
"title": "Datetime",
"type": "datetime",
Expand Down Expand Up @@ -505,8 +505,8 @@ exports[`renders correctly with content 1`] = `
"width": "small",
},
Object {
"id": "switch",
"title": "Switch - Prefilld",
"id": "switch-refilled",
"title": "Switch - Prefilled",
"type": "switch",
"value": true,
"width": "small",
Expand Down
52 changes: 48 additions & 4 deletions src/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,38 @@ type FormProps = {
isFixedSubmitButton?: boolean;
};

const fieldIsField = (
field: FormFieldGroup | FormFieldField | undefined,
): field is FormFieldField => {
return !!field && !(field as any).group;
};

const fieldIsGroup = (
field: FormFieldGroup | FormFieldField | undefined,
): field is FormFieldGroup => {
return !!field && (field as any).group;
};

const findFields = (fields: FormField[], key: string): FormFieldField[] => {
const matchingFields = fields.flatMap((field) => {
if (fieldIsGroup(field)) {
return findFields(field.data, key);
}

if (field.id === key) {
return [field];
}

return [];
});

return matchingFields;
};

const Form: FunctionComponent<FormProps> = ({
form,
data,
onSubmit: onSubmitProps,
onSubmit: onSubmitProp,
onDataChanged,
...props
}) => {
Expand All @@ -113,10 +141,26 @@ const Form: FunctionComponent<FormProps> = ({
return;
}

const response = getMappedData(state);
const mappedResponse = getMappedData(state);
const response = { ...mappedResponse };

Object.entries(mappedResponse).map(([key, value]) => {
const matchingField = findFields(form, key)?.[0];

if (fieldIsField(matchingField) && matchingField?.onBeforeSubmit) {
console.log({
onBeforeSubmit: matchingField.onBeforeSubmit(value),
key,
});
response[key] = matchingField.onBeforeSubmit(value);

onSubmitProps(response);
}, [form, state, onSubmitProps]);
return;
}

response[key] = value;
});
onSubmitProp(response);
}, [form, state, onSubmitProp]);

return (
<FormContext.Provider value={{ state, dispatch }}>
Expand Down
18 changes: 9 additions & 9 deletions src/__visual__/data/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,6 @@ const form: FormField[] = [
title: "Email",
type: TYPES.EMAIL,
width: "mid",
onBeforeSubmit: (url?: string) => {
if (url) {
return `${url}#top`;
}

return url;
},
},
{
id: "password",
Expand All @@ -111,6 +104,13 @@ const form: FormField[] = [
message: "Please add a trailing slash to your URL",
},
],
onBeforeSubmit: (url?: string) => {
if (url) {
return `${url}#top`;
}

return url;
},
},
{
id: "empty",
Expand Down Expand Up @@ -188,8 +188,8 @@ const form: FormField[] = [
width: WIDTH.SMALL,
},
{
id: "switch",
title: "Switch - Prefilld",
id: "switch-refilled",
title: "Switch - Prefilled",
type: TYPES.SWITCH,
value: true,
width: WIDTH.SMALL,
Expand Down

0 comments on commit 67b4960

Please sign in to comment.