UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

75 lines 3.38 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; 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; } import React, { useCallback, useMemo } from 'react'; import StringField from '../String'; import useTranslation from '../../hooks/useTranslation'; function OrganizationNumber(props) { const translations = useTranslation().OrganizationNumber; const { errorOrgNo, errorOrgNoLength, errorRequired, label } = translations; const errorMessages = useMemo(() => _objectSpread({ 'Field.errorRequired': errorRequired, 'Field.errorPattern': errorOrgNo }, props.errorMessages), [errorRequired, errorOrgNo, props.errorMessages]); const organizationNumberValidator = useCallback(value => { if (value !== undefined) { const orgNoIs9Digits = (value === null || value === void 0 ? void 0 : value.length) === 9; if (!orgNoIs9Digits) { return Error(errorOrgNoLength); } if (orgNoIs9Digits && !isValidOrgNumber(value)) { return Error(errorOrgNo); } } }, [errorOrgNo, errorOrgNoLength]); const { validate = true, omitMask, validator, onChangeValidator = validator, onBlurValidator = organizationNumberValidator, label: labelProp, width } = props; const mask = useMemo(() => omitMask ? [/\d/, /\d/, /\d/, /\d/, /\d/, /\d/, /\d/, /\d/, /\d/] : [/\d/, /\d/, /\d/, ' ', /\d/, /\d/, /\d/, ' ', /\d/, /\d/, /\d/], [omitMask]); const onBlurValidatorToUse = onBlurValidator === false ? undefined : onBlurValidator; const StringFieldProps = _objectSpread(_objectSpread({}, props), {}, { className: 'dnb-forms-field-organization-number', 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: { organizationNumberValidator } }); return React.createElement(StringField, StringFieldProps); } function isValidOrgNumber(digits) { if (parseFloat(digits) === 0) { return false; } let checkDigit = 2; let sum = 0; for (let i = digits.length - 2; i >= 0; --i) { sum += parseInt(digits.charAt(i)) * checkDigit; checkDigit += 1; if (checkDigit > 7) { checkDigit = 2; } } const result = 11 - sum % 11; const finalCheckDigit = result === 11 ? 0 : result; return parseInt(digits.charAt(digits.length - 1), 10) === finalCheckDigit; } OrganizationNumber._supportsSpacingProps = true; export default OrganizationNumber; //# sourceMappingURL=OrganizationNumber.js.map