Commit 7b5101df authored by tom's avatar tom

address type validation

parent e4c382c8
...@@ -80,6 +80,7 @@ const ContractMethodCallable = <T extends SmartContractMethod>({ data, onSubmit, ...@@ -80,6 +80,7 @@ const ContractMethodCallable = <T extends SmartContractMethod>({ data, onSubmit,
const { control, handleSubmit, setValue, getValues } = useForm<MethodFormFields>({ const { control, handleSubmit, setValue, getValues } = useForm<MethodFormFields>({
defaultValues: _fromPairs(inputs.map(({ name }, index) => [ getFieldName(name, index), '' ])), defaultValues: _fromPairs(inputs.map(({ name }, index) => [ getFieldName(name, index), '' ])),
mode: 'onBlur',
}); });
const handleTxSettle = React.useCallback(() => { const handleTxSettle = React.useCallback(() => {
......
...@@ -6,8 +6,9 @@ import { ...@@ -6,8 +6,9 @@ import {
InputRightElement, InputRightElement,
} from '@chakra-ui/react'; } from '@chakra-ui/react';
import React from 'react'; import React from 'react';
import type { Control, ControllerRenderProps, UseFormGetValues, UseFormSetValue } from 'react-hook-form'; import type { Control, ControllerRenderProps, UseFormGetValues, UseFormSetValue, UseFormStateReturn } from 'react-hook-form';
import { Controller } from 'react-hook-form'; import { Controller } from 'react-hook-form';
import { isAddress } from 'viem';
import type { MethodFormFields } from './types'; import type { MethodFormFields } from './types';
import type { SmartContractMethodArgType } from 'types/api/contract'; import type { SmartContractMethodArgType } from 'types/api/contract';
...@@ -47,31 +48,51 @@ const ContractMethodField = ({ control, name, valueType, placeholder, setValue, ...@@ -47,31 +48,51 @@ const ContractMethodField = ({ control, name, valueType, placeholder, setValue,
const hasZerosControl = addZeroesAllowed(valueType); const hasZerosControl = addZeroesAllowed(valueType);
const renderInput = React.useCallback(({ field }: { field: ControllerRenderProps<MethodFormFields> }) => { const renderInput = React.useCallback((
{ field, formState }: { field: ControllerRenderProps<MethodFormFields>; formState: UseFormStateReturn<MethodFormFields> },
) => {
const error = formState.errors[name];
return ( return (
<FormControl <Box>
id={ name } <FormControl
w="100%" id={ name }
mb={{ base: 1, lg: 0 }} w="100%"
isDisabled={ isDisabled } mb={{ base: 1, lg: 0 }}
> isDisabled={ isDisabled }
<InputGroup size="xs"> >
<Input <InputGroup size="xs">
{ ...field } <Input
ref={ ref } { ...field }
placeholder={ placeholder } ref={ ref }
paddingRight={ hasZerosControl ? '120px' : '40px' } isInvalid={ Boolean(error) }
autoComplete="off" required
/> placeholder={ placeholder }
<InputRightElement w="auto" right={ 1 }> paddingRight={ hasZerosControl ? '120px' : '40px' }
{ field.value && <ClearButton onClick={ handleClear } isDisabled={ isDisabled }/> } autoComplete="off"
{ hasZerosControl && <ContractMethodFieldZeroes onClick={ handleAddZeroesClick } isDisabled={ isDisabled }/> } />
</InputRightElement> <InputRightElement w="auto" right={ 1 }>
</InputGroup> { field.value && <ClearButton onClick={ handleClear } isDisabled={ isDisabled }/> }
</FormControl> { hasZerosControl && <ContractMethodFieldZeroes onClick={ handleAddZeroesClick } isDisabled={ isDisabled }/> }
</InputRightElement>
</InputGroup>
</FormControl>
{ error && <Box color="error" fontSize="sm" mt={ 1 }>{ error.message }</Box> }
</Box>
); );
}, [ name, isDisabled, placeholder, hasZerosControl, handleClear, handleAddZeroesClick ]); }, [ name, isDisabled, placeholder, hasZerosControl, handleClear, handleAddZeroesClick ]);
const validate = React.useCallback((value: string) => {
switch (valueType) {
case 'address': {
return !isAddress(value) ? 'Invalid address format' : true;
}
default:
return;
}
}, [ valueType ]);
return ( return (
<> <>
<Box <Box
...@@ -86,6 +107,7 @@ const ContractMethodField = ({ control, name, valueType, placeholder, setValue, ...@@ -86,6 +107,7 @@ const ContractMethodField = ({ control, name, valueType, placeholder, setValue,
name={ name } name={ name }
control={ control } control={ control }
render={ renderInput } render={ renderInput }
rules={{ required: 'Field is required', validate }}
/> />
</> </>
); );
......
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