@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
267 lines (266 loc) • 9.3 kB
JavaScript
"use client";
import _extends from "@babel/runtime-corejs3/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 !== 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 = {
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,
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 === 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,
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
}) => {
var _event$nativeEvent;
if (typeof (event === null || event === 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(({
i18n
}) => 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;
setDisplayValue((_getCurrencyObjectByI = getCurrencyObjectByIso(value)) === null || _getCurrencyObjectByI === void 0 || (_getCurrencyObjectByI = _getCurrencyObjectByI.i18n) === null || _getCurrencyObjectByI === void 0 ? void 0 : _getCurrencyObjectByI[langRef.current]);
}
}, [getCurrencyObjectByIso, itemPath, path, setDisplayValue, value]);
const fieldBlockProps = {
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,
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, _currency$search;
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 = currency.search) === null || _currency$search === void 0 ? void 0 : _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
}) => {
var _String, _String$localeCompare;
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 = String(a[lang])) === null || _String === 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