fix-currency-field-input (#2666)
* fix-currency-field-input * modify according to comments
This commit is contained in:
@ -11,8 +11,7 @@ type CurrencyDisplayProps = {
|
|||||||
export const CurrencyDisplay = ({ value }: CurrencyDisplayProps) => {
|
export const CurrencyDisplay = ({ value }: CurrencyDisplayProps) => {
|
||||||
return (
|
return (
|
||||||
<EllipsisDisplay>
|
<EllipsisDisplay>
|
||||||
{convertCurrencyMicrosToCurrency(value?.amountMicros)}{' '}
|
{convertCurrencyMicrosToCurrency(value?.amountMicros)}
|
||||||
{value?.currencyCode}
|
|
||||||
</EllipsisDisplay>
|
</EllipsisDisplay>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
import { useContext } from 'react';
|
import { useContext } from 'react';
|
||||||
import { useRecoilState } from 'recoil';
|
import { useRecoilState } from 'recoil';
|
||||||
|
|
||||||
|
import { FieldInitialValue } from '@/ui/object/field/types/FieldInitialValue';
|
||||||
|
|
||||||
import { FieldContext } from '../../contexts/FieldContext';
|
import { FieldContext } from '../../contexts/FieldContext';
|
||||||
import { useFieldInitialValue } from '../../hooks/useFieldInitialValue';
|
import { useFieldInitialValue } from '../../hooks/useFieldInitialValue';
|
||||||
import { usePersistField } from '../../hooks/usePersistField';
|
import { usePersistField } from '../../hooks/usePersistField';
|
||||||
@ -10,6 +12,22 @@ import { assertFieldMetadata } from '../../types/guards/assertFieldMetadata';
|
|||||||
import { isFieldCurrency } from '../../types/guards/isFieldCurrency';
|
import { isFieldCurrency } from '../../types/guards/isFieldCurrency';
|
||||||
import { isFieldCurrencyValue } from '../../types/guards/isFieldCurrencyValue';
|
import { isFieldCurrencyValue } from '../../types/guards/isFieldCurrencyValue';
|
||||||
|
|
||||||
|
const initializeValue = (
|
||||||
|
fieldInitialValue: FieldInitialValue | undefined,
|
||||||
|
fieldValue: FieldCurrencyValue,
|
||||||
|
) => {
|
||||||
|
if (fieldInitialValue?.isEmpty) {
|
||||||
|
return { amountMicros: 0, currencyCode: 'USD' };
|
||||||
|
}
|
||||||
|
if (!isNaN(Number(fieldInitialValue?.value))) {
|
||||||
|
return {
|
||||||
|
amountMicros: Number(fieldInitialValue?.value),
|
||||||
|
currencyCode: 'USD',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return fieldValue;
|
||||||
|
};
|
||||||
|
|
||||||
export const useCurrencyField = () => {
|
export const useCurrencyField = () => {
|
||||||
const { entityId, fieldDefinition, hotkeyScope } = useContext(FieldContext);
|
const { entityId, fieldDefinition, hotkeyScope } = useContext(FieldContext);
|
||||||
|
|
||||||
@ -36,11 +54,7 @@ export const useCurrencyField = () => {
|
|||||||
|
|
||||||
const fieldInitialValue = useFieldInitialValue();
|
const fieldInitialValue = useFieldInitialValue();
|
||||||
|
|
||||||
const initialValue: FieldCurrencyValue = fieldInitialValue?.isEmpty
|
const initialValue = initializeValue(fieldInitialValue, fieldValue);
|
||||||
? { amountMicros: 0, currencyCode: '' }
|
|
||||||
: !isNaN(Number(fieldInitialValue?.value))
|
|
||||||
? { amountMicros: Number(fieldInitialValue?.value), currencyCode: '' }
|
|
||||||
: { amountMicros: 0, currencyCode: '' } ?? fieldValue;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
fieldDefinition,
|
fieldDefinition,
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
import { TextInput } from '@/ui/object/field/meta-types/input/components/internal/TextInput';
|
import { TextInput } from '@/ui/object/field/meta-types/input/components/internal/TextInput';
|
||||||
import { convertCurrencyToCurrencyMicros } from '~/utils/convert-currency-amount';
|
import {
|
||||||
|
convertCurrencyMicrosToCurrency,
|
||||||
|
convertCurrencyToCurrencyMicros,
|
||||||
|
} from '~/utils/convert-currency-amount';
|
||||||
|
|
||||||
import { useCurrencyField } from '../../hooks/useCurrencyField';
|
import { useCurrencyField } from '../../hooks/useCurrencyField';
|
||||||
|
|
||||||
@ -80,7 +83,11 @@ export const CurrencyFieldInput = ({
|
|||||||
return (
|
return (
|
||||||
<FieldInputOverlay>
|
<FieldInputOverlay>
|
||||||
<TextInput
|
<TextInput
|
||||||
value={initialValue.amountMicros?.toString() ?? ''}
|
value={
|
||||||
|
convertCurrencyMicrosToCurrency(
|
||||||
|
initialValue.amountMicros,
|
||||||
|
)?.toString() ?? ''
|
||||||
|
}
|
||||||
placeholder="Currency"
|
placeholder="Currency"
|
||||||
onClickOutside={handleClickOutside}
|
onClickOutside={handleClickOutside}
|
||||||
onEnter={handleEnter}
|
onEnter={handleEnter}
|
||||||
|
|||||||
Reference in New Issue
Block a user