This repository has been archived by the owner on Jan 14, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.tsx
148 lines (135 loc) Β· 3.53 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import * as React from 'react';
import { useFormContext } from 'react-hook-form';
import { Props, EventFunction } from './types';
const isUndefined = (val: unknown): val is undefined => val === undefined;
function getValue(target: any, { isCheckbox }: { isCheckbox: boolean }) {
// the following logic is specific for react-select
if (target && (Array.isArray(target) || (target.label && target.value))) {
return target;
}
return target
? isCheckbox
? isUndefined(target.checked)
? target
: target.checked
: isUndefined(target.value)
? target
: target.value
: target;
}
const RHFInput = ({
innerProps,
setValue: setValueFromProp,
register: registerFromProp,
unregister: unregisterFromProp,
name,
rules,
mode = 'onSubmit',
as: InnerComponent,
onChange,
onBlur,
type,
value,
defaultValue,
defaultChecked,
onChangeName,
onChangeEvent,
onBlurName,
onBlurEvent,
...rest
}: Props) => {
const isCheckbox = type === 'checkbox';
const isOnChange = mode === 'onChange';
const isOnBlur = mode === 'onBlur';
const defaultData = isCheckbox
? isUndefined(defaultChecked)
? false
: defaultChecked
: defaultValue;
const [inputValue, setInputValue] = React.useState(defaultData);
const valueRef = React.useRef(defaultData);
const methods = useFormContext() || {};
const setValue = setValueFromProp || methods.setValue;
const register = registerFromProp || methods.register;
const unregister = unregisterFromProp || methods.unregister;
const commonTask = (target: any) => {
const data = getValue(target, { isCheckbox });
setInputValue(data);
valueRef.current = data;
return data;
};
const eventWrapper = (event: EventFunction, eventName: string) => {
return async (...arg: any) => {
const data = commonTask(await event(arg));
setValue(
name,
data,
(isOnChange && eventName === 'onChange') ||
(isOnBlur && eventName === 'onBlur'),
);
};
};
const handleChange = (e: any) => {
const data = commonTask(e && e.target ? e.target : e);
setValue(name, data, isOnChange);
if (onChange) {
onChange(e);
}
};
const handleBlur = (e: any) => {
const data = commonTask(e && e.target ? e.target : e);
setValue(name, data, isOnBlur);
if (onBlur) {
onBlur(e);
}
};
React.useEffect(() => {
register(
Object.defineProperty(
{
name,
},
'value',
{
set(data) {
setInputValue(data);
valueRef.current = data;
},
get() {
return valueRef.current;
},
},
),
{ ...rules },
);
return (): void => {
if (unregister) {
unregister(name as string);
}
};
}, [register, unregister, name]); // eslint-disable-line react-hooks/exhaustive-deps
const props = {
...innerProps,
...(onChangeEvent
? {
[onChangeName || 'onChange']: eventWrapper(onChangeEvent, 'onChange'),
}
: { onChange: handleChange }),
...(isOnBlur
? onBlurEvent
? {
[onBlurName || 'onBlur']: eventWrapper(onBlurEvent, 'onBlur'),
}
: { onBlur: handleBlur }
: {}),
value: inputValue || value || '',
...(isCheckbox ? { checked: inputValue } : {}),
...rest,
};
return React.isValidElement(InnerComponent) ? (
React.cloneElement(InnerComponent, props)
) : (
<InnerComponent {...props} />
);
};
export { RHFInput };