Commit b9ea29b6 authored by tom's avatar tom

refactor addresses

parent bb6af2d8
import { FormControl, GridItem, IconButton, Input } from '@chakra-ui/react'; import { FormControl, GridItem, IconButton, Input } from '@chakra-ui/react';
import React from 'react'; import React from 'react';
import type { ControllerFieldState, ControllerRenderProps, UseFormStateReturn } from 'react-hook-form'; import { useFieldArray, useFormContext } from 'react-hook-form';
import { Controller, useFieldArray, useFormContext } from 'react-hook-form';
import type { FormFields } from '../types'; import type { FormFields } from '../types';
...@@ -12,7 +11,7 @@ import InputPlaceholder from 'ui/shared/InputPlaceholder'; ...@@ -12,7 +11,7 @@ import InputPlaceholder from 'ui/shared/InputPlaceholder';
const LIMIT = 10; const LIMIT = 10;
const PublicTagsSubmitFieldAddresses = () => { const PublicTagsSubmitFieldAddresses = () => {
const { control, formState } = useFormContext<FormFields>(); const { control, formState, register } = useFormContext<FormFields>();
const { fields, insert, remove } = useFieldArray<FormFields, 'addresses'>({ const { fields, insert, remove } = useFieldArray<FormFields, 'addresses'>({
name: 'addresses', name: 'addresses',
control, control,
...@@ -34,37 +33,23 @@ const PublicTagsSubmitFieldAddresses = () => { ...@@ -34,37 +33,23 @@ const PublicTagsSubmitFieldAddresses = () => {
} }
}, [ remove ]); }, [ remove ]);
const renderControl = React.useCallback(({ field, formState, fieldState }: {
field: ControllerRenderProps<FormFields, `addresses.${ number }.hash`>;
fieldState: ControllerFieldState;
formState: UseFormStateReturn<FormFields>;
}) => {
return (
<FormControl variant="floating" id={ field.name } isRequired size={{ base: 'md', lg: 'lg' }}>
<Input
{ ...field }
isInvalid={ Boolean(fieldState.error) }
isDisabled={ formState.isSubmitting }
required
autoComplete="off"
/>
<InputPlaceholder text="Smart contract / Address (0x...)" error={ fieldState.error }/>
</FormControl>
);
}, []);
return ( return (
<> <>
{ fields.map((field, index) => { { fields.map((field, index) => {
const error = formState.errors?.addresses?.[ index ]?.hash;
return ( return (
<React.Fragment key={ field.id }> <React.Fragment key={ field.id }>
<GridItem colSpan={{ base: 1, lg: 2 }}> <GridItem colSpan={{ base: 1, lg: 2 }}>
<Controller <FormControl variant="floating" isRequired size={{ base: 'md', lg: 'lg' }}>
name={ `addresses.${ index }.hash` } <Input
control={ control } { ...register(`addresses.${ index }.hash`, { required: true, pattern: ADDRESS_REGEXP }) }
render={ renderControl } isInvalid={ Boolean(error) }
rules={{ required: true, pattern: ADDRESS_REGEXP }} isDisabled={ formState.isSubmitting }
/> autoComplete="off"
/>
<InputPlaceholder text="Smart contract / Address (0x...)" error={ error }/>
</FormControl>
</GridItem> </GridItem>
<GridItem display="flex" alignItems="center" columnGap={ 5 }> <GridItem display="flex" alignItems="center" columnGap={ 5 }>
{ fields.length < LIMIT && !(fields.length > 1 && index === 0) && ( { fields.length < LIMIT && !(fields.length > 1 && index === 0) && (
......
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