Commit cd328318 authored by tom's avatar tom

change color previews only if hex is correct

parent 36b4be0b
export const COLOR_HEX_REGEXP = /^[A-Fa-f\d]{3,6}$/;
export const validator = (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;
};
...@@ -6,6 +6,7 @@ import type { FormFields, FormFieldTag } from '../types'; ...@@ -6,6 +6,7 @@ import type { FormFields, FormFieldTag } from '../types';
import type { PublicTagType } from 'types/api/addressMetadata'; import type { PublicTagType } from 'types/api/addressMetadata';
import useIsMobile from 'lib/hooks/useIsMobile'; import useIsMobile from 'lib/hooks/useIsMobile';
import { validator as colorValidator } from 'lib/validations/color';
import { validator as urlValidator } from 'lib/validations/url'; import { validator as urlValidator } from 'lib/validations/url';
import EntityTag from 'ui/shared/EntityTags/EntityTag'; import EntityTag from 'ui/shared/EntityTags/EntityTag';
import IconSvg from 'ui/shared/IconSvg'; import IconSvg from 'ui/shared/IconSvg';
...@@ -74,6 +75,7 @@ const PublicTagsSubmitFieldTag = ({ index, isDisabled, register, errors, onAddCl ...@@ -74,6 +75,7 @@ const PublicTagsSubmitFieldTag = ({ index, isDisabled, register, errors, onAddCl
</FormControl> </FormControl>
</GridItem> </GridItem>
<PublicTagsSubmitFieldTagColor <PublicTagsSubmitFieldTagColor
fieldType="bgColor"
fieldName={ `tags.${ index }.bgColor` } fieldName={ `tags.${ index }.bgColor` }
placeholder="Background color" placeholder="Background color"
index={ index } index={ index }
...@@ -82,6 +84,7 @@ const PublicTagsSubmitFieldTag = ({ index, isDisabled, register, errors, onAddCl ...@@ -82,6 +84,7 @@ const PublicTagsSubmitFieldTag = ({ index, isDisabled, register, errors, onAddCl
isDisabled={ isDisabled } isDisabled={ isDisabled }
/> />
<PublicTagsSubmitFieldTagColor <PublicTagsSubmitFieldTagColor
fieldType="textColor"
fieldName={ `tags.${ index }.textColor` } fieldName={ `tags.${ index }.textColor` }
placeholder="Text color" placeholder="Text color"
index={ index } index={ index }
...@@ -141,8 +144,8 @@ const PublicTagsSubmitFieldTag = ({ index, isDisabled, register, errors, onAddCl ...@@ -141,8 +144,8 @@ const PublicTagsSubmitFieldTag = ({ index, isDisabled, register, errors, onAddCl
tagType: field.type.value, tagType: field.type.value,
meta: { meta: {
tagUrl: field.url, tagUrl: field.url,
bgColor: field.bgColor ? `#${ field.bgColor }` : undefined, bgColor: field.bgColor && colorValidator(field.bgColor) === true ? `#${ field.bgColor }` : undefined,
textColor: field.textColor ? `#${ field.textColor }` : undefined, textColor: field.textColor && colorValidator(field.textColor) === true ? `#${ field.textColor }` : undefined,
tooltipDescription: field.tooltipDescription, tooltipDescription: field.tooltipDescription,
}, },
slug: 'new', slug: 'new',
......
...@@ -5,10 +5,14 @@ import { useFormContext, type FieldError, type UseFormRegister } from 'react-hoo ...@@ -5,10 +5,14 @@ import { useFormContext, type FieldError, type UseFormRegister } from 'react-hoo
import type { FormFields } from '../types'; import type { FormFields } from '../types';
import useIsMobile from 'lib/hooks/useIsMobile'; import useIsMobile from 'lib/hooks/useIsMobile';
import { validator as colorValidator } from 'lib/validations/color';
import InputPlaceholder from 'ui/shared/InputPlaceholder'; import InputPlaceholder from 'ui/shared/InputPlaceholder';
interface Props { type ColorFieldTypes = 'bgColor' | 'textColor';
fieldName: `tags.${ number }.${ 'bgColor' | 'textColor' }`;
interface Props<Type extends ColorFieldTypes> {
fieldType: Type;
fieldName: `tags.${ number }.${ Type }`;
index: number; index: number;
isDisabled: boolean; isDisabled: boolean;
register: UseFormRegister<FormFields>; register: UseFormRegister<FormFields>;
...@@ -16,30 +20,15 @@ interface Props { ...@@ -16,30 +20,15 @@ interface Props {
placeholder: string; placeholder: string;
} }
const COLOR_HEX_REGEXP = /^[A-Fa-f\d]{3,6}$/; const PublicTagsSubmitFieldTagColor = <Type extends ColorFieldTypes>({ isDisabled, error, fieldName, placeholder, fieldType }: Props<Type>) => {
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 { getValues, register } = useFormContext<FormFields>();
const inputBgColor = useColorModeValue('white', 'black'); const inputBgColor = useColorModeValue('white', 'black');
const circleBgColorDefault = useColorModeValue('gray.100', 'gray.700'); const circleBgColorDefault = {
bgColor: useColorModeValue('gray.100', 'gray.700'),
textColor: useColorModeValue('blackAlpha.800', 'whiteAlpha.800'),
};
const isMobile = useIsMobile(); const isMobile = useIsMobile();
const field = register(fieldName, { validate, maxLength: 6 }); const field = register(fieldName, { validate: colorValidator, maxLength: 6 });
const value = getValues(fieldName); const value = getValues(fieldName);
return ( return (
...@@ -57,7 +46,7 @@ const PublicTagsSubmitFieldTagColor = ({ isDisabled, error, fieldName, placehold ...@@ -57,7 +46,7 @@ const PublicTagsSubmitFieldTagColor = ({ isDisabled, error, fieldName, placehold
<InputRightElement w="30px" right={ 4 } zIndex={ 10 }> <InputRightElement w="30px" right={ 4 } zIndex={ 10 }>
<Circle <Circle
size="30px" size="30px"
bgColor={ !value ? circleBgColorDefault : `#${ value }` } bgColor={ value && colorValidator(value) === true ? `#${ value }` : circleBgColorDefault[fieldType] }
borderColor="gray.300" borderColor="gray.300"
borderWidth="1px" borderWidth="1px"
/> />
......
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