Skip to content

Commit

Permalink
tweak: improve UX of create/edit account and multi-key selector
Browse files Browse the repository at this point in the history
  • Loading branch information
brusherru committed Sep 13, 2024
1 parent 2690327 commit e394e7f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
7 changes: 6 additions & 1 deletion src/components/CreateAccountModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ function CreateAccountModal({
}
}, [totalAmount, setValue]);

const close = () => {
reset(defaultValues);
onClose();
};

const submit = handleSubmit(async (data) => {
const success = await withPassword(
(password) =>
Expand Down Expand Up @@ -286,7 +291,7 @@ function CreateAccountModal({
};

return (
<Modal isOpen={isOpen} onClose={onClose} isCentered size="lg">
<Modal isOpen={isOpen} onClose={close} isCentered size="lg">
<Form control={control}>
<ModalOverlay />
<ModalContent>
Expand Down
29 changes: 10 additions & 19 deletions src/components/FormMultiKeySelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { IconPlus, IconTrash } from '@tabler/icons-react';

import { SafeKey } from '../types/wallet';
import { BUTTON_ICON_SIZE, MAX_MULTISIG_AMOUNT } from '../utils/constants';
import { noop } from '../utils/func';

import FormKeySelect from './FormKeySelect';

Expand All @@ -28,7 +27,7 @@ type Props<T extends FieldValues, FieldName extends ArrayPath<T>> = {
unregister: UseFormUnregister<T>;
errors: FieldErrors<T>;
isSubmitted?: boolean;
values?: string[];
values?: string[] | null;
};

function FormMultiKeySelect<
Expand All @@ -42,7 +41,7 @@ function FormMultiKeySelect<
unregister,
errors,
isSubmitted = false,
values = [],
values = null,
}: Props<T, FieldName>): JSX.Element {
const { fields, append, remove } = useFieldArray({
control,
Expand All @@ -61,22 +60,14 @@ function FormMultiKeySelect<
);

useEffect(() => {
// Have at least one field by default
if (!values) {
append(keys[0]?.publicKey as FieldArray<T, FieldName>);
if (!values || values.length === 0) {
append((keys?.[0]?.publicKey || '0x01') as FieldArray<T, FieldName>);
} else {
// In case there are some values — restore them in the form
values.forEach((v) => append(v as FieldArray<T, FieldName>));
}
}, [append, keys, values]);

useEffect(() => {
if (values.length === 0) {
return noop;
}
// In case there are some values — restore them in the form
values.forEach((_, idx) => append(idx as FieldArray<T, FieldName>));
return () => {
values.forEach((_, idx) => remove(idx));
};
}, [values, append, remove]);
return () => remove();
}, [values, append, remove, keys]);

const rootError = errors[fieldName]?.message;
return (
Expand All @@ -96,7 +87,7 @@ function FormMultiKeySelect<
errors={errors}
isSubmitted={isSubmitted}
isRequired
value={values[index] || keys[index]?.publicKey}
value={values?.[index] || keys[index]?.publicKey}
>
<IconButton
icon={<IconTrash size={BUTTON_ICON_SIZE} />}
Expand Down

0 comments on commit e394e7f

Please sign in to comment.