Commit 26ef668d authored by tom's avatar tom

color preview

parent a2263142
...@@ -72,19 +72,19 @@ const PublicTagsSubmitFieldTag = ({ index, isDisabled, register, errors, onAddCl ...@@ -72,19 +72,19 @@ const PublicTagsSubmitFieldTag = ({ index, isDisabled, register, errors, onAddCl
</FormControl> </FormControl>
</GridItem> </GridItem>
<PublicTagsSubmitFieldTagColor <PublicTagsSubmitFieldTagColor
fieldName="bgColor" fieldName={ `tags.${ index }.bgColor` }
placeholder="Background color" placeholder="Background color"
index={ index } index={ index }
register={ register } register={ register }
errors={ errors } error={ errors?.bgColor }
isDisabled={ isDisabled } isDisabled={ isDisabled }
/> />
<PublicTagsSubmitFieldTagColor <PublicTagsSubmitFieldTagColor
fieldName="textColor" fieldName={ `tags.${ index }.textColor` }
placeholder="Text color" placeholder="Text color"
index={ index } index={ index }
register={ register } register={ register }
errors={ errors } error={ errors?.textColor }
isDisabled={ isDisabled } isDisabled={ isDisabled }
/> />
<GridItem colSpan={{ base: 1, lg: 4 }}> <GridItem colSpan={{ base: 1, lg: 4 }}>
......
import { FormControl, Input, useColorModeValue } from '@chakra-ui/react'; import { Circle, FormControl, Input, InputGroup, InputRightElement, useColorModeValue } from '@chakra-ui/react';
import React from 'react'; import React from 'react';
import type { FieldError, FieldErrorsImpl, Merge, UseFormRegister } from 'react-hook-form'; import { useFormContext, type FieldError, type UseFormRegister } from 'react-hook-form';
import type { FormFieldTag, FormFields } from '../types'; import type { FormFields } from '../types';
import useIsMobile from 'lib/hooks/useIsMobile';
import InputPlaceholder from 'ui/shared/InputPlaceholder'; import InputPlaceholder from 'ui/shared/InputPlaceholder';
interface Props { interface Props {
fieldName: 'textColor' | 'bgColor'; fieldName: `tags.${ number }.${ 'bgColor' | 'textColor' }`;
index: number; index: number;
isDisabled: boolean; isDisabled: boolean;
register: UseFormRegister<FormFields>; register: UseFormRegister<FormFields>;
errors: Merge<FieldError, FieldErrorsImpl<FormFieldTag>> | undefined; error: FieldError | undefined;
placeholder: string; placeholder: string;
} }
const PublicTagsSubmitFieldTagColor = ({ index, isDisabled, register, errors, fieldName, placeholder }: Props) => { const COLOR_HEX_REGEXP = /^[A-Fa-f\d]{3,6}$/;
const validate = (value: string | undefined) => {
if (!value || value.length === 0) {
return true;
}
if (value.length !== 3 && value.length !== 6) {
return 'Invalid length';
}
if (!COLOR_HEX_REGEXP.test(value)) {
return 'Invalid hex code';
}
return true;
};
const PublicTagsSubmitFieldTagColor = ({ isDisabled, error, fieldName, placeholder }: Props) => {
const { getValues, register } = useFormContext<FormFields>();
const inputBgColor = useColorModeValue('white', 'black'); const inputBgColor = useColorModeValue('white', 'black');
const circleBgColorDefault = useColorModeValue('gray.100', 'gray.700');
const isMobile = useIsMobile();
const field = register(fieldName, { validate, maxLength: 6 });
const value = getValues(fieldName);
return ( return (
<FormControl variant="floating" size={{ base: 'md', lg: 'lg' }}> <FormControl variant="floating" size={{ base: 'md', lg: 'lg' }}>
<InputGroup size={ isMobile ? 'md' : 'lg' }>
<Input <Input
{ ...register(`tags.${ index }.${ fieldName }`) } { ...field }
isInvalid={ Boolean(errors?.[fieldName]) } isInvalid={ Boolean(error) }
isDisabled={ isDisabled } isDisabled={ isDisabled }
autoComplete="off" autoComplete="off"
bgColor={ inputBgColor } bgColor={ inputBgColor }
maxLength={ 6 }
/>
<InputPlaceholder text={ placeholder } error={ error }/>
<InputRightElement w="30px" right={ 4 } zIndex={ 10 }>
<Circle
size="30px"
bgColor={ !value ? circleBgColorDefault : `#${ value }` }
borderColor="gray.300"
borderWidth="1px"
/> />
<InputPlaceholder text={ placeholder } error={ errors?.[fieldName] }/> </InputRightElement>
</InputGroup>
</FormControl> </FormControl>
); );
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment