UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

151 lines 5.25 kB
import React, { useCallback, useMemo } from 'react'; import StringField from "../String/index.js"; import { dnr, fnr } from '@navikt/fnrvalidator'; import { FormError } from "../../utils/index.js"; import useTranslation from "../../hooks/useTranslation.js"; import withComponentMarkers from "../../../../shared/helpers/withComponentMarkers.js"; import { jsx as _jsx } from "react/jsx-runtime"; function NationalIdentityNumber(props) { const translations = useTranslation().NationalIdentityNumber; const { label, errorRequired, errorFnr, errorFnrLength, errorDnr, errorDnrLength } = translations; const errorMessages = useMemo(() => ({ 'Field.errorRequired': errorRequired, 'Field.errorPattern': errorFnr, ...props.errorMessages }), [errorRequired, errorFnr, props.errorMessages]); const identificationNumberIsOfLength = (identificationNumber, length) => { return identificationNumber?.length === length; }; const fnrValidator = useCallback(value => { if (value !== undefined) { if (Number.parseInt(value.substring(0, 1), 10) > 3) { return Error(errorFnr); } const fnrIs11Digits = identificationNumberIsOfLength(value, 11); if (!fnrIs11Digits) { return Error(errorFnrLength); } if (fnrIs11Digits && fnr(value).status === 'invalid') { return Error(errorFnr); } } return undefined; }, [errorFnr, errorFnrLength]); const dnrValidator = useCallback(value => { if (value !== undefined) { if (Number.parseInt(value.substring(0, 1), 10) < 4) { return Error(errorDnr); } const dnrIs11Digits = identificationNumberIsOfLength(value, 11); if (!dnrIs11Digits) { return Error(errorDnrLength); } if (dnrIs11Digits && dnr(value).status === 'invalid') { return Error(errorDnr); } } return undefined; }, [errorDnr, errorDnrLength]); const dnrAndFnrValidator = useCallback(value => { const dnrValidationPattern = '^[4-9].*'; if (new RegExp(dnrValidationPattern).test(value)) { return dnrValidator(value); } return fnrValidator(value); }, [dnrValidator, fnrValidator]); const { validate = true, omitMask, onChangeValidator, onBlurValidator = dnrAndFnrValidator, width, label: labelProp } = props; const mask = 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 _jsx(StringField, { ...StringFieldProps }); } export 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; } export 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); } export function createMinimumAgeValidator(age) { return value => { if (typeof value !== 'string') { return undefined; } const identificationNumberIs7DigitsOrMore = value?.length >= 7; if (!identificationNumberIs7DigitsOrMore) { return new FormError('NationalIdentityNumber.errorMinimumAgeValidatorLength'); } if (identificationNumberIs7DigitsOrMore) { const date = getBirthDateByFnrOrDnr(value); if (getAgeByBirthDate(date) >= age) { return undefined; } } return new FormError('NationalIdentityNumber.errorMinimumAgeValidator', { messageValues: { age: String(age) } }); }; } export function createMinimumAgeVerifier(age) { const validator = createMinimumAgeValidator(age); return value => { if (value?.length >= 7) { return !(validator(value) instanceof Error); } return false; }; } withComponentMarkers(NationalIdentityNumber, { _supportsSpacingProps: true }); export default NationalIdentityNumber; //# sourceMappingURL=NationalIdentityNumber.js.map