-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathuseInput.ts
142 lines (128 loc) · 4.17 KB
/
useInput.ts
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
import { useCallback, ChangeEvent, FocusEvent, useEffect } from 'react';
import {
useField as useFinalFormField,
FieldProps,
FieldRenderProps,
FieldInputProps,
} from 'react-final-form';
import get from 'lodash/get';
import { Validator, composeValidators } from './validate';
import isRequired from './isRequired';
import { useFormGroupContext } from './useFormGroupContext';
import { useFormContext } from './useFormContext';
import { useRecordContext } from '../controller';
export interface InputProps<T = any>
extends Omit<
FieldProps<any, FieldRenderProps<any, HTMLElement>, HTMLElement>,
'validate' | 'children'
> {
defaultValue?: any;
id?: string;
input?: FieldInputProps<any, HTMLElement>;
meta?: any;
name?: string;
onBlur?: (event: FocusEvent<T>) => void;
onChange?: (event: ChangeEvent | any) => void;
onFocus?: (event: FocusEvent<T>) => void;
options?: T;
resource?: string;
source: string;
validate?: Validator | Validator[];
isRequired?: boolean;
}
export interface UseInputValue extends FieldRenderProps<any, HTMLElement> {
id: string;
isRequired: boolean;
}
const useInput = ({
defaultValue,
initialValue,
id,
name,
source,
validate,
onBlur: customOnBlur,
onChange: customOnChange,
onFocus: customOnFocus,
isRequired: isRequiredOption,
...options
}: InputProps): UseInputValue => {
const finalName = name || source;
const formGroupName = useFormGroupContext();
const formContext = useFormContext();
const record = useRecordContext();
useEffect(() => {
if (!formContext || !formGroupName) {
return;
}
formContext.registerField(source, formGroupName);
return () => {
formContext.unregisterField(source, formGroupName);
};
}, [formContext, formGroupName, source]);
const sanitizedValidate = Array.isArray(validate)
? composeValidators(validate)
: validate;
// Fetch the initialValue from the record if available or apply the provided initialValue.
// This ensure dynamically added inputs have their value set correctly (ArrayInput for example).
// We don't do this for the form level initialValues so that it works as it should in final-form
// (ie. field level initialValue override form level initialValues for this field).
const { input, meta } = useFinalFormField(finalName, {
initialValue: get(record, source, initialValue),
defaultValue,
validate: sanitizedValidate,
...options,
});
// Extract the event handlers so that we can provide ours
// allowing users to provide theirs without breaking the form
const { onBlur, onChange, onFocus, ...inputProps } = input;
const handleBlur = useCallback(
event => {
onBlur(event);
if (typeof customOnBlur === 'function') {
customOnBlur(event);
}
},
[onBlur, customOnBlur]
);
const handleChange = useCallback(
event => {
onChange(event);
if (typeof customOnChange === 'function') {
customOnChange(event);
}
},
[onChange, customOnChange]
);
const handleFocus = useCallback(
event => {
onFocus(event);
if (typeof customOnFocus === 'function') {
customOnFocus(event);
}
},
[onFocus, customOnFocus]
);
// If there is an input prop, this input has already been enhanced by final-form
// This is required in for inputs used inside other inputs (such as the SelectInput inside a ReferenceInput)
if (options.input) {
return {
id: id || source,
input: options.input,
meta: options.meta,
isRequired: isRequiredOption || isRequired(validate),
};
}
return {
id: id || source,
input: {
...inputProps,
onBlur: handleBlur,
onChange: handleChange,
onFocus: handleFocus,
},
meta,
isRequired: isRequiredOption || isRequired(validate),
};
};
export default useInput;