|
| 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 | +}; |
0 commit comments