Skip to content
This repository was archived by the owner on Jan 14, 2021. It is now read-only.

Commit 02eab1b

Browse files
committed
remove trigger and replaced with mode
1 parent 0496a15 commit 02eab1b

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function App() {
8080
| `setValue` | Function || React Hook Form <a href="https://react-hook-form.com/api#setValue">setValue</a> function |
8181
| `register` | Function || React Hook Form <a href="https://react-hook-form.com/api#register">register</a> function |
8282
| `name` | string || Unique name for React Hook Form to register the input |
83-
| `trigger` | boolean | | Should trigger validation |
83+
| `mode` | string | | default to `onSubmit` |
8484
| `rules` | Object | | Validation rules according to <a href="https://react-hook-form.com/api#register">register</a> at React Hook Form |
8585
| `type` | string | | Currently support `checkbox` or `input` input type includes: `radio` and `select` |
8686
| `value` | string | | value can be applied for `checkbox` |

src/index.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ describe('React Hook Form Input', () => {
5656
},
5757
});
5858

59-
expect(setValue).toBeCalledWith('test', 'test', undefined);
59+
expect(setValue).toBeCalledWith('test', 'test', false);
6060
});
6161
});

src/index.tsx

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,55 @@ type Props = {
2828
rules?: ValidationOptions;
2929
value?: string | boolean;
3030
onChange?: (value: any) => void;
31-
trigger?: boolean;
31+
onBlur?: (value: any) => void;
32+
mode?: 'onBlur' | 'onChange' | 'onSubmit';
3233
};
3334

35+
function getValue(e: any, { isCheckbox }: { isCheckbox: boolean }) {
36+
return e.target ? (isCheckbox ? e.target.checked : e.target.value) : e;
37+
}
38+
3439
const HookFormInput = ({
3540
setValue,
3641
name,
3742
register,
3843
rules,
39-
trigger,
44+
mode = 'onSubmit',
4045
component,
4146
onChange,
47+
onBlur,
4248
type,
4349
value,
4450
...rest
4551
}: Props) => {
4652
const isCheckbox = type === 'checkbox';
53+
const isOnChange = mode === 'onChange';
54+
const isOnBlur = mode === 'onBlur';
4755
const [inputValue, setInputValue] = React.useState(isCheckbox ? false : '');
4856
const valueRef = React.useRef();
49-
const handleChange = (e: any) => {
50-
const data = e.target
51-
? isCheckbox
52-
? e.target.checked
53-
: e.target.value
54-
: e;
57+
const commonTask = (e: any) => {
58+
const data = getValue(e, { isCheckbox });
5559
setInputValue(data);
56-
setValue(name, data, trigger);
5760
valueRef.current = data;
61+
return data;
62+
};
63+
64+
const handleChange = (e: any) => {
65+
const data = commonTask(e);
66+
setValue(name, data, isOnChange);
5867
if (onChange) {
5968
onChange(e);
6069
}
6170
};
6271

72+
const handleBlur = (e: any) => {
73+
const data = commonTask(e);
74+
setValue(name, data, isOnBlur);
75+
if (onBlur) {
76+
onBlur(e);
77+
}
78+
};
79+
6380
React.useEffect(() => {
6481
register(
6582
Object.defineProperty(
@@ -85,6 +102,7 @@ const HookFormInput = ({
85102
return React.cloneElement(component, {
86103
...rest,
87104
onChange: handleChange,
105+
...(isOnBlur ? { onBlur: handleBlur } : {}),
88106
value: value || inputValue,
89107
...(isCheckbox ? { checked: inputValue } : {}),
90108
});

0 commit comments

Comments
 (0)