Skip to content

Commit

Permalink
fix(wallet): Improve send token and nft forms (#2778)
Browse files Browse the repository at this point in the history
* fix: nft token send form initial validation

* fix: manually validate form inputs in Send Token Form

* fix: disbling button in TransferNftForm
  • Loading branch information
brancoder authored Sep 26, 2024
1 parent 3045570 commit 15121c8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
9 changes: 8 additions & 1 deletion apps/wallet/src/ui/app/components/address-input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,31 @@ export interface AddressInputProps {
placeholder?: string;
name: string;
label?: string;
shouldValidateManually?: boolean;
}

export function AddressInput({
disabled: forcedDisabled,
placeholder = '0x...',
name = 'to',
label = 'Enter Recipient Address',
shouldValidateManually,
}: AddressInputProps) {
const [field, meta] = useField(name);

const { isSubmitting, setFieldValue } = useFormikContext();
const { isSubmitting, setFieldValue, validateField } = useFormikContext();
const iotaAddressValidation = useIotaAddressValidation();

const disabled = forcedDisabled !== undefined ? forcedDisabled : isSubmitting;
const handleOnChange = useCallback<ChangeEventHandler<HTMLInputElement>>(
(e) => {
const address = e.currentTarget.value;
setFieldValue(name, iotaAddressValidation.cast(address));
if (shouldValidateManually) {
setTimeout(() => {
validateField(name);
}, 0);
}
},
[setFieldValue, name, iotaAddressValidation],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ export function TransferNFTForm({ objectId, objectType }: TransferNFTFormProps)
initialValues={{
to: '',
}}
validateOnMount
validateOnChange
validationSchema={validationSchema}
onSubmit={({ to }) => transferNFT.mutateAsync(to)}
>
{({ isValid }) => (
{({ isValid, dirty }) => (
<Form autoComplete="off" className="h-full">
<div className="flex h-full flex-col justify-between">
<Field
Expand All @@ -102,7 +102,11 @@ export function TransferNFTForm({ objectId, objectType }: TransferNFTFormProps)
placeholder="Enter Address"
/>

<Button htmlType={ButtonHtmlType.Submit} disabled={!isValid} text="Send" />
<Button
htmlType={ButtonHtmlType.Submit}
disabled={!(isValid && dirty)}
text="Send"
/>
</div>
</Form>
)}
Expand Down
9 changes: 7 additions & 2 deletions apps/wallet/src/ui/app/pages/home/transfer-coin/FormInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ export function FormInput({

const isActionButtonDisabled =
isInputDisabled || meta?.initialValue === meta?.value || !!meta?.error;
const errorMessage = meta?.error && meta.touched ? meta.error : undefined;
const errorMessage = meta?.error ? meta.error : undefined;

const isNumericFormat = props.type === InputType.NumericFormat;
const numericPropsOnly: Partial<NumericFormatInputProps> = {
decimalScale: decimals ? undefined : 0,
thousandSeparator: true,
onValueChange: (values) => form.setFieldValue(props.name, values.value),
onValueChange: (values) => {
form.setFieldValue(props.name, values.value);
setTimeout(() => {
form.validateField(props.name);
}, 0);
},
};

return (
Expand Down
20 changes: 10 additions & 10 deletions apps/wallet/src/ui/app/pages/home/transfer-coin/SendTokenForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: Apache-2.0

import { useActiveAddress } from '_app/hooks/useActiveAddress';
import { Loading } from '_components';
import { AddressInput, Loading } from '_components';
import { GAS_SYMBOL } from '_src/ui/app/redux/slices/iota-objects/Coin';
import {
useGetAllCoins,
Expand All @@ -17,7 +17,7 @@ import { useIotaClient } from '@iota/dapp-kit';
import { type CoinStruct } from '@iota/iota-sdk/client';
import { IOTA_TYPE_ARG } from '@iota/iota-sdk/utils';
import { useQuery } from '@tanstack/react-query';
import { Form, Formik, useFormikContext } from 'formik';
import { Field, Form, Formik, useFormikContext } from 'formik';
import { useEffect, useMemo } from 'react';

import { createValidationSchemaStepOne } from './validation';
Expand Down Expand Up @@ -115,7 +115,7 @@ function useGasBudgetEstimation({
// gasBudgetEstimation should change when the amount above changes

useEffect(() => {
setFieldValue('gasBudgetEst', formattedGas, true);
setFieldValue('gasBudgetEst', formattedGas, false);
}, [formattedGas, setFieldValue, values.amount]);

return formattedGas ? formattedGas + ' ' + GAS_SYMBOL : '--';
Expand Down Expand Up @@ -201,8 +201,8 @@ export function SendTokenForm({
}}
validationSchema={validationSchemaStepOne}
enableReinitialize
validateOnMount
validateOnChange
validateOnChange={false}
validateOnBlur={false}
onSubmit={handleFormSubmit}
>
{({ isValid, isSubmitting, setFieldValue, values, submitForm }) => {
Expand Down Expand Up @@ -235,7 +235,7 @@ export function SendTokenForm({
<div className="flex h-full w-full flex-col">
<Form autoComplete="off" noValidate className="flex-1">
<div className="flex h-full w-full flex-col gap-md">
{!hasEnoughBalance && isValid ? (
{!hasEnoughBalance ? (
<InfoBox
type={InfoBoxType.Warning}
supportingText="Insufficient IOTA to cover transaction"
Expand All @@ -252,12 +252,12 @@ export function SendTokenForm({
onActionClick={onMaxTokenButtonClick}
isActionButtonDisabled={isMaxActionDisabled}
/>

<FormInput
<Field
component={AddressInput}
allowNegative={false}
name="to"
placeholder="Enter Address"
label="Address"
type={InputType.Text}
shouldValidateManually
/>
</div>
</Form>
Expand Down

0 comments on commit 15121c8

Please sign in to comment.