Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Refactor(utils): cloneObject will not clone event object
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed Jan 2, 2021
1 parent 19b47df commit 569167b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-shirts-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-form": patch
---

Refactor(utils): `cloneObject` will not handle event object
23 changes: 14 additions & 9 deletions app/src/Playground/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useForm, Parser, Formatter } from "react-cool-form";
import { useForm, Parser } from "react-cool-form";
import { makeStyles } from "@material-ui/core/styles";
import {
FormControl,
InputLabel,
Expand All @@ -11,17 +12,23 @@ interface FormValues {
slider: any;
}

const useStyles = makeStyles({
root: {
width: 200,
},
});

const Playground = (): JSX.Element => {
const classes = useStyles();
const { form, controller } = useForm<FormValues>({
defaultValues: { age: "", slider: 0 },
onSubmit: (values, e) => alert(JSON.stringify(values, undefined, 2)),
});

const formatter: Formatter<number, number> = (value) => value / 2;
const parser: Parser<[Event, number], number> = (e, value) => value * 2;
const parser: Parser<[Event, number], number> = (e, value) => value;

return (
<form ref={form} noValidate>
<form ref={form} noValidate className={classes.root}>
{/* <div>
<FormControl>
<InputLabel id="age-native-helper">Age</InputLabel>
Expand All @@ -38,17 +45,15 @@ const Playground = (): JSX.Element => {
</NativeSelect>
</FormControl>
</div> */}
{/* <div>
<div>
<Slider
{...controller("slider", {
format: formatter,
parse: parser,
onChange: (e, value) => console.log("LOG ===> onChange: ", value),
// onChange: (e, value) => console.log("LOG ===> onChange: ", e),
})}
aria-labelledby="continuous-slider"
/>
</div> */}
<input type="range" {...controller("slider")} />
</div>
<input type="submit" />
</form>
);
Expand Down
2 changes: 2 additions & 0 deletions src/utils/cloneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import isObject from "./isObject";
import isFileList from "./isFileList";

const cloneObject = (object: unknown): any => {
if (object instanceof Event) throw new Error("Unable to clone event.");

if (!isObject(object) || isFileList(object)) return object;

if (object instanceof Date) return new Date(object.getTime());
Expand Down

0 comments on commit 569167b

Please sign in to comment.