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) => {
|
||||
return (
|
||||
<EllipsisDisplay>
|
||||
{convertCurrencyMicrosToCurrency(value?.amountMicros)}{' '}
|
||||
{value?.currencyCode}
|
||||
{convertCurrencyMicrosToCurrency(value?.amountMicros)}
|
||||
</EllipsisDisplay>
|
||||
);
|
||||
};
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
import { useContext } from 'react';
|
||||
import { useRecoilState } from 'recoil';
|
||||
|
||||
import { FieldInitialValue } from '@/ui/object/field/types/FieldInitialValue';
|
||||
|
||||
import { FieldContext } from '../../contexts/FieldContext';
|
||||
import { useFieldInitialValue } from '../../hooks/useFieldInitialValue';
|
||||
import { usePersistField } from '../../hooks/usePersistField';
|
||||
@ -10,6 +12,22 @@ import { assertFieldMetadata } from '../../types/guards/assertFieldMetadata';
|
||||
import { isFieldCurrency } from '../../types/guards/isFieldCurrency';
|
||||
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 = () => {
|
||||
const { entityId, fieldDefinition, hotkeyScope } = useContext(FieldContext);
|
||||
|
||||
@ -36,11 +54,7 @@ export const useCurrencyField = () => {
|
||||
|
||||
const fieldInitialValue = useFieldInitialValue();
|
||||
|
||||
const initialValue: FieldCurrencyValue = fieldInitialValue?.isEmpty
|
||||
? { amountMicros: 0, currencyCode: '' }
|
||||
: !isNaN(Number(fieldInitialValue?.value))
|
||||
? { amountMicros: Number(fieldInitialValue?.value), currencyCode: '' }
|
||||
: { amountMicros: 0, currencyCode: '' } ?? fieldValue;
|
||||
const initialValue = initializeValue(fieldInitialValue, fieldValue);
|
||||
|
||||
return {
|
||||
fieldDefinition,
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
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';
|
||||
|
||||
@ -80,7 +83,11 @@ export const CurrencyFieldInput = ({
|
||||
return (
|
||||
<FieldInputOverlay>
|
||||
<TextInput
|
||||
value={initialValue.amountMicros?.toString() ?? ''}
|
||||
value={
|
||||
convertCurrencyMicrosToCurrency(
|
||||
initialValue.amountMicros,
|
||||
)?.toString() ?? ''
|
||||
}
|
||||
placeholder="Currency"
|
||||
onClickOutside={handleClickOutside}
|
||||
onEnter={handleEnter}
|
||||
|
||||
Reference in New Issue
Block a user