Commit 36b4be0b authored by tom's avatar tom

add icon to tag type control

parent a33a8d54
import { FormControl, useColorModeValue } from '@chakra-ui/react'; import { chakra, Flex, FormControl, useColorModeValue } from '@chakra-ui/react';
import type { GroupBase, SelectComponentsConfig, SingleValueProps } from 'chakra-react-select';
import { chakraComponents } from 'chakra-react-select';
import React from 'react'; import React from 'react';
import type { ControllerRenderProps } from 'react-hook-form'; import type { ControllerRenderProps } from 'react-hook-form';
import { Controller, useFormContext } from 'react-hook-form'; import { Controller, useFormContext } from 'react-hook-form';
import type { FormFields } from '../types'; import type { FormFields } from '../types';
import type { PublicTagType } from 'types/api/addressMetadata'; import type { PublicTagType } from 'types/api/addressMetadata';
import type { Option } from 'ui/shared/FancySelect/types';
import useIsMobile from 'lib/hooks/useIsMobile'; import useIsMobile from 'lib/hooks/useIsMobile';
import FancySelect from 'ui/shared/FancySelect/FancySelect'; import FancySelect from 'ui/shared/FancySelect/FancySelect';
import IconSvg from 'ui/shared/IconSvg';
interface Props { interface Props {
index: number; index: number;
...@@ -17,7 +21,7 @@ interface Props { ...@@ -17,7 +21,7 @@ interface Props {
const PublicTagsSubmitFieldTagType = ({ index, tagTypes, isDisabled }: Props) => { const PublicTagsSubmitFieldTagType = ({ index, tagTypes, isDisabled }: Props) => {
const isMobile = useIsMobile(); const isMobile = useIsMobile();
const { control } = useFormContext<FormFields>(); const { control, watch } = useFormContext<FormFields>();
const inputBgColor = useColorModeValue('white', 'black'); const inputBgColor = useColorModeValue('white', 'black');
const typeOptions = React.useMemo(() => tagTypes?.map((type) => ({ const typeOptions = React.useMemo(() => tagTypes?.map((type) => ({
...@@ -25,6 +29,36 @@ const PublicTagsSubmitFieldTagType = ({ index, tagTypes, isDisabled }: Props) => ...@@ -25,6 +29,36 @@ const PublicTagsSubmitFieldTagType = ({ index, tagTypes, isDisabled }: Props) =>
label: type.type, label: type.type,
})), [ tagTypes ]); })), [ tagTypes ]);
const fieldValue = watch(`tags.${ index }.type`).value;
const selectComponents: SelectComponentsConfig<Option, boolean, GroupBase<Option>> = React.useMemo(() => {
type SingleValueComponentProps = SingleValueProps<Option, boolean, GroupBase<Option>> & { children: React.ReactNode }
const SingleValue = ({ children, ...props }: SingleValueComponentProps) => {
switch (fieldValue) {
case 'name': {
return (
<chakraComponents.SingleValue { ...props }>
<Flex alignItems="center" columnGap={ 1 }>
<IconSvg name="publictags_slim" boxSize={ 4 } flexShrink={ 0 } color="gray.400"/>
{ children }
</Flex>
</chakraComponents.SingleValue>
);
}
case 'protocol':
case 'generic': {
return (<chakraComponents.SingleValue { ...props }><chakra.span color="gray.400">#</chakra.span> { children }</chakraComponents.SingleValue>);
}
default: {
return (<chakraComponents.SingleValue { ...props }>{ children }</chakraComponents.SingleValue>);
}
}
};
return { SingleValue };
}, [ fieldValue ]);
const renderControl = React.useCallback(({ field }: { field: ControllerRenderProps<FormFields, `tags.${ number }.type`> }) => { const renderControl = React.useCallback(({ field }: { field: ControllerRenderProps<FormFields, `tags.${ number }.type`> }) => {
return ( return (
<FormControl variant="floating" id={ field.name } isRequired size={{ base: 'md', lg: 'lg' }}> <FormControl variant="floating" id={ field.name } isRequired size={{ base: 'md', lg: 'lg' }}>
...@@ -36,6 +70,8 @@ const PublicTagsSubmitFieldTagType = ({ index, tagTypes, isDisabled }: Props) => ...@@ -36,6 +70,8 @@ const PublicTagsSubmitFieldTagType = ({ index, tagTypes, isDisabled }: Props) =>
isDisabled={ isDisabled } isDisabled={ isDisabled }
isRequired isRequired
isAsync={ false } isAsync={ false }
isSearchable={ false }
components={ selectComponents }
formControlStyles={{ formControlStyles={{
bgColor: inputBgColor, bgColor: inputBgColor,
borderRadius: 'base', borderRadius: 'base',
...@@ -43,7 +79,7 @@ const PublicTagsSubmitFieldTagType = ({ index, tagTypes, isDisabled }: Props) => ...@@ -43,7 +79,7 @@ const PublicTagsSubmitFieldTagType = ({ index, tagTypes, isDisabled }: Props) =>
/> />
</FormControl> </FormControl>
); );
}, [ inputBgColor, isDisabled, isMobile, typeOptions ]); }, [ inputBgColor, isDisabled, isMobile, selectComponents, typeOptions ]);
return ( return (
<Controller <Controller
......
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