forked from deriv-com/p2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdFormTextArea.tsx
66 lines (63 loc) · 2.04 KB
/
AdFormTextArea.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
import { ChangeEvent } from 'react';
import clsx from 'clsx';
import { Controller, useFormContext } from 'react-hook-form';
import { VALID_SYMBOLS_PATTERN } from '@/constants';
import { getTextFieldError } from '@/utils';
import { useTranslations } from '@deriv-com/translations';
import { TextArea } from '@deriv-com/ui';
import './AdFormTextArea.scss';
export type TChangeEvent = (e: ChangeEvent<HTMLTextAreaElement>) => void;
type TAdFormTextAreaProps = {
className?: string;
field: string;
hint?: string;
label: string;
name: string;
onFieldChange?: TChangeEvent;
required?: boolean;
};
const AdFormTextArea = ({
className,
field,
hint = '',
label,
name,
onFieldChange,
required = false,
}: TAdFormTextAreaProps) => {
const { control } = useFormContext();
const { localize } = useTranslations();
return (
<Controller
control={control}
name={name}
render={({ field: { onBlur, onChange, value }, fieldState: { error } }) => (
<div className={clsx('mb-[2.4rem] ad-form-textarea', className)}>
<TextArea
className={className}
hint={error ? error.message : hint}
isInvalid={!!error}
label={label}
maxLength={300}
onBlur={onBlur}
onChange={e => {
onChange(e);
onFieldChange?.(e);
}}
shouldShowCounter
textSize='sm'
value={value}
/>
</div>
)}
rules={{
pattern: {
message: getTextFieldError(field),
value: VALID_SYMBOLS_PATTERN,
},
required: required ? localize('{{field}} is required', { field }) : undefined,
}}
/>
);
};
export default AdFormTextArea;