UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

148 lines (147 loc) 5.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.namePattern = exports.default = exports.companyPattern = void 0; var _react = _interopRequireWildcard(require("react")); var _index = _interopRequireDefault(require("../String/index.js")); var _useTranslation = _interopRequireDefault(require("../../hooks/useTranslation.js")); var _index2 = require("../../utils/index.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); } const namePattern = exports.namePattern = '^(?!.*[-\\s]{2})[\\p{L}]([\\p{L}\\p{M}\\p{Zs}-]*[\\p{L}])?$'; const companyPattern = exports.companyPattern = '^(?!.*[-\\s]{2})(?!.*[\\.]{2})[\\p{L}\\p{N}]([\\p{L}\\p{N}\\p{P}\\p{Zs}.]*[\\p{L}\\p{N}])?$'; function Name(props) { const { onBlurValidator: onBlurValidatorProp, minLength: minLengthProp = 1, ...otherProps } = props; const nameValidator = (0, _react.useCallback)(value => { if (value !== undefined) { if (value === '') { return; } if (!new RegExp(namePattern, 'u').test(value)) { return new _index2.FormError('Field.errorPattern'); } } }, []); const onBlurValidator = (0, _react.useMemo)(() => { if (onBlurValidatorProp === false) { return undefined; } if (typeof onBlurValidatorProp === 'function') { return (value, args) => { const coreResult = nameValidator(value); if (coreResult instanceof Error) { return coreResult; } return onBlurValidatorProp(value, args); }; } return nameValidator; }, [onBlurValidatorProp, nameValidator]); const StringFieldProps = { trim: true, autoComplete: 'name', minLength: minLengthProp, ...otherProps, onBlurValidator, exportValidators: { nameValidator } }; return _react.default.createElement(_index.default, StringFieldProps); } Name._supportsSpacingProps = true; Name.First = function FirstName(props) { const translations = (0, _useTranslation.default)().FirstName; const errorMessages = (0, _react.useMemo)(() => { return { 'Field.errorRequired': translations.errorRequired, 'Field.errorPattern': translations.errorPattern, ...props.errorMessages }; }, [props.errorMessages, translations.errorPattern, translations.errorRequired]); const nameProps = { label: translations.label, autoComplete: 'given-name', ...props, errorMessages }; return _react.default.createElement(Name, nameProps); }; Name.First['_supportsSpacingProps'] = true; Name.Last = function LastName(props) { const translations = (0, _useTranslation.default)().LastName; const errorMessages = (0, _react.useMemo)(() => { return { 'Field.errorRequired': translations.errorRequired, 'Field.errorPattern': translations.errorPattern, ...props.errorMessages }; }, [props.errorMessages, translations.errorPattern, translations.errorRequired]); const nameProps = { label: translations.label, autoComplete: 'family-name', ...props, errorMessages }; return _react.default.createElement(Name, nameProps); }; Name.Last['_supportsSpacingProps'] = true; Name.Company = function CompanyName(props) { const translations = (0, _useTranslation.default)().CompanyName; const { onBlurValidator: onBlurValidatorProp, minLength: minLengthProp = 3, ...otherProps } = props; const errorMessages = (0, _react.useMemo)(() => { return { 'Field.errorRequired': translations.errorRequired, ...props.errorMessages }; }, [props.errorMessages, translations.errorRequired]); const companyValidator = (0, _react.useCallback)(value => { if (value !== undefined) { if (value === '') { return; } if (!new RegExp(companyPattern, 'u').test(value)) { return new _index2.FormError('Field.errorPattern'); } } }, []); const onBlurValidator = (0, _react.useMemo)(() => { if (onBlurValidatorProp === false) { return undefined; } if (typeof onBlurValidatorProp === 'function') { return (value, args) => { const coreResult = companyValidator(value); if (coreResult instanceof Error) { return coreResult; } return onBlurValidatorProp(value, args); }; } return companyValidator; }, [onBlurValidatorProp, companyValidator]); const StringFieldProps = { label: translations.label, autoComplete: 'organization', minLength: minLengthProp, ...otherProps, errorMessages, onBlurValidator, exportValidators: { companyValidator } }; return _react.default.createElement(_index.default, StringFieldProps); }; Name.Company['_supportsSpacingProps'] = true; var _default = exports.default = Name; //# sourceMappingURL=Name.js.map