Fix link and currency input (#2697)

* fix link focus

* fix currency value null

* fix currency code nullable

* change in progress

* currency is working

* modify path
This commit is contained in:
bosiraphael 2023-11-24 15:19:29 +01:00 committed by GitHub
parent 323c69948c
commit cf1b0bfccf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 84 additions and 70 deletions

View File

@ -48,8 +48,6 @@ export const FieldInput = ({
}: FieldInputProps) => { }: FieldInputProps) => {
const { fieldDefinition } = useContext(FieldContext); const { fieldDefinition } = useContext(FieldContext);
console.log(fieldDefinition);
return ( return (
<> <>
{isFieldRelation(fieldDefinition) ? ( {isFieldRelation(fieldDefinition) ? (

View File

@ -2,7 +2,7 @@ import { useCurrencyField } from '../../hooks/useCurrencyField';
import { CurrencyDisplay } from '../content-display/components/CurrencyDisplay'; import { CurrencyDisplay } from '../content-display/components/CurrencyDisplay';
export const CurrencyFieldDisplay = () => { export const CurrencyFieldDisplay = () => {
const { fieldValue } = useCurrencyField(); const { initialAmount } = useCurrencyField();
return <CurrencyDisplay value={fieldValue} />; return <CurrencyDisplay amount={initialAmount} />;
}; };

View File

@ -1,17 +1,10 @@
import { FieldCurrencyValue } from '@/ui/object/field/types/FieldMetadata';
import { convertCurrencyMicrosToCurrency } from '~/utils/convert-currency-amount';
import { EllipsisDisplay } from './EllipsisDisplay'; import { EllipsisDisplay } from './EllipsisDisplay';
type CurrencyDisplayProps = { type CurrencyDisplayProps = {
value?: FieldCurrencyValue; amount?: number | null;
}; };
// TODO: convert currencyCode to currency symbol // TODO: convert currencyCode to currency symbol
export const CurrencyDisplay = ({ value }: CurrencyDisplayProps) => { export const CurrencyDisplay = ({ amount }: CurrencyDisplayProps) => {
return ( return <EllipsisDisplay>{amount}</EllipsisDisplay>;
<EllipsisDisplay>
{convertCurrencyMicrosToCurrency(value?.amountMicros)}
</EllipsisDisplay>
);
}; };

View File

@ -2,6 +2,11 @@ import { useContext } from 'react';
import { useRecoilState } from 'recoil'; import { useRecoilState } from 'recoil';
import { FieldInitialValue } from '@/ui/object/field/types/FieldInitialValue'; import { FieldInitialValue } from '@/ui/object/field/types/FieldInitialValue';
import { canBeCastAsIntegerOrNull } from '~/utils/cast-as-integer-or-null';
import {
convertCurrencyMicrosToCurrency,
convertCurrencyToCurrencyMicros,
} from '~/utils/convert-currency-amount';
import { FieldContext } from '../../contexts/FieldContext'; import { FieldContext } from '../../contexts/FieldContext';
import { useFieldInitialValue } from '../../hooks/useFieldInitialValue'; import { useFieldInitialValue } from '../../hooks/useFieldInitialValue';
@ -17,15 +22,18 @@ const initializeValue = (
fieldValue: FieldCurrencyValue, fieldValue: FieldCurrencyValue,
) => { ) => {
if (fieldInitialValue?.isEmpty) { if (fieldInitialValue?.isEmpty) {
return { amountMicros: 0, currencyCode: 'USD' }; return { amount: null, currencyCode: 'USD' };
} }
if (!isNaN(Number(fieldInitialValue?.value))) { if (!isNaN(Number(fieldInitialValue?.value))) {
return { return {
amountMicros: Number(fieldInitialValue?.value), amount: Number(fieldInitialValue?.value),
currencyCode: 'USD', currencyCode: 'USD',
}; };
} }
return fieldValue; return {
amount: convertCurrencyMicrosToCurrency(fieldValue.amountMicros),
currencyCode: fieldValue.currencyCode,
};
}; };
export const useCurrencyField = () => { export const useCurrencyField = () => {
@ -44,22 +52,43 @@ export const useCurrencyField = () => {
const persistField = usePersistField(); const persistField = usePersistField();
const persistCurrencyField = (newValue: FieldCurrencyValue) => { const persistCurrencyField = ({
if (!isFieldCurrencyValue(newValue)) { amountText,
currencyCode,
}: {
amountText: string;
currencyCode: string;
}) => {
if (!canBeCastAsIntegerOrNull(amountText)) {
return; return;
} }
const amount = parseFloat(amountText);
persistField(newValue); const newCurrencyValue = {
amountMicros: isNaN(amount)
? null
: convertCurrencyToCurrencyMicros(amount),
currencyCode: currencyCode,
};
if (!isFieldCurrencyValue(newCurrencyValue)) {
return;
}
persistField(newCurrencyValue);
}; };
const fieldInitialValue = useFieldInitialValue(); const fieldInitialValue = useFieldInitialValue();
const initialValue = initializeValue(fieldInitialValue, fieldValue); const initialValue = initializeValue(fieldInitialValue, fieldValue);
const initialAmount = initialValue.amount;
const initialCurrencyCode = initialValue.currencyCode;
return { return {
fieldDefinition, fieldDefinition,
fieldValue, fieldValue,
initialValue, initialAmount,
initialCurrencyCode,
setFieldValue, setFieldValue,
hotkeyScope, hotkeyScope,
persistCurrencyField, persistCurrencyField,

View File

@ -1,8 +1,4 @@
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 {
convertCurrencyMicrosToCurrency,
convertCurrencyToCurrencyMicros,
} from '~/utils/convert-currency-amount';
import { useCurrencyField } from '../../hooks/useCurrencyField'; import { useCurrencyField } from '../../hooks/useCurrencyField';
@ -24,58 +20,57 @@ export const CurrencyFieldInput = ({
onTab, onTab,
onShiftTab, onShiftTab,
}: CurrencyFieldInputProps) => { }: CurrencyFieldInputProps) => {
const { hotkeyScope, initialValue, persistCurrencyField } = const {
useCurrencyField(); hotkeyScope,
initialAmount,
initialCurrencyCode,
persistCurrencyField,
} = useCurrencyField();
const handleEnter = (newValue: string) => { const handleEnter = (newValue: string) => {
onEnter?.(() => onEnter?.(() => {
persistCurrencyField({ persistCurrencyField({
amountMicros: amountText: newValue,
convertCurrencyToCurrencyMicros(parseFloat(newValue)) ?? 0, currencyCode: initialCurrencyCode,
currencyCode: initialValue.currencyCode, });
}), });
);
}; };
const handleEscape = (newValue: string) => { const handleEscape = (newValue: string) => {
onEscape?.(() => onEscape?.(() => {
persistCurrencyField({ persistCurrencyField({
amountMicros: amountText: newValue,
convertCurrencyToCurrencyMicros(parseFloat(newValue)) ?? 0, currencyCode: initialCurrencyCode,
currencyCode: initialValue.currencyCode, });
}), });
);
}; };
const handleClickOutside = ( const handleClickOutside = (
event: MouseEvent | TouchEvent, event: MouseEvent | TouchEvent,
newValue: string, newValue: string,
) => { ) => {
onClickOutside?.(() => onClickOutside?.(() => {
persistCurrencyField({ persistCurrencyField({
amountMicros: amountText: newValue,
convertCurrencyToCurrencyMicros(parseFloat(newValue)) ?? 0, currencyCode: initialCurrencyCode,
currencyCode: initialValue.currencyCode, });
}), });
);
}; };
const handleTab = (newValue: string) => { const handleTab = (newValue: string) => {
onTab?.(() => onTab?.(() => {
persistCurrencyField({ persistCurrencyField({
amountMicros: amountText: newValue,
convertCurrencyToCurrencyMicros(parseFloat(newValue)) ?? 0, currencyCode: initialCurrencyCode,
currencyCode: initialValue.currencyCode, });
}), });
);
}; };
const handleShiftTab = (newValue: string) => { const handleShiftTab = (newValue: string) => {
onShiftTab?.(() => onShiftTab?.(() =>
persistCurrencyField({ persistCurrencyField({
amountMicros: amountText: newValue,
convertCurrencyToCurrencyMicros(parseFloat(newValue)) ?? 0, currencyCode: initialCurrencyCode,
currencyCode: initialValue.currencyCode,
}), }),
); );
}; };
@ -83,11 +78,8 @@ export const CurrencyFieldInput = ({
return ( return (
<FieldInputOverlay> <FieldInputOverlay>
<TextInput <TextInput
value={ value={initialAmount?.toString() ?? ''}
convertCurrencyMicrosToCurrency( autoFocus
initialValue.amountMicros,
)?.toString() ?? ''
}
placeholder="Currency" placeholder="Currency"
onClickOutside={handleClickOutside} onClickOutside={handleClickOutside}
onEnter={handleEnter} onEnter={handleEnter}

View File

@ -74,6 +74,7 @@ export const LinkFieldInput = ({
<FieldInputOverlay> <FieldInputOverlay>
<TextInput <TextInput
value={initialValue.url} value={initialValue.url}
autoFocus
placeholder="URL" placeholder="URL"
hotkeyScope={hotkeyScope} hotkeyScope={hotkeyScope}
onClickOutside={handleClickOutside} onClickOutside={handleClickOutside}

View File

@ -103,7 +103,10 @@ export type FieldBooleanValue = boolean;
export type FieldPhoneValue = string; export type FieldPhoneValue = string;
export type FieldEmailValue = string; export type FieldEmailValue = string;
export type FieldLinkValue = { url: string; label: string }; export type FieldLinkValue = { url: string; label: string };
export type FieldCurrencyValue = { currencyCode: string; amountMicros: number }; export type FieldCurrencyValue = {
currencyCode: string;
amountMicros: number | null;
};
export type FieldFullNameValue = { firstName: string; lastName: string }; export type FieldFullNameValue = { firstName: string; lastName: string };
export type FieldProbabilityValue = number; export type FieldProbabilityValue = number;

View File

@ -3,8 +3,8 @@ import { z } from 'zod';
import { FieldCurrencyValue } from '../FieldMetadata'; import { FieldCurrencyValue } from '../FieldMetadata';
const currencySchema = z.object({ const currencySchema = z.object({
currencyCode: z.string(), currencyCode: z.string().nullable(),
amountMicros: z.number(), amountMicros: z.number().nullable(),
}); });
export const isFieldCurrencyValue = ( export const isFieldCurrencyValue = (

View File

@ -1,10 +1,8 @@
import { isUndefined } from '@sniptt/guards';
export const convertCurrencyToCurrencyMicros = ( export const convertCurrencyToCurrencyMicros = (
currencyAmount: number | undefined, currencyAmount: number | null | undefined,
) => { ) => {
if (!currencyAmount) { if (currencyAmount == null) {
return undefined; return null;
} }
const currencyAmountAsNumber = +currencyAmount; const currencyAmountAsNumber = +currencyAmount;
if (isNaN(currencyAmountAsNumber)) { if (isNaN(currencyAmountAsNumber)) {
@ -18,10 +16,10 @@ export const convertCurrencyToCurrencyMicros = (
}; };
export const convertCurrencyMicrosToCurrency = ( export const convertCurrencyMicrosToCurrency = (
currencyAmountMicros: number | undefined, currencyAmountMicros: number | null | undefined,
) => { ) => {
if (isUndefined(currencyAmountMicros)) { if (currencyAmountMicros == null) {
return undefined; return null;
} }
const currencyAmountMicrosAsNumber = +currencyAmountMicros; const currencyAmountMicrosAsNumber = +currencyAmountMicros;
if (isNaN(currencyAmountMicrosAsNumber)) { if (isNaN(currencyAmountMicrosAsNumber)) {