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,8 +48,13 @@ const ContractMethodField = ({ control, name, valueType, placeholder, setValue, ...@@ -47,8 +48,13 @@ 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 (
<Box>
<FormControl <FormControl
id={ name } id={ name }
w="100%" w="100%"
...@@ -59,6 +65,8 @@ const ContractMethodField = ({ control, name, valueType, placeholder, setValue, ...@@ -59,6 +65,8 @@ const ContractMethodField = ({ control, name, valueType, placeholder, setValue,
<Input <Input
{ ...field } { ...field }
ref={ ref } ref={ ref }
isInvalid={ Boolean(error) }
required
placeholder={ placeholder } placeholder={ placeholder }
paddingRight={ hasZerosControl ? '120px' : '40px' } paddingRight={ hasZerosControl ? '120px' : '40px' }
autoComplete="off" autoComplete="off"
...@@ -69,9 +77,22 @@ const ContractMethodField = ({ control, name, valueType, placeholder, setValue, ...@@ -69,9 +77,22 @@ const ContractMethodField = ({ control, name, valueType, placeholder, setValue,
</InputRightElement> </InputRightElement>
</InputGroup> </InputGroup>
</FormControl> </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