-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInputText.tsx
More file actions
59 lines (52 loc) · 1.66 KB
/
InputText.tsx
File metadata and controls
59 lines (52 loc) · 1.66 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
import React, { useEffect } from 'react';
import { useInitValidators, useValidateInput } from './InputText.utils';
import BaseFormControl from '@ids-internal/partials/BaseFormControl';
import InputText from '../../inputs/InputText';
import withStateValue from '@ids-internal/hoc/withStateValue';
import { FormControlInputTextProps, ValueType } from './InputText.types';
const FormControlInputText = ({
helperText,
helperTextExtra = {},
id,
input = {},
label,
labelExtra = {},
name,
onChange = () => undefined,
onValidate = () => undefined,
value = '',
}: FormControlInputTextProps) => {
const required = input.required ?? false;
const validators = useInitValidators({ required });
const { isValid, messages } = useValidateInput({ validators, value });
const helperTextProps = {
children: isValid ? helperText : messages.join(', '),
type: isValid ? ('default' as const) : ('error' as const),
...helperTextExtra,
};
const labelProps = {
children: label,
error: !isValid,
htmlFor: id,
required,
...labelExtra,
};
const inputProps = {
...input,
error: !isValid,
id,
name,
onChange,
value,
};
useEffect(() => {
onValidate(isValid, messages);
}, [isValid, messages, onValidate]);
return (
<BaseFormControl helperText={helperTextProps} label={labelProps} type="input-text">
<InputText {...inputProps} />
</BaseFormControl>
);
};
export default FormControlInputText;
export const FormControlInputTextStateful = withStateValue<ValueType>(FormControlInputText);