@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
197 lines (196 loc) • 8.41 kB
JavaScript
;
require("core-js/modules/web.dom-collections.iterator.js");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _utils = require("../../../../components/flex/utils");
var _hooks = require("../../hooks");
var _classnames = _interopRequireDefault(require("classnames"));
var _FieldBlock = _interopRequireDefault(require("../../FieldBlock"));
var _inputMasked = require("../../../../components/input-masked");
var _shared = require("../../../../shared");
var _useTranslation = _interopRequireDefault(require("../../hooks/useTranslation"));
var _utils2 = require("../../utils");
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 Expiry(props) {
const {
Date: {
errorRequired
},
Expiry: {
label: expiryLabel
}
} = (0, _useTranslation.default)();
const {
DatePicker: {
placeholderCharacters: placeholders,
month: monthLabel,
year: yearLabel
}
} = (0, _shared.useTranslation)();
const errorMessages = (0, _react.useMemo)(() => _objectSpread({
'Field.errorRequired': errorRequired
}, props.errorMessages), [errorRequired, props.errorMessages]);
const handleInput = (0, _react.useCallback)(values => {
const month = expiryValueToString(values.month, placeholders.month);
const year = expiryValueToString(values.year, placeholders.year);
if (isFieldEmpty(month, placeholders.month) && isFieldEmpty(year, placeholders.year)) {
return '';
}
return `${month}${year}`;
}, [placeholders.month, placeholders.year]);
const validateRequired = (0, _react.useCallback)((value, _ref) => {
let {
required,
error
} = _ref;
return required && !value ? error : undefined;
}, []);
const monthAndYearValidator = (0, _react.useCallback)(value => validateMonthAndYear(value, placeholders), [placeholders]);
const validateInitially = (0, _react.useMemo)(() => {
if (props.validateInitially) {
return props.validateInitially;
}
if (props.value) {
return true;
}
return undefined;
}, [props.validateInitially, props.value]);
const valueProp = (0, _react.useMemo)(() => {
var _props$value;
const {
month,
year
} = stringToExpiryValue((_props$value = props.value) !== null && _props$value !== void 0 ? _props$value : props.defaultValue);
const monthString = expiryValueToString(month, placeholders.month);
const yearString = expiryValueToString(year, placeholders.year);
if (isFieldEmpty(monthString, placeholders.month) && isFieldEmpty(yearString, placeholders.year)) {
return '';
}
return `${monthString}${yearString}`;
}, [props.value, props.defaultValue, placeholders.month, placeholders.year]);
const preparedProps = _objectSpread(_objectSpread({}, props), {}, {
errorMessages,
value: valueProp,
fromInput: handleInput,
validateRequired,
validateInitially: validateInitially,
onBlurValidator: monthAndYearValidator
});
const {
id,
path,
itemPath,
className,
label = expiryLabel,
hasError,
info,
warning,
disabled,
size,
value = '',
htmlAttributes,
handleFocus,
handleBlur,
handleChange,
setDisplayValue
} = (0, _hooks.useFieldProps)(preparedProps);
const expiry = (0, _react.useMemo)(() => stringToExpiryValue(value), [value]);
(0, _react.useMemo)(() => {
if ((path || itemPath) && expiry.month && expiry.year) {
setDisplayValue(`${expiry.month}/${expiry.year}`);
}
}, [expiry.month, expiry.year, itemPath, path, setDisplayValue]);
const status = hasError ? 'error' : warning ? 'warn' : info ? 'info' : null;
const fieldBlockProps = _objectSpread({
id,
forId: `${id}-input-month`,
className: (0, _classnames.default)('dnb-forms-field-expiry', className),
label
}, (0, _utils.pickSpacingProps)(props));
return _react.default.createElement(_FieldBlock.default, fieldBlockProps, _react.default.createElement(_inputMasked.MultiInputMask, {
stretch: true,
id: `${id}-input`,
values: expiry,
status: status,
statusState: disabled ? 'disabled' : undefined,
disabled: disabled,
size: size,
onChange: handleChange,
onBlur: handleBlur,
onFocus: handleFocus,
delimiter: "/",
inputMode: "numeric",
inputs: [_objectSpread({
id: 'month',
label: monthLabel,
mask: [/[0-9]/, /[0-9]/],
placeholderCharacter: placeholders['month'],
autoComplete: 'cc-exp-month'
}, htmlAttributes), _objectSpread({
id: 'year',
label: yearLabel,
mask: [/[0-9]/, /[0-9]/],
placeholderCharacter: placeholders['year'],
autoComplete: 'cc-exp-year'
}, htmlAttributes)]
}));
}
function isFieldEmpty(value, placeholder) {
return value === `${placeholder}${placeholder}`;
}
function stringToExpiryValue(value) {
var _value$substring, _value$substring2;
const month = (_value$substring = value === null || value === void 0 ? void 0 : value.substring(0, 2)) !== null && _value$substring !== void 0 ? _value$substring : '';
const year = (_value$substring2 = value === null || value === void 0 ? void 0 : value.substring(2, 4)) !== null && _value$substring2 !== void 0 ? _value$substring2 : '';
return {
month,
year
};
}
function expiryValueToString(value, placeholder) {
if (!value) {
return `${placeholder}${placeholder}`;
}
if (value.length === 1) {
return `${value}${placeholder}`;
}
return value;
}
function validateMonthAndYear(date, placeholders) {
const {
month,
year
} = stringToExpiryValue(date);
const monthNumber = Number(month);
const messages = [];
if (month.includes(placeholders.month) || monthNumber < 1 || monthNumber > 12) {
messages.push(new _utils2.FormError('Expiry.errorMonth', {
messageValues: {
month: month
}
}));
}
if (year.includes(placeholders.year)) {
messages.push(new _utils2.FormError('Expiry.errorYear', {
messageValues: {
year: year
}
}));
}
if (messages.length) {
return messages;
}
}
Expiry._supportsEufemiaSpacingProps = true;
var _default = exports.default = Expiry;
//# sourceMappingURL=Expiry.js.map