Commit d19fcbdb authored by tom's avatar tom

handle button click

parent d966aeca
...@@ -68,7 +68,7 @@ const ContractMethodCallable = <T extends SmartContractMethod>({ data, onSubmit, ...@@ -68,7 +68,7 @@ const ContractMethodCallable = <T extends SmartContractMethod>({ data, onSubmit,
]; ];
}, [ data ]); }, [ data ]);
const { control, handleSubmit, setValue } = 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), '' ])),
}); });
...@@ -122,8 +122,9 @@ const ContractMethodCallable = <T extends SmartContractMethod>({ data, onSubmit, ...@@ -122,8 +122,9 @@ const ContractMethodCallable = <T extends SmartContractMethod>({ data, onSubmit,
placeholder={ `${ name }(${ type })` } placeholder={ `${ name }(${ type })` }
control={ control } control={ control }
setValue={ setValue } setValue={ setValue }
getValues={ getValues }
isDisabled={ isLoading } isDisabled={ isLoading }
onClear={ handleFormChange } onChange={ handleFormChange }
/> />
); );
}) } }) }
......
...@@ -5,7 +5,7 @@ import { ...@@ -5,7 +5,7 @@ import {
InputRightElement, InputRightElement,
} from '@chakra-ui/react'; } from '@chakra-ui/react';
import React from 'react'; import React from 'react';
import type { Control, ControllerRenderProps, UseFormSetValue } from 'react-hook-form'; import type { Control, ControllerRenderProps, UseFormGetValues, UseFormSetValue } from 'react-hook-form';
import { Controller } from 'react-hook-form'; import { Controller } from 'react-hook-form';
import type { MethodFormFields } from './types'; import type { MethodFormFields } from './types';
...@@ -18,21 +18,30 @@ import ContractMethodFieldZeroes from './ContractMethodFieldZeroes'; ...@@ -18,21 +18,30 @@ import ContractMethodFieldZeroes from './ContractMethodFieldZeroes';
interface Props { interface Props {
control: Control<MethodFormFields>; control: Control<MethodFormFields>;
setValue: UseFormSetValue<MethodFormFields>; setValue: UseFormSetValue<MethodFormFields>;
getValues: UseFormGetValues<MethodFormFields>;
placeholder: string; placeholder: string;
name: string; name: string;
valueType: SmartContractMethodArgType; valueType: SmartContractMethodArgType;
isDisabled: boolean; isDisabled: boolean;
onClear: () => void; onChange: () => void;
} }
const ContractMethodField = ({ control, name, valueType, placeholder, setValue, isDisabled, onClear }: Props) => { const ContractMethodField = ({ control, name, valueType, placeholder, setValue, getValues, isDisabled, onChange }: Props) => {
const ref = React.useRef<HTMLInputElement>(null); const ref = React.useRef<HTMLInputElement>(null);
const handleClear = React.useCallback(() => { const handleClear = React.useCallback(() => {
setValue(name, ''); setValue(name, '');
onClear(); onChange();
ref.current?.focus(); ref.current?.focus();
}, [ name, onClear, setValue ]); }, [ name, onChange, setValue ]);
const handleAddZeroesClick = React.useCallback((power: number) => {
const value = getValues()[name];
const zeroes = Array(power).fill('0').join('');
const newValue = value ? value + zeroes : '1' + zeroes;
setValue(name, newValue);
onChange();
}, [ getValues, name, onChange, setValue ]);
const hasZerosControl = valueType.includes('int') && !valueType.includes('[]'); const hasZerosControl = valueType.includes('int') && !valueType.includes('[]');
...@@ -54,12 +63,12 @@ const ContractMethodField = ({ control, name, valueType, placeholder, setValue, ...@@ -54,12 +63,12 @@ const ContractMethodField = ({ control, name, valueType, placeholder, setValue,
/> />
<InputRightElement w="auto" right={ 1 }> <InputRightElement w="auto" right={ 1 }>
{ field.value && <InputClearButton onClick={ handleClear } isDisabled={ isDisabled }/> } { field.value && <InputClearButton onClick={ handleClear } isDisabled={ isDisabled }/> }
{ hasZerosControl && <ContractMethodFieldZeroes/> } { hasZerosControl && <ContractMethodFieldZeroes onClick={ handleAddZeroesClick }/> }
</InputRightElement> </InputRightElement>
</InputGroup> </InputGroup>
</FormControl> </FormControl>
); );
}, [ handleClear, isDisabled, hasZerosControl, name, placeholder ]); }, [ name, isDisabled, placeholder, hasZerosControl, handleClear, handleAddZeroesClick ]);
return ( return (
<Controller <Controller
......
...@@ -18,8 +18,12 @@ import iconEastMini from 'icons/arrows/east-mini.svg'; ...@@ -18,8 +18,12 @@ import iconEastMini from 'icons/arrows/east-mini.svg';
import iconCheck from 'icons/check.svg'; import iconCheck from 'icons/check.svg';
import { times } from 'lib/html-entities'; import { times } from 'lib/html-entities';
const ContractMethodFieldZeroes = () => { interface Props {
const [ selectedOption, setSelectedOption ] = React.useState<number>(); onClick: (power: number) => void;
}
const ContractMethodFieldZeroes = ({ onClick }: Props) => {
const [ selectedOption, setSelectedOption ] = React.useState<number | undefined>(18);
const [ customValue, setCustomValue ] = React.useState<number>(); const [ customValue, setCustomValue ] = React.useState<number>();
const { isOpen, onToggle, onClose } = useDisclosure(); const { isOpen, onToggle, onClose } = useDisclosure();
...@@ -27,6 +31,7 @@ const ContractMethodFieldZeroes = () => { ...@@ -27,6 +31,7 @@ const ContractMethodFieldZeroes = () => {
const id = Number((event.currentTarget as HTMLDivElement).getAttribute('data-id')); const id = Number((event.currentTarget as HTMLDivElement).getAttribute('data-id'));
if (!Object.is(id, NaN)) { if (!Object.is(id, NaN)) {
setSelectedOption((prev) => prev === id ? undefined : id); setSelectedOption((prev) => prev === id ? undefined : id);
setCustomValue(undefined);
onClose(); onClose();
} }
}, [ onClose ]); }, [ onClose ]);
...@@ -38,6 +43,10 @@ const ContractMethodFieldZeroes = () => { ...@@ -38,6 +43,10 @@ const ContractMethodFieldZeroes = () => {
const value = selectedOption || customValue; const value = selectedOption || customValue;
const handleButtonClick = React.useCallback(() => {
value && onClick(value);
}, [ onClick, value ]);
return ( return (
<> <>
{ Boolean(value) && ( { Boolean(value) && (
...@@ -50,8 +59,7 @@ const ContractMethodFieldZeroes = () => { ...@@ -50,8 +59,7 @@ const ContractMethodFieldZeroes = () => {
variant="subtle" variant="subtle"
colorScheme="gray" colorScheme="gray"
display="inline" display="inline"
_hover={{ color: 'inherit' }} onClick={ handleButtonClick }
cursor="default"
> >
{ times } { times }
<chakra.span>10</chakra.span> <chakra.span>10</chakra.span>
......
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