Repository navigation
Expand file tree
/
Copy pathuseFormControl.ts
More file actions
226 lines (200 loc) · 6.75 KB
/
Copy pathuseFormControl.ts
File metadata and controls
226 lines (200 loc) · 6.75 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import type { ClerkAPIError } from '@clerk/shared/types';
import type { HTMLInputTypeAttribute } from 'react';
import { useState } from 'react';
import { useDebounce } from '../hooks';
import type { LocalizationKey } from '../localization';
import { useLocalizations } from '../localization';
type SelectOption = { value: string; label?: string };
type Transformer = (value: string) => string;
type Options = {
isRequired?: boolean;
placeholder?: string | LocalizationKey;
options?: SelectOption[];
transformer?: Transformer;
defaultChecked?: boolean;
infoText?: LocalizationKey | string;
} & (
| {
label: string | LocalizationKey;
validatePassword?: never;
buildErrorMessage?: never;
type?: Exclude<HTMLInputTypeAttribute, 'password' | 'radio'>;
radioOptions?: never;
}
| {
label: string | LocalizationKey;
type: Extract<HTMLInputTypeAttribute, 'password'>;
validatePassword: boolean;
buildErrorMessage?: (err: ClerkAPIError[]) => ClerkAPIError | string | undefined;
radioOptions?: never;
}
| {
label: string | LocalizationKey;
type: Extract<HTMLInputTypeAttribute, 'text'>;
validatePassword?: never;
buildErrorMessage?: (err: ClerkAPIError[]) => ClerkAPIError | string | undefined;
radioOptions?: never;
}
| {
validatePassword?: never;
buildErrorMessage?: never;
type: Extract<HTMLInputTypeAttribute, 'radio'>;
label?: string | LocalizationKey;
radioOptions: {
value: string;
label: string | LocalizationKey;
description?: string | LocalizationKey;
}[];
}
);
type FieldStateProps<Id> = {
id: Id;
name: Id;
value: string;
checked?: boolean;
onChange: React.ChangeEventHandler<HTMLInputElement>;
onBlur: React.FocusEventHandler<HTMLInputElement>;
onFocus: React.FocusEventHandler<HTMLInputElement>;
feedback: string;
feedbackType: FeedbackType;
setError: (error: string | ClerkAPIError) => void;
setWarning: (warning: string) => void;
setSuccess: (message: string) => void;
setInfo: (info: string) => void;
setHasPassedComplexity: (b: boolean) => void;
clearFeedback: () => void;
hasPassedComplexity: boolean;
isFocused: boolean;
ignorePasswordManager?: boolean;
} & Omit<Options, 'defaultChecked'>;
export type FormControlState<Id = string> = FieldStateProps<Id> & {
setError: (error: string | ClerkAPIError) => void;
setSuccess: (message: string) => void;
setInfo: (info: string) => void;
setValue: (val: string | undefined) => void;
setChecked: (isChecked: boolean) => void;
clearFeedback: () => void;
props: FieldStateProps<Id>;
};
export type FeedbackType = 'success' | 'error' | 'warning' | 'info';
const emailTransformer = (v: string) => v.trim();
const applyTransformers = (v: string, transformers: Transformer[]) => {
let value = v;
for (let i = 0; i < transformers.length; i++) {
value = transformers[i](value);
}
return value;
};
export const useFormControl = <Id extends string>(
id: Id,
initialState: string,
opts?: Options,
): FormControlState<Id> => {
const options = opts || {
type: 'text',
label: '',
isRequired: false,
placeholder: '',
options: [],
defaultChecked: false,
};
const transformers: Transformer[] = [];
if (options.transformer) {
transformers.push(options.transformer);
}
if (options.type === 'email') {
transformers.push(emailTransformer);
}
const { translateError, t } = useLocalizations();
const [value, setValueInternal] = useState<string>(applyTransformers(initialState, transformers));
const [isFocused, setFocused] = useState(false);
const [checked, setCheckedInternal] = useState<boolean>(options?.defaultChecked || false);
const [hasPassedComplexity, setHasPassedComplexity] = useState(false);
const [feedback, setFeedback] = useState<{ message: string; type: FeedbackType }>({
message: '',
type: 'info',
});
const onChange: FormControlState['onChange'] = event => {
if (options?.type === 'checkbox') {
return setCheckedInternal(event.target.checked);
}
return setValueInternal(applyTransformers(event.target.value || '', transformers));
};
const setValue: FormControlState['setValue'] = val => setValueInternal(val || '');
const setChecked: FormControlState['setChecked'] = checked => setCheckedInternal(checked);
const setError: FormControlState['setError'] = error => {
setFeedback({ message: translateError(error), type: 'error' });
};
const setSuccess: FormControlState['setSuccess'] = message => {
setFeedback({ message, type: 'success' });
};
const setWarning: FormControlState['setWarning'] = warning => {
setFeedback({ message: translateError(warning), type: 'warning' });
};
const setInfo: FormControlState['setInfo'] = info => {
setFeedback({ message: info, type: 'info' });
};
const clearFeedback: FormControlState['clearFeedback'] = () => {
setFeedback({ message: '', type: 'info' });
};
const onFocus: FormControlState['onFocus'] = () => {
setFocused(true);
};
const onBlur: FormControlState['onBlur'] = () => {
setFocused(false);
};
const { defaultChecked, validatePassword: validatePasswordProp, buildErrorMessage, ...restOpts } = options;
const props = {
id,
name: id,
value,
checked,
setSuccess,
setError,
onChange,
onBlur,
onFocus,
setWarning,
feedback: feedback.message || t(options.infoText),
feedbackType: feedback.type,
setInfo,
clearFeedback,
hasPassedComplexity,
setHasPassedComplexity,
validatePassword: options.type === 'password' ? options.validatePassword : undefined,
isFocused,
...restOpts,
};
return { props, ...props, buildErrorMessage, setError, setValue, setChecked };
};
type FormControlStateLike = Pick<FormControlState, 'id' | 'value' | 'checked' | 'type'>;
export const buildRequest = (fieldStates: Array<FormControlStateLike>): Record<string, any> => {
const request: { [x: string]: any } = {};
fieldStates.forEach(x => {
request[x.id] = x.type !== 'checkbox' ? x.value : x.checked;
});
return request;
};
type DebouncedFeedback = {
debounced: {
feedback: string;
feedbackType: FeedbackType;
};
};
type DebouncingOption = {
feedback?: string;
feedbackType?: FeedbackType;
isFocused?: boolean;
delayInMs?: number;
};
export const useFormControlFeedback = (opts?: DebouncingOption): DebouncedFeedback => {
const { feedback = '', delayInMs = 100, feedbackType = 'info', isFocused = false } = opts || {};
const shouldHide = isFocused ? false : ['info', 'warning'].includes(feedbackType);
const debouncedState = useDebounce(
{ feedback: shouldHide ? '' : feedback, feedbackType: shouldHide ? 'info' : feedbackType },
delayInMs,
);
return {
debounced: debouncedState,
};
};