Skip to content

Commit

Permalink
feat: add form handler component
Browse files Browse the repository at this point in the history
  • Loading branch information
etc-tiago committed Feb 24, 2021
1 parent 6e277d7 commit dcb80b2
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import dot from "dot-object";
import React, { FC, FormEvent, useCallback } from "react";

const getAttrs = (element: any): { name: string; value: string | boolean } => {
const type = element.type;
const name = element.getAttribute("name");
let value = element.value || "";
if (!type || type === "button" || type === "submit") {
return { name: "", value: "" };
}
if (type === "checkbox") {
value = value === "on";
}
return { name, value };
};

export const Form: FC<
JSX.IntrinsicElements["form"] & { onSubmit: (values: any) => void }
> = ({ onSubmit, children, ...props }) => {
const handleSubmit = useCallback(
(event: FormEvent) => {
if (event) {
event.preventDefault();
}
const target = event.target as any;
const elementsKeys = Object.keys(target.elements);

const objects: any = {};

elementsKeys.forEach((key: any) => {
if ([target.elements[key]].toString() === "[object RadioNodeList]") {
target.elements[key].forEach((el) => {
const { name, value } = getAttrs(el);
if (name) {
dot.set(name, value, objects, true);
}
});
} else {
const { name, value } = getAttrs(target.elements[key]);
if (name) {
dot.set(name, value, objects, true);
}
}
});

onSubmit(objects);
},
[onSubmit],
);

return (
<form {...props} onSubmit={handleSubmit}>
{children}
</form>
);
};

export default Form;

0 comments on commit dcb80b2

Please sign in to comment.