Skip to content

Commit 5bca58d

Browse files
RileranDecampsRenan
authored andcommitted
feat: FieldImageUpload
1 parent f00379f commit 5bca58d

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { Box, Button, Center, Stack, Tag, Text } from '@chakra-ui/react';
2+
import { Formiz, useForm } from '@formiz/core';
3+
import { FiImage } from 'react-icons/fi';
4+
5+
import { FieldImageUpload } from '@/components/FieldImageUpload';
6+
import { Icon } from '@/components/Icons';
7+
8+
export default {
9+
title: 'Fields/FieldImageUpload',
10+
};
11+
12+
export const Default = () => (
13+
<Formiz onSubmit={console.log} autoForm>
14+
<Stack spacing={6}>
15+
<FieldImageUpload
16+
name="demo"
17+
label="Profil picture"
18+
helper="This is an helper"
19+
required="Profil picture is required"
20+
width="360px"
21+
/>
22+
<FieldImageUpload
23+
name="demo-default-value"
24+
label="Default value"
25+
defaultValue="https://bit.ly/dan-abramov"
26+
width="360px"
27+
/>
28+
<FieldImageUpload
29+
name="demo-ratio"
30+
label="Custom aspect ratio and size"
31+
imageUploadProps={{ ratio: 1 }}
32+
w="240px"
33+
/>
34+
<Box>
35+
<Button type="submit">Submit</Button>
36+
</Box>
37+
</Stack>
38+
</Formiz>
39+
);
40+
41+
export const CustomPlaceholder = () => {
42+
const PlaceholderComponent = () => (
43+
<Center bgColor="gray.50" overflow="hidden">
44+
<Stack textAlign="center" spacing={2}>
45+
<Icon
46+
fontSize="48px"
47+
textColor="gray.400"
48+
icon={FiImage}
49+
alignSelf="center"
50+
/>
51+
<Text textColor="gray.600" fontWeight="medium" fontSize="md">
52+
Upload a photo
53+
</Text>
54+
</Stack>
55+
</Center>
56+
);
57+
58+
return (
59+
<Formiz onSubmit={console.log} autoForm>
60+
<Stack spacing={6}>
61+
<FieldImageUpload
62+
name="demo"
63+
label="Image with placeholder"
64+
imageUploadProps={{ placeholder: <PlaceholderComponent /> }}
65+
w="480px"
66+
/>
67+
<Box>
68+
<Button type="submit">Submit</Button>
69+
</Box>
70+
</Stack>
71+
</Formiz>
72+
);
73+
};
74+
75+
export const InvalidateFormWhileUploading = () => {
76+
const form = useForm();
77+
78+
return (
79+
<Formiz connect={form} onSubmit={console.log} autoForm>
80+
<Stack spacing={2}>
81+
<FieldImageUpload
82+
name="demo"
83+
label="Invalidate during uploading"
84+
w="360px"
85+
imageUploadProps={{
86+
onUploadStateChange: (isUploading) =>
87+
isUploading &&
88+
form.invalidateFields({ demo: 'Image is uploading' }),
89+
}}
90+
/>
91+
<Box>
92+
{form.isValid ? (
93+
<Tag size="lg" colorScheme="green">
94+
Valid
95+
</Tag>
96+
) : (
97+
<Tag size="lg" colorScheme="red">
98+
Invalid
99+
</Tag>
100+
)}
101+
</Box>
102+
<Box>
103+
<Button type="submit" isDisabled={!form.isValid && form.isSubmitted}>
104+
Submit
105+
</Button>
106+
</Box>
107+
</Stack>
108+
</Formiz>
109+
);
110+
};
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import React, { useEffect, useState } from 'react';
2+
3+
import { InputGroup } from '@chakra-ui/react';
4+
import { FieldProps, useField } from '@formiz/core';
5+
6+
import { FormGroup, FormGroupProps } from '@/components/FormGroup';
7+
import { ImageUpload, ImageUploadProps } from '@/components/ImageUpload';
8+
9+
export interface FieldImageUploadProps
10+
extends FieldProps,
11+
Omit<FormGroupProps, 'placeholder'> {
12+
imageUploadProps?: Omit<ImageUploadProps, 'value' | 'onChange'>;
13+
}
14+
15+
export const FieldImageUpload = (props: FieldImageUploadProps) => {
16+
const {
17+
errorMessage,
18+
id,
19+
isValid,
20+
isSubmitted,
21+
resetKey,
22+
setValue,
23+
value,
24+
otherProps,
25+
} = useField(props);
26+
const {
27+
children,
28+
label,
29+
type,
30+
placeholder,
31+
helper,
32+
leftIcon,
33+
rightIcon,
34+
color,
35+
imageUploadProps,
36+
...rest
37+
} = otherProps;
38+
const { required } = props;
39+
const [isTouched, setIsTouched] = useState(false);
40+
const showError = !isValid && (isTouched || isSubmitted);
41+
42+
useEffect(() => {
43+
setIsTouched(false);
44+
}, [resetKey]);
45+
46+
const formGroupProps = {
47+
errorMessage,
48+
helper,
49+
id,
50+
isRequired: !!required,
51+
label,
52+
showError,
53+
...rest,
54+
};
55+
56+
return (
57+
<FormGroup {...formGroupProps}>
58+
<InputGroup>
59+
<ImageUpload
60+
flex="1"
61+
id={id}
62+
value={value ?? ''}
63+
onChange={(e) => setValue(e)}
64+
onBlur={() => setIsTouched(true)}
65+
bgColor={color}
66+
{...imageUploadProps}
67+
/>
68+
</InputGroup>
69+
{children}
70+
</FormGroup>
71+
);
72+
};

0 commit comments

Comments
 (0)