Skip to content

Commit

Permalink
Добавил поле, которое умеет преобразовывать валюту
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Belik committed Jan 1, 2025
1 parent 8446c02 commit 19bfb1e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/@common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum FieldType {
SELECT_MULTIPLE = 'select-multiple',
DATE = 'date',
DATETIME = 'datetime-local',
SECRET_CURRENCY = 'secret-currency-field-multiple-by-10000',
}

// TODO: не уверен на счет 'button'
Expand Down
19 changes: 19 additions & 0 deletions src/@common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const getValueByType = new Map<FieldType, (evt: any) => any>([
[FieldType.FLOAT, getValue],
[FieldType.TEXT, getValue],
[FieldType.SELECT_MULTIPLE, getMultipleSelect],
[FieldType.SECRET_CURRENCY, getValue],
]);

const parseBoolean = (value: string | undefined): any =>
Expand Down Expand Up @@ -85,6 +86,9 @@ const toArray = (value: any): string[] => {
return [];
};

const parseSecretCur = (value: string) =>
value === '' ? null : Math.round(parseFloat(value) * 10000);

export const parseValueByType = new Map<FieldType, (a: any) => any>([
[FieldType.TEXT, parseDefault],
[FieldType.EMAIL, parseDefault],
Expand All @@ -96,6 +100,7 @@ export const parseValueByType = new Map<FieldType, (a: any) => any>([
[FieldType.PHONE, parsePhone],
[FieldType.DATE, parseDate],
[FieldType.DATETIME, parseDate],
[FieldType.SECRET_CURRENCY, parseSecretCur],
]);

export const inputTypeByType = new Map<FieldType, InputType>([
Expand All @@ -110,6 +115,20 @@ export const inputTypeByType = new Map<FieldType, InputType>([
[FieldType.PHONE, 'tel'],
[FieldType.DATE, 'date'],
[FieldType.DATETIME, 'datetime-local'],
[FieldType.SECRET_CURRENCY, 'number'],
]);

export const prepDefDefault = (v: any): any => v;

const prepDefSecretCur = (v: any): any => {
if (v) {
return v / 10000;
}
return v;
};

export const prepDefValue = new Map<FieldType, (v: any) => string>([
[FieldType.SECRET_CURRENCY, prepDefSecretCur],
]);

export const getInputTypeByFieldType = (fieldType: FieldType): InputType => {
Expand Down
13 changes: 10 additions & 3 deletions src/create-field/named-field/validated-field/validated-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
IgnoredProp,
ValidateFunc,
} from '~/@common/types';
import {getInputTypeByFieldType} from '~/@common/utils';
import {
getInputTypeByFieldType,
prepDefDefault,
prepDefValue,
} from '~/@common/utils';

import {useValidateInput} from './hooks/use-validate-input';

Expand Down Expand Up @@ -75,13 +79,16 @@ export const ValidatedField = <

return useMemo(() => {
const type = getInputTypeByFieldType(fieldType);
const defValue =
const prepDef = prepDefValue.get(fieldType) ?? prepDefDefault;
const defValue = prepDef(
defaultValueJustAdded !== undefined
? defaultValueJustAdded ?? undefined
: typeof fieldMeta?.defaultValue === 'undefined'
? defaultValue
: fieldMeta?.defaultValue;
: fieldMeta?.defaultValue,
);
const isChecked = ['checkbox', 'radio'].includes(type);

const inputProps: FieldInputProps<any> = {
defaultChecked: isChecked ? defValue : undefined,
defaultValue: isChecked ? undefined : defValue,
Expand Down

0 comments on commit 19bfb1e

Please sign in to comment.