UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

262 lines (261 loc) 8 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import React, { useCallback, useContext, useMemo, useRef } from 'react'; import classnames from 'classnames'; import SharedContext from "../../../../shared/Context.js"; import FieldBlockContext from "../../FieldBlock/FieldBlockContext.js"; import { LOCALE } from "../../../../shared/defaults.js"; import { Autocomplete } from "../../../../components/index.js"; import { pickSpacingProps } from "../../../../components/flex/utils.js"; import currencies, { prioritizedCurrencies } from "../../constants/currencies.js"; import { useFieldProps } from "../../hooks/index.js"; import FieldBlock from "../../FieldBlock/index.js"; import useTranslation from "../../hooks/useTranslation.js"; 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(({ iso }) => value === iso); if (currency?.i18n) { currency['name'] = currency.i18n[lang]; } return currency; }, [lang]); const provideAdditionalArgs = useCallback(value => { const currency = getCurrencyObjectByIso(value); if (currency?.iso) { return currency; } }, [getCurrencyObjectByIso]); const errorMessages = 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 } = 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, enableSort: ccFilter, enableSearch: true }); if (isLangChange && value && typeof window !== 'undefined') { updateValue(null); window.requestAnimationFrame(() => { updateValue(value); }); } } }, [lang, filter, ccFilter, updateValue, value]); const handleCurrencyChange = useCallback(({ data }) => { const newValue = data?.selectedKey; const currency = getCurrencyObjectByIso(newValue); if (currency?.iso) { handleChange(currency.iso, currency); } }, [getCurrencyObjectByIso, handleChange]); const fillData = useCallback(() => { if (!wasFilled.current) { wasFilled.current = true; dataRef.current = getCurrencyData({ lang: langRef.current, filter, enableSort: ccFilter, enableSearch: true }); forceUpdate(); } }, [ccFilter, filter, forceUpdate]); const onFocusHandler = useCallback(({ updateData }) => { fillData(); updateData(dataRef.current); handleFocus(); }, [fillData, handleFocus]); const onTypeHandler = useCallback(({ value: currentValue, setHidden, event }) => { if (typeof event?.nativeEvent?.data === 'undefined') { const search = currentValue.toLowerCase(); const currency = currencies.find(({ i18n }) => Object.values(i18n).some(s => s.toLowerCase().includes(search))); if (currency?.iso) { setHidden(); handleChange(currency.iso); } } }, [handleChange]); useMemo(() => { if (path || itemPath) { setDisplayValue(getCurrencyObjectByIso(value)?.i18n?.[langRef.current]); } }, [getCurrencyObjectByIso, itemPath, path, setDisplayValue, value]); const fieldBlockProps = { forId: id, className: classnames('dnb-forms-field-select-currency', className), label, width: width === 'stretch' || 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, selectall: true, status: hasError ? 'error' : undefined, show_submit_button: true, keep_selection: true, autoComplete: autoComplete, no_animation: noAnimation }, htmlAttributes))); } export 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 search_content = enableSearch ? [translation, currency.iso, ...(currency.search?.[lang] || [])] : undefined; return { selectedKey: currency.iso, selected_value: `${translation} (${currency.iso})`, search_content, content }; } } = {}) { const sortedCurrencies = currencies.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 = 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(a[lang])?.localeCompare?.(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': { result = 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 => { switch (ccFilter) { case 'Scandinavia': case 'Nordic': return currency.regions?.includes(ccFilter); case 'Europe': return currency.continent.includes(ccFilter); } return true; }; } SelectCurrency._supportsSpacingProps = true; export default SelectCurrency; //# sourceMappingURL=SelectCurrency.js.map