-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathindex.tsx
62 lines (54 loc) Β· 1.45 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
import React, { FC, useEffect, useState } from 'react';
import { InputGroup } from '@chakra-ui/react';
import { FieldProps, useField } from '@formiz/core';
import { FormGroup, FormGroupProps } from '@/components/FormGroup';
import { ImageUpload, ImageUploadProps } from '@/components/ImageUpload';
export type FieldImageUploadProps = FieldProps &
Omit<FormGroupProps, 'placeholder'> & {
imageUploadProps?: Omit<ImageUploadProps, 'value' | 'onChange'>;
};
export const FieldImageUpload: FC<FieldImageUploadProps> = (props) => {
const {
errorMessage,
id,
isValid,
isSubmitted,
resetKey,
setValue,
value,
otherProps,
} = useField(props);
const { children, label, helper, color, imageUploadProps, ...rest } =
otherProps;
const { required } = props;
const [isTouched, setIsTouched] = useState(false);
const showError = !isValid && (isTouched || isSubmitted);
useEffect(() => {
setIsTouched(false);
}, [resetKey]);
const formGroupProps = {
errorMessage,
helper,
id,
isRequired: !!required,
label,
showError,
...rest,
};
return (
<FormGroup {...formGroupProps}>
<InputGroup>
<ImageUpload
flex="1"
id={id}
value={value ?? ''}
onChange={(e) => setValue(e)}
onBlur={() => setIsTouched(true)}
bgColor={color}
{...imageUploadProps}
/>
</InputGroup>
{children}
</FormGroup>
);
};