UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

136 lines (135 loc) 5.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createMinimumAgeValidator = createMinimumAgeValidator; exports.createMinimumAgeVerifier = createMinimumAgeVerifier; exports.default = void 0; exports.getAgeByBirthDate = getAgeByBirthDate; exports.getBirthDateByFnrOrDnr = getBirthDateByFnrOrDnr; var _react = require("react"); var _index = _interopRequireDefault(require("../String/index.js")); var _validators = require("./validators.js"); var _index2 = require("../../utils/index.js"); var _useTranslation = _interopRequireDefault(require("../../hooks/useTranslation.js")); var _withComponentMarkers = _interopRequireDefault(require("../../../../shared/helpers/withComponentMarkers.js")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function NationalIdentityNumber(props) { const translations = (0, _useTranslation.default)().NationalIdentityNumber; const { label, errorRequired, errorFnr, errorFnrLength, errorDnr, errorDnrLength } = translations; const errorMessages = (0, _react.useMemo)(() => ({ 'Field.errorRequired': errorRequired, 'Field.errorPattern': errorFnr, ...props.errorMessages }), [errorRequired, errorFnr, props.errorMessages]); const fnrValidator = (0, _react.useCallback)(value => (0, _validators.validateFnr)(value, { errorFnr, errorFnrLength }), [errorFnr, errorFnrLength]); const dnrValidator = (0, _react.useCallback)(value => (0, _validators.validateDnr)(value, { errorDnr, errorDnrLength }), [errorDnr, errorDnrLength]); const dnrAndFnrValidator = (0, _react.useCallback)(value => (0, _validators.validateDnrAndFnr)(value, { errorFnr, errorFnrLength, errorDnr, errorDnrLength }), [errorFnr, errorFnrLength, errorDnr, errorDnrLength]); const { validate = true, omitMask, onChangeValidator, onBlurValidator = dnrAndFnrValidator, width, label: labelProp } = props; const mask = (0, _react.useMemo)(() => omitMask ? Array(11).fill(/\d/) : [/\d/, /\d/, /\d/, /\d/, /\d/, /\d/, ' ', /\d/, /\d/, /\d/, /\d/, /\d/], [omitMask]); const onBlurValidatorToUse = onBlurValidator === false ? undefined : onBlurValidator; const StringFieldProps = { ...props, label: labelProp !== null && labelProp !== void 0 ? labelProp : label, errorMessages, mask, allowOverflow: true, width: width !== null && width !== void 0 ? width : 'medium', inputMode: 'numeric', onChangeValidator: validate ? onChangeValidator : undefined, onBlurValidator: validate ? onBlurValidatorToUse : undefined, exportValidators: { dnrValidator, fnrValidator, dnrAndFnrValidator } }; return (0, _jsxRuntime.jsx)(_index.default, { ...StringFieldProps }); } function getAgeByBirthDate(birthDate, today = new Date()) { const age = today.getFullYear() - birthDate.getFullYear(); const month = today.getMonth() - birthDate.getMonth(); const day = today.getDate() - birthDate.getDate(); if (month < 0 || month === 0 && day < 0) { return age - 1; } return age; } function getBirthDateByFnrOrDnr(value) { if (value === undefined) { return undefined; } const yearPart = value.substring(4, 6); const centuryNumber = Number.parseInt(value.substring(6, 7), 10); const isBornIn20XX = centuryNumber >= 5; const year = isBornIn20XX ? `20${yearPart}` : `19${yearPart}`; const month = Number.parseInt(value.substring(2, 4), 10); const differentiatorValue = value.length > 0 ? Number.parseInt(value.substring(0, 1), 10) : undefined; const isDnr = differentiatorValue && differentiatorValue > 3; const day = isDnr ? Number.parseInt(value.substring(0, 2), 10) - 40 : Number.parseInt(value.substring(0, 2), 10); return new Date(Number.parseInt(year, 10), month - 1, day); } function createMinimumAgeValidator(age, getToday = () => new Date()) { return value => { if (typeof value !== 'string') { return undefined; } const identificationNumberIs7DigitsOrMore = value?.length >= 7; if (!identificationNumberIs7DigitsOrMore) { return new _index2.FormError('NationalIdentityNumber.errorMinimumAgeValidatorLength'); } if (identificationNumberIs7DigitsOrMore) { const date = getBirthDateByFnrOrDnr(value); if (getAgeByBirthDate(date, getToday()) >= age) { return undefined; } } return new _index2.FormError('NationalIdentityNumber.errorMinimumAgeValidator', { messageValues: { age: String(age) } }); }; } function createMinimumAgeVerifier(age, getToday) { const validator = createMinimumAgeValidator(age, getToday); return value => { if (value?.length >= 7) { return !(validator(value) instanceof Error); } return false; }; } (0, _withComponentMarkers.default)(NationalIdentityNumber, { _supportsSpacingProps: true }); var _default = exports.default = NationalIdentityNumber; //# sourceMappingURL=NationalIdentityNumber.js.map