@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
164 lines (163 loc) • 8.06 kB
JavaScript
;
require("core-js/modules/web.dom-collections.iterator.js");
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 _String = _interopRequireDefault(require("../String"));
var _fnrvalidator = require("@navikt/fnrvalidator");
var _utils = require("../../utils");
var _useTranslation = _interopRequireDefault(require("../../hooks/useTranslation"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function NationalIdentityNumber(props) {
const translations = (0, _useTranslation.default)().NationalIdentityNumber;
const {
label,
errorRequired,
errorFnr,
errorFnrLength,
errorDnr,
errorDnrLength
} = translations;
const errorMessages = (0, _react.useMemo)(() => _objectSpread({
'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 = _objectSpread(_objectSpread({}, 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(_String.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 _utils.FormError('NationalIdentityNumber.errorMinimumAgeValidatorLength', {
validationRule: 'errorMinimumAgeValidatorLength'
});
}
if (identificationNumberIs7DigitsOrMore) {
const date = getBirthDateByFnrOrDnr(value);
if (getAgeByBirthDate(date) >= age) {
return;
}
}
return new _utils.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