UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

275 lines (274 loc) 10.1 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; 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, useContext, useMemo, useRef } from 'react'; import classnames from 'classnames'; import SharedContext from '../../../../shared/Context'; import FieldBlockContext from '../../FieldBlock/FieldBlockContext'; import { LOCALE } from '../../../../shared/defaults'; import { Autocomplete } from '../../../../components'; import { pickSpacingProps } from '../../../../components/flex/utils'; import currencies, { prioritizedCurrencies } from '../../constants/currencies'; import { useFieldProps } from '../../hooks'; import FieldBlock from '../../FieldBlock'; import useTranslation from '../../hooks/useTranslation'; function SelectCurrency(props) { var _props$width; const sharedContext = useContext(SharedContext); const fieldBlockContext = useContext(FieldBlockContext); const { label: defaultLabel, placeholder: defaultPlaceholder, errorRequired } = useTranslation().SelectCurrency; const lang = (sharedContext.locale || LOCALE).split('-')[0]; const getCurrencyObjectByIso = useCallback(value => { const currency = currencies.find(_ref => { let { iso } = _ref; return value === iso; }); if (currency !== null && currency !== void 0 && currency.i18n) { currency['name'] = currency.i18n[lang]; } return currency; }, [lang]); const provideAdditionalArgs = useCallback(value => { const currency = getCurrencyObjectByIso(value); if (currency !== null && currency !== void 0 && currency.iso) { return currency; } }, [getCurrencyObjectByIso]); const errorMessages = useMemo(() => ({ required: errorRequired }), [errorRequired]); const preparedProps = _objectSpread(_objectSpread({ errorMessages }, props), {}, { width: (_props$width = props.width) !== null && _props$width !== void 0 ? _props$width : fieldBlockContext !== null && fieldBlockContext !== void 0 && 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 } = useFieldProps(preparedProps); const dataRef = useRef(null); const langRef = useRef(lang); const wasFilled = useRef(false); const filter = useCallback(currency => { return currencyFilter(currency, filterCurrencies, ccFilter); }, [ccFilter, filterCurrencies]); 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, sort: ccFilter }); if (isLangChange && value && typeof window !== 'undefined') { updateValue(null); window.requestAnimationFrame(() => { updateValue(value); }); } } }, [lang, filter, ccFilter, updateValue, value]); const handleCurrencyChange = useCallback(_ref2 => { let { data } = _ref2; const newValue = data === null || data === void 0 ? void 0 : data.selectedKey; const currency = getCurrencyObjectByIso(newValue); if (currency !== null && currency !== void 0 && currency.iso) { handleChange(currency.iso, currency); } }, [getCurrencyObjectByIso, handleChange]); const fillData = useCallback(() => { if (!wasFilled.current) { wasFilled.current = true; dataRef.current = getCurrencyData({ lang: langRef.current, filter, sort: ccFilter }); forceUpdate(); } }, [ccFilter, filter, forceUpdate]); const onFocusHandler = useCallback(_ref3 => { let { updateData } = _ref3; fillData(); updateData(dataRef.current); handleFocus(); }, [fillData, handleFocus]); const onTypeHandler = useCallback(_ref4 => { var _event$nativeEvent; let { value: currentValue, setHidden, event } = _ref4; if (typeof (event === null || event === void 0 ? void 0 : (_event$nativeEvent = event.nativeEvent) === null || _event$nativeEvent === void 0 ? void 0 : _event$nativeEvent.data) === 'undefined') { const search = currentValue.toLowerCase(); const currency = currencies.find(_ref5 => { let { i18n } = _ref5; return Object.values(i18n).some(s => s.toLowerCase().includes(search)); }); if (currency !== null && currency !== void 0 && currency.iso) { setHidden(); handleChange(currency.iso); } } }, [handleChange]); useMemo(() => { if (path || itemPath) { var _getCurrencyObjectByI, _getCurrencyObjectByI2; setDisplayValue((_getCurrencyObjectByI = getCurrencyObjectByIso(value)) === null || _getCurrencyObjectByI === void 0 ? void 0 : (_getCurrencyObjectByI2 = _getCurrencyObjectByI.i18n) === null || _getCurrencyObjectByI2 === void 0 ? void 0 : _getCurrencyObjectByI2[langRef.current]); } }, [getCurrencyObjectByIso, itemPath, path, setDisplayValue, value]); const fieldBlockProps = _objectSpread({ forId: id, className: classnames('dnb-forms-field-select-currency', className), label, width: width === 'stretch' || fieldBlockContext !== null && fieldBlockContext !== void 0 && fieldBlockContext.composition ? width : undefined, contentWidth: width !== false ? width : undefined }, pickSpacingProps(props)); return React.createElement(FieldBlock, fieldBlockProps, React.createElement(Autocomplete, _extends({ id: id, placeholder: placeholder, input_icon: false, data: dataRef.current, value: typeof value === 'string' ? value : null, disabled: disabled, size: size, on_show: fillData, on_focus: onFocusHandler, on_blur: handleBlur, on_change: handleCurrencyChange, on_type: onTypeHandler, stretch: true, status: hasError ? 'error' : undefined, show_submit_button: true, keep_selection: true, autoComplete: autoComplete, no_animation: noAnimation }, htmlAttributes))); } export function getCurrencyData() { let { lang = 'nb', filter = null, sort = 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]; return { selectedKey: currency.iso, selected_value: `${translation} (${currency.iso})`, content }; } } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; const sortedCurrencies = currencies.filter(currency => { if (typeof filter === 'function') { return filter(currency); } return !filter; }).sort((_ref6, _ref7) => { var _String, _String$localeCompare; let { i18n: a, iso: a1 } = _ref6; let { i18n: b, iso: b1 } = _ref7; if (sort === 'Prioritized') { const indexA = prioritizedCurrencies.indexOf(a1); const indexB = 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 = String(a[lang])) === null || _String === void 0 ? void 0 : (_String$localeCompare = _String.localeCompare) === null || _String$localeCompare === void 0 ? void 0 : _String$localeCompare.call(_String, b[lang], 'nb'); }).map(currency => makeObject(currency, lang)); if (sortedCurrencies.length === 0) { return undefined; } return sortedCurrencies; } export function currencyFilter(currency, filterCurrencies, ccFilter) { let result = true; if (ccFilter !== 'Prioritized') { switch (ccFilter) { case 'Scandinavia': case 'Nordic': { var _currency$regions; result = (_currency$regions = currency.regions) === null || _currency$regions === void 0 ? void 0 : _currency$regions.includes(ccFilter); break; } case 'Europe': { result = currency.continent.includes(ccFilter); break; } } } if (result && filterCurrencies) { result = filterCurrencies(currency); } return result; } export function makeCurrencyFilterSet(ccFilter) { return currency => { var _currency$regions2; switch (ccFilter) { case 'Scandinavia': case 'Nordic': return (_currency$regions2 = currency.regions) === null || _currency$regions2 === void 0 ? void 0 : _currency$regions2.includes(ccFilter); case 'Europe': return currency.continent.includes(ccFilter); } return true; }; } SelectCurrency._supportsSpacingProps = true; export default SelectCurrency; //# sourceMappingURL=SelectCurrency.js.map