UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

184 lines 6.56 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 { pickSpacingProps } from '../../../../components/flex/utils'; import { useFieldProps } from '../../hooks'; import classnames from 'classnames'; import FieldBlock from '../../FieldBlock'; import { MultiInputMask } from '../../../../components/input-masked'; import { useTranslation as useSharedTranslation } from '../../../../shared'; import useTranslation from '../../hooks/useTranslation'; import { FormError } from '../../utils'; function Expiry(props) { const { Date: { errorRequired }, Expiry: { label: expiryLabel } } = useTranslation(); const { DatePicker: { placeholderCharacters: placeholders, month: monthLabel, year: yearLabel } } = useSharedTranslation(); const errorMessages = useMemo(() => _objectSpread({ 'Field.errorRequired': errorRequired }, props.errorMessages), [errorRequired, props.errorMessages]); const handleInput = 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 = useCallback((value, { required, error }) => { return required && !value ? error : undefined; }, []); const monthAndYearValidator = useCallback(value => validateMonthAndYear(value, placeholders), [placeholders]); const validateInitially = useMemo(() => { if (props.validateInitially) { return props.validateInitially; } if (props.value) { return true; } return undefined; }, [props.validateInitially, props.value]); const valueProp = 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 } = useFieldProps(preparedProps); const expiry = useMemo(() => stringToExpiryValue(value), [value]); 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: classnames('dnb-forms-field-expiry', className), label }, pickSpacingProps(props)); return React.createElement(FieldBlock, fieldBlockProps, React.createElement(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 FormError('Expiry.errorMonth', { messageValues: { month: month } })); } if (year.includes(placeholders.year)) { messages.push(new FormError('Expiry.errorYear', { messageValues: { year: year } })); } if (messages.length) { return messages; } } Expiry._supportsEufemiaSpacingProps = true; export default Expiry; //# sourceMappingURL=Expiry.js.map