Skip to content

Commit

Permalink
feat: FieldImageUpload
Browse files Browse the repository at this point in the history
  • Loading branch information
Rileran authored and DecampsRenan committed Dec 23, 2022
1 parent f00379f commit 5bca58d
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
110 changes: 110 additions & 0 deletions src/components/FieldImageUpload/docs.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { Box, Button, Center, Stack, Tag, Text } from '@chakra-ui/react';
import { Formiz, useForm } from '@formiz/core';
import { FiImage } from 'react-icons/fi';

import { FieldImageUpload } from '@/components/FieldImageUpload';
import { Icon } from '@/components/Icons';

export default {
title: 'Fields/FieldImageUpload',
};

export const Default = () => (
<Formiz onSubmit={console.log} autoForm>
<Stack spacing={6}>
<FieldImageUpload
name="demo"
label="Profil picture"
helper="This is an helper"
required="Profil picture is required"
width="360px"
/>
<FieldImageUpload
name="demo-default-value"
label="Default value"
defaultValue="https://bit.ly/dan-abramov"
width="360px"
/>
<FieldImageUpload
name="demo-ratio"
label="Custom aspect ratio and size"
imageUploadProps={{ ratio: 1 }}
w="240px"
/>
<Box>
<Button type="submit">Submit</Button>
</Box>
</Stack>
</Formiz>
);

export const CustomPlaceholder = () => {
const PlaceholderComponent = () => (
<Center bgColor="gray.50" overflow="hidden">
<Stack textAlign="center" spacing={2}>
<Icon
fontSize="48px"
textColor="gray.400"
icon={FiImage}
alignSelf="center"
/>
<Text textColor="gray.600" fontWeight="medium" fontSize="md">
Upload a photo
</Text>
</Stack>
</Center>
);

return (
<Formiz onSubmit={console.log} autoForm>
<Stack spacing={6}>
<FieldImageUpload
name="demo"
label="Image with placeholder"
imageUploadProps={{ placeholder: <PlaceholderComponent /> }}
w="480px"
/>
<Box>
<Button type="submit">Submit</Button>
</Box>
</Stack>
</Formiz>
);
};

export const InvalidateFormWhileUploading = () => {
const form = useForm();

return (
<Formiz connect={form} onSubmit={console.log} autoForm>
<Stack spacing={2}>
<FieldImageUpload
name="demo"
label="Invalidate during uploading"
w="360px"
imageUploadProps={{
onUploadStateChange: (isUploading) =>
isUploading &&
form.invalidateFields({ demo: 'Image is uploading' }),
}}
/>
<Box>
{form.isValid ? (
<Tag size="lg" colorScheme="green">
Valid
</Tag>
) : (
<Tag size="lg" colorScheme="red">
Invalid
</Tag>
)}
</Box>
<Box>
<Button type="submit" isDisabled={!form.isValid && form.isSubmitted}>
Submit
</Button>
</Box>
</Stack>
</Formiz>
);
};
72 changes: 72 additions & 0 deletions src/components/FieldImageUpload/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React, { 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 interface FieldImageUploadProps
extends FieldProps,
Omit<FormGroupProps, 'placeholder'> {
imageUploadProps?: Omit<ImageUploadProps, 'value' | 'onChange'>;
}

export const FieldImageUpload = (props: FieldImageUploadProps) => {
const {
errorMessage,
id,
isValid,
isSubmitted,
resetKey,
setValue,
value,
otherProps,
} = useField(props);
const {
children,
label,
type,
placeholder,
helper,
leftIcon,
rightIcon,
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>
);
};

0 comments on commit 5bca58d

Please sign in to comment.