UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

159 lines (158 loc) 6.52 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 = _interopRequireWildcard(require("react")); var _index = _interopRequireDefault(require("../String/index.js")); var _fnrvalidator = require("@navikt/fnrvalidator"); var _index2 = require("../../utils/index.js"); var _useTranslation = _interopRequireDefault(require("../../hooks/useTranslation.js")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } 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 identificationNumberIsOfLength = (identificationNumber, length) => { return (identificationNumber === null || identificationNumber === void 0 ? void 0 : identificationNumber.length) === length; }; const fnrValidator = (0, _react.useCallback)(value => { if (value !== undefined) { if (Number.parseInt(value.substring(0, 1)) > 3) { return Error(errorFnr); } const fnrIs11Digits = identificationNumberIsOfLength(value, 11); if (!fnrIs11Digits) { return Error(errorFnrLength); } if (fnrIs11Digits && (0, _fnrvalidator.fnr)(value).status === 'invalid') { return Error(errorFnr); } } }, [errorFnr, errorFnrLength]); const dnrValidator = (0, _react.useCallback)(value => { if (value !== undefined) { if (Number.parseInt(value.substring(0, 1)) < 4) { return Error(errorDnr); } const dnrIs11Digits = identificationNumberIsOfLength(value, 11); if (!dnrIs11Digits) { return Error(errorDnrLength); } if (dnrIs11Digits && (0, _fnrvalidator.dnr)(value).status === 'invalid') { return Error(errorDnr); } } }, [errorDnr, errorDnrLength]); const dnrAndFnrValidator = (0, _react.useCallback)(value => { const dnrValidationPattern = '^[4-9].*'; if (new RegExp(dnrValidationPattern).test(value)) { return dnrValidator(value); } return fnrValidator(value); }, [dnrValidator, fnrValidator]); const { validate = true, omitMask, validator, onChangeValidator = validator, 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, width: width !== null && width !== void 0 ? width : 'medium', inputMode: 'numeric', onChangeValidator: validate ? onChangeValidator : undefined, onBlurValidator: validate ? onBlurValidatorToUse : undefined, exportValidators: { dnrValidator, fnrValidator, dnrAndFnrValidator } }; return _react.default.createElement(_index.default, StringFieldProps); } function getAgeByBirthDate(birthDate) { const 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; } const yearPart = value.substring(4, 6); const centuryNumber = Number.parseInt(value.substring(6, 7)); const isBornIn20XX = centuryNumber >= 5; const year = isBornIn20XX ? `20${yearPart}` : `19${yearPart}`; const month = Number.parseInt(value.substring(2, 4)); const differentiatorValue = value.length > 0 ? Number.parseInt(value.substring(0, 1)) : undefined; const isDnr = differentiatorValue && differentiatorValue > 3; const day = isDnr ? Number.parseInt(value.substring(0, 2)) - 40 : Number.parseInt(value.substring(0, 2)); return new Date(Number.parseInt(year), month - 1, day); } function createMinimumAgeValidator(age) { return value => { if (typeof value !== 'string') { return; } const identificationNumberIs7DigitsOrMore = (value === null || value === void 0 ? void 0 : value.length) >= 7; if (!identificationNumberIs7DigitsOrMore) { return new _index2.FormError('NationalIdentityNumber.errorMinimumAgeValidatorLength', { validationRule: 'errorMinimumAgeValidatorLength' }); } if (identificationNumberIs7DigitsOrMore) { const date = getBirthDateByFnrOrDnr(value); if (getAgeByBirthDate(date) >= age) { return; } } return new _index2.FormError('NationalIdentityNumber.errorMinimumAgeValidator', { validationRule: 'errorMinimumAgeValidator', messageValues: { age: String(age) } }); }; } function createMinimumAgeVerifier(age) { const validator = createMinimumAgeValidator(age); return value => { if ((value === null || value === void 0 ? void 0 : value.length) >= 7) { return !(validator(value) instanceof Error); } return false; }; } NationalIdentityNumber._supportsSpacingProps = true; var _default = exports.default = NationalIdentityNumber; //# sourceMappingURL=NationalIdentityNumber.js.map