UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

286 lines (285 loc) 10.1 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.currencyFilter = currencyFilter; exports.default = void 0; exports.getCurrencyData = getCurrencyData; exports.makeCurrencyFilterSet = makeCurrencyFilterSet; var _includes = _interopRequireDefault(require("core-js-pure/stable/instance/includes.js")); var _react = require("react"); var _clsx = require("clsx"); var _Context = _interopRequireDefault(require("../../../../shared/Context.js")); var _FieldBlockContext = _interopRequireDefault(require("../../FieldBlock/FieldBlockContext.js")); var _defaults = require("../../../../shared/defaults.js"); var _index = require("../../../../components/index.js"); var _utils = require("../../../../components/flex/utils.js"); var _currencies = _interopRequireWildcard(require("../../constants/currencies.js")); var _index2 = require("../../hooks/index.js"); var _index3 = _interopRequireDefault(require("../../FieldBlock/index.js")); var _useTranslation = _interopRequireDefault(require("../../hooks/useTranslation.js")); var _withComponentMarkers = _interopRequireDefault(require("../../../../shared/helpers/withComponentMarkers.js")); var _jsxRuntime = require("react/jsx-runtime"); 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); } function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function SelectCurrency(props) { var _props$width; const sharedContext = (0, _react.useContext)(_Context.default); const fieldBlockContext = (0, _react.useContext)(_FieldBlockContext.default); const { label: defaultLabel, placeholder: defaultPlaceholder, errorRequired } = (0, _useTranslation.default)().SelectCurrency; const lang = (sharedContext.locale || _defaults.LOCALE).split('-')[0]; const getCurrencyObjectByIso = (0, _react.useCallback)(value => { const currency = _currencies.default.find(({ iso }) => value === iso); if (currency?.i18n) { currency['name'] = currency.i18n[lang]; } return currency; }, [lang]); const provideAdditionalArgs = (0, _react.useCallback)(value => { const currency = getCurrencyObjectByIso(value); if (currency?.iso) { return currency; } return undefined; }, [getCurrencyObjectByIso]); const errorMessages = (0, _react.useMemo)(() => ({ required: errorRequired }), [errorRequired]); const preparedProps = { errorMessages, ...props, width: (_props$width = props.width) !== null && _props$width !== void 0 ? _props$width : fieldBlockContext?.composition ? 'stretch' : 'large', provideAdditionalArgs }; const { id, path, itemPath, className, placeholder = defaultPlaceholder, label = defaultLabel, currencies: ccFilter = 'Prioritized', hasError, disabled, size, value, width, noAnimation, autoComplete, htmlAttributes, handleFocus, handleBlur, handleChange, updateValue, setDisplayValue, forceUpdate, filterCurrencies } = (0, _index2.useFieldProps)(preparedProps); const dataRef = (0, _react.useRef)(null); const langRef = (0, _react.useRef)(lang); const wasFilled = (0, _react.useRef)(false); const filter = (0, _react.useCallback)(currency => { return currencyFilter(currency, filterCurrencies, ccFilter); }, [ccFilter, filterCurrencies]); (0, _react.useMemo)(() => { const isLangChange = lang !== langRef.current; if (isLangChange || !wasFilled.current) { langRef.current = lang; dataRef.current = getCurrencyData({ lang, filter: !wasFilled.current ? currency => currency.iso === value : filter, enableSort: ccFilter, enableSearch: true }); if (isLangChange && value && typeof window !== 'undefined') { updateValue(null); window.requestAnimationFrame(() => { updateValue(value); }); } } }, [lang, filter, ccFilter, updateValue, value]); const handleCurrencyChange = (0, _react.useCallback)(event => { const data = event.data; const newValue = data && typeof data === 'object' && 'selectedKey' in data ? data.selectedKey : undefined; const currency = getCurrencyObjectByIso(newValue); if (currency?.iso) { handleChange(currency.iso, currency); } }, [getCurrencyObjectByIso, handleChange]); const fillData = (0, _react.useCallback)(() => { if (!wasFilled.current) { wasFilled.current = true; dataRef.current = getCurrencyData({ lang: langRef.current, filter, enableSort: ccFilter, enableSearch: true }); forceUpdate(); } }, [ccFilter, filter, forceUpdate]); const onFocusHandler = (0, _react.useCallback)(({ updateData }) => { fillData(); updateData(dataRef.current); handleFocus(); }, [fillData, handleFocus]); const onTypeHandler = (0, _react.useCallback)(({ value: currentValue, setHidden, event }) => { if (typeof event?.nativeEvent?.data === 'undefined') { const search = currentValue.toLowerCase(); const currency = _currencies.default.find(({ i18n }) => Object.values(i18n).some(s => { var _context; return (0, _includes.default)(_context = s.toLowerCase()).call(_context, search); })); if (currency?.iso) { setHidden(); handleChange(currency.iso); } } }, [handleChange]); (0, _react.useMemo)(() => { if (path || itemPath) { setDisplayValue(getCurrencyObjectByIso(value)?.i18n?.[langRef.current]); } }, [getCurrencyObjectByIso, itemPath, path, setDisplayValue, value]); const fieldBlockProps = { forId: id, className: (0, _clsx.clsx)('dnb-forms-field-select-currency', className), label, width: width === 'stretch' || fieldBlockContext?.composition ? width : undefined, contentWidth: width !== false ? width : undefined, ...(0, _utils.pickSpacingProps)(props) }; return (0, _jsxRuntime.jsx)(_index3.default, { ...fieldBlockProps, children: (0, _jsxRuntime.jsx)(_index.Autocomplete, { id: id, placeholder: placeholder, icon: false, data: dataRef.current, value: typeof value === 'string' ? value : null, disabled: disabled, size: size, onOpen: fillData, onFocus: onFocusHandler, onBlur: handleBlur, onChange: handleCurrencyChange, onType: onTypeHandler, stretch: true, selectAll: true, status: hasError ? 'error' : undefined, showSubmitButton: true, keepSelection: true, autoComplete: autoComplete, noAnimation: noAnimation, ...htmlAttributes }) }); } function getCurrencyData({ lang = 'nb', filter = null, enableSort = null, enableSearch = null, makeObject = (currency, lang) => { var _currency$i18n$lang; const translation = (_currency$i18n$lang = currency.i18n[lang]) !== null && _currency$i18n$lang !== void 0 ? _currency$i18n$lang : currency.i18n.en; const content = [translation, currency.iso]; const searchContent = enableSearch ? [translation, currency.iso, ...(currency.search?.[lang] || [])] : undefined; return { selectedKey: currency.iso, selectedValue: `${translation} (${currency.iso})`, searchContent, content }; } } = {}) { const sortedCurrencies = _currencies.default.filter(currency => { if (typeof filter === 'function') { return filter(currency); } return !filter; }).sort(({ i18n: a, iso: a1 }, { i18n: b, iso: b1 }) => { if (enableSort === 'Prioritized') { const indexA = _currencies.prioritizedCurrencies.indexOf(a1); const indexB = _currencies.prioritizedCurrencies.indexOf(b1); const priorityA = indexA !== -1; const priorityB = indexB !== -1; if (priorityA && priorityB) { return indexA - indexB; } else if (priorityA) { return -1; } else if (priorityB) { return 1; } } return String(a[lang])?.localeCompare?.(b[lang], 'nb'); }).map(currency => makeObject(currency, lang)); if (sortedCurrencies.length === 0) { return undefined; } return sortedCurrencies; } function currencyFilter(currency, filterCurrencies, ccFilter) { let result = true; if (ccFilter !== 'Prioritized') { switch (ccFilter) { case 'Scandinavia': case 'Nordic': { var _context2; result = ((_context2 = currency.regions) == null ? void 0 : Function.call.bind((0, _includes.default)(_context2), _context2))?.(ccFilter); break; } case 'Europe': { var _context3; result = (0, _includes.default)(_context3 = currency.continent).call(_context3, ccFilter); break; } } } if (result && filterCurrencies) { result = filterCurrencies(currency); } return result; } function makeCurrencyFilterSet(ccFilter) { return currency => { var _context4, _context5; switch (ccFilter) { case 'Scandinavia': case 'Nordic': return ((_context4 = currency.regions) == null ? void 0 : Function.call.bind((0, _includes.default)(_context4), _context4))?.(ccFilter); case 'Europe': return (0, _includes.default)(_context5 = currency.continent).call(_context5, ccFilter); } return true; }; } (0, _withComponentMarkers.default)(SelectCurrency, { _supportsSpacingProps: true }); var _default = exports.default = SelectCurrency; //# sourceMappingURL=SelectCurrency.js.map