Commit 2334ef5b authored by tom's avatar tom

fixed size array

parent ab6e752e
...@@ -100,7 +100,7 @@ const ContractMethodField = ({ control, name, groupName, index, argType, placeho ...@@ -100,7 +100,7 @@ const ContractMethodField = ({ control, name, groupName, index, argType, placeho
autoComplete="off" autoComplete="off"
/> />
<InputRightElement w="auto" right={ 1 }> <InputRightElement w="auto" right={ 1 }>
{ field.value && <ClearButton onClick={ handleClear } isDisabled={ isDisabled }/> } { typeof field.value === 'string' && field.value.replace('\n', '') && <ClearButton onClick={ handleClear } isDisabled={ isDisabled }/> }
{ hasZerosControl && <ContractMethodFieldZeroes onClick={ handleAddZeroesClick } isDisabled={ isDisabled }/> } { hasZerosControl && <ContractMethodFieldZeroes onClick={ handleAddZeroesClick } isDisabled={ isDisabled }/> }
</InputRightElement> </InputRightElement>
</InputGroup> </InputGroup>
...@@ -110,11 +110,17 @@ const ContractMethodField = ({ control, name, groupName, index, argType, placeho ...@@ -110,11 +110,17 @@ const ContractMethodField = ({ control, name, groupName, index, argType, placeho
); );
}, [ index, groupName, name, intMatch, isDisabled, placeholder, handleClear, handleAddZeroesClick ]); }, [ index, groupName, name, intMatch, isDisabled, placeholder, handleClear, handleAddZeroesClick ]);
const validate = React.useCallback((value: string | Array<string>) => { const validate = React.useCallback((_value: string | Array<string>) => {
if (typeof value === 'object' || !value) { if (typeof _value === 'object') {
return; return;
} }
const value = _value.replace('\n', '');
if (!value) {
return 'Field is required';
}
if (argType === 'address') { if (argType === 'address') {
return !isAddress(value) ? 'Invalid address format' : true; return !isAddress(value) ? 'Invalid address format' : true;
} }
......
...@@ -22,15 +22,26 @@ interface Props { ...@@ -22,15 +22,26 @@ interface Props {
onChange: () => void; onChange: () => void;
} }
const ContractMethodFieldArray = ({ control, name, setValue, getValues, isDisabled, argType, onChange }: Props) => { const ContractMethodFieldArray = ({ control, name, setValue, getValues, isDisabled, argType, onChange, size }: Props) => {
const { fields, append, remove } = useFieldArray({ const { fields, append, remove } = useFieldArray({
name: name as never, name: name as never,
control, control,
}); });
React.useEffect(() => { React.useEffect(() => {
fields.length === 0 && append(''); if (fields.length === 0) {
}, [ append, fields.length ]); if (size === Infinity) {
append('');
} else {
for (let i = 0; i < size - 1; i++) {
// a little hack to append multiple empty fields in the array
// had to adjust code in ContractMethodField as well
append('\n');
}
}
}
}, [ fields.length, append, size ]);
const handleAddButtonClick = React.useCallback(() => { const handleAddButtonClick = React.useCallback(() => {
append(''); append('');
...@@ -60,7 +71,7 @@ const ContractMethodFieldArray = ({ control, name, setValue, getValues, isDisabl ...@@ -60,7 +71,7 @@ const ContractMethodFieldArray = ({ control, name, setValue, getValues, isDisabl
isDisabled={ isDisabled } isDisabled={ isDisabled }
onChange={ onChange } onChange={ onChange }
/> />
{ array.length > 1 && ( { array.length > 1 && size === Infinity && (
<IconButton <IconButton
aria-label="remove" aria-label="remove"
data-index={ index } data-index={ index }
...@@ -73,7 +84,7 @@ const ContractMethodFieldArray = ({ control, name, setValue, getValues, isDisabl ...@@ -73,7 +84,7 @@ const ContractMethodFieldArray = ({ control, name, setValue, getValues, isDisabl
isDisabled={ isDisabled } isDisabled={ isDisabled }
/> />
) } ) }
{ index === array.length - 1 && ( { index === array.length - 1 && size === Infinity && (
<IconButton <IconButton
aria-label="add" aria-label="add"
data-index={ index } data-index={ index }
......
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