diff --git a/src/@common/types.ts b/src/@common/types.ts index c817a2b..7603ceb 100644 --- a/src/@common/types.ts +++ b/src/@common/types.ts @@ -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' diff --git a/src/@common/utils.ts b/src/@common/utils.ts index fbf54d7..a8647e1 100644 --- a/src/@common/utils.ts +++ b/src/@common/utils.ts @@ -55,6 +55,7 @@ const getValueByType = new Map any>([ [FieldType.FLOAT, getValue], [FieldType.TEXT, getValue], [FieldType.SELECT_MULTIPLE, getMultipleSelect], + [FieldType.SECRET_CURRENCY, getValue], ]); const parseBoolean = (value: string | undefined): any => @@ -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 any>([ [FieldType.TEXT, parseDefault], [FieldType.EMAIL, parseDefault], @@ -96,6 +100,7 @@ export const parseValueByType = new Map any>([ [FieldType.PHONE, parsePhone], [FieldType.DATE, parseDate], [FieldType.DATETIME, parseDate], + [FieldType.SECRET_CURRENCY, parseSecretCur], ]); export const inputTypeByType = new Map([ @@ -110,6 +115,20 @@ export const inputTypeByType = new Map([ [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 string>([ + [FieldType.SECRET_CURRENCY, prepDefSecretCur], ]); export const getInputTypeByFieldType = (fieldType: FieldType): InputType => { diff --git a/src/create-field/named-field/validated-field/validated-field.tsx b/src/create-field/named-field/validated-field/validated-field.tsx index 53671a2..e6be3e1 100644 --- a/src/create-field/named-field/validated-field/validated-field.tsx +++ b/src/create-field/named-field/validated-field/validated-field.tsx @@ -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'; @@ -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 = { defaultChecked: isChecked ? defValue : undefined, defaultValue: isChecked ? undefined : defValue,