UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

275 lines (274 loc) 10.5 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.countryFilter = countryFilter; exports.default = void 0; exports.getCountryData = getCountryData; exports.makeCountryFilterSet = makeCountryFilterSet; var _react = _interopRequireWildcard(require("react")); var _classnames = _interopRequireDefault(require("classnames")); 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 _countries = require("../../constants/countries.js"); var _useCountries = _interopRequireDefault(require("./useCountries.js")); var _index2 = require("../../hooks/index.js"); var _index3 = _interopRequireDefault(require("../../FieldBlock/index.js")); var _useTranslation = _interopRequireDefault(require("../../hooks/useTranslation.js")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } 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 _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function SelectCountry(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)().SelectCountry; const lang = (sharedContext.locale || _defaults.LOCALE).split('-')[0]; const { countries } = (0, _useCountries.default)(); const getCountryObjectByIso = (0, _react.useCallback)(value => { const country = countries.find(({ iso }) => value === iso); if (country !== null && country !== void 0 && country.i18n) { country['name'] = country.i18n[lang]; } return country; }, [countries, lang]); const provideAdditionalArgs = (0, _react.useCallback)(value => { const country = getCountryObjectByIso(value); if (country !== null && country !== void 0 && country.iso) { return country; } }, [getCountryObjectByIso]); 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 !== null && fieldBlockContext !== void 0 && fieldBlockContext.composition ? 'stretch' : 'large', provideAdditionalArgs }; const { id, path, itemPath, className, placeholder = defaultPlaceholder, label = defaultLabel, countries: ccFilter = 'Prioritized', hasError, disabled, size, value, width, noAnimation, autoComplete, htmlAttributes, handleFocus, handleBlur, handleChange, updateValue, setDisplayValue, forceUpdate, filterCountries } = (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)(country => { return countryFilter(country, filterCountries, ccFilter); }, [ccFilter, filterCountries]); (0, _react.useMemo)(() => { const isLangChange = lang !== langRef.current; if (isLangChange || !wasFilled.current) { langRef.current = lang; dataRef.current = getCountryData({ countries, lang, filter: !wasFilled.current ? country => country.iso === value : filter, sort: ccFilter }); if (isLangChange && value && typeof window !== 'undefined') { updateValue(null); window.requestAnimationFrame(() => { updateValue(value); }); } } }, [lang, countries, filter, ccFilter, value, updateValue]); const handleCountryChange = (0, _react.useCallback)(({ data }) => { const newValue = data === null || data === void 0 ? void 0 : data.selectedKey; const country = getCountryObjectByIso(newValue); if (country !== null && country !== void 0 && country.iso) { handleChange(country.iso, country); } }, [getCountryObjectByIso, handleChange]); const fillData = (0, _react.useCallback)(() => { if (!wasFilled.current) { wasFilled.current = true; dataRef.current = getCountryData({ countries, lang: langRef.current, filter, sort: ccFilter }); forceUpdate(); } }, [ccFilter, countries, filter, forceUpdate]); const onFocusHandler = (0, _react.useCallback)(({ updateData }) => { fillData(); updateData(dataRef.current); handleFocus(); }, [fillData, handleFocus]); const onTypeHandler = (0, _react.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 country = countries.find(({ i18n }) => Object.values(i18n).some(s => s.toLowerCase().includes(search))); if (country !== null && country !== void 0 && country.iso) { setHidden(); handleChange(country.iso); } } }, [countries, handleChange]); (0, _react.useMemo)(() => { if (path || itemPath) { var _getCountryObjectByIs; setDisplayValue((_getCountryObjectByIs = getCountryObjectByIso(value)) === null || _getCountryObjectByIs === void 0 || (_getCountryObjectByIs = _getCountryObjectByIs.i18n) === null || _getCountryObjectByIs === void 0 ? void 0 : _getCountryObjectByIs[langRef.current]); } }, [getCountryObjectByIso, itemPath, path, setDisplayValue, value]); const fieldBlockProps = { forId: id, className: (0, _classnames.default)('dnb-forms-field-select-country', className), label, width: width === 'stretch' || fieldBlockContext !== null && fieldBlockContext !== void 0 && fieldBlockContext.composition ? width : undefined, contentWidth: width !== false ? width : undefined, ...(0, _utils.pickSpacingProps)(props) }; return _react.default.createElement(_index3.default, fieldBlockProps, _react.default.createElement(_index.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: handleCountryChange, on_type: onTypeHandler, stretch: true, selectall: true, status: hasError ? 'error' : undefined, show_submit_button: true, keep_selection: true, autoComplete: autoComplete !== null && autoComplete !== void 0 ? autoComplete : 'country-name', no_animation: noAnimation }, htmlAttributes))); } function getCountryData({ countries, lang = 'nb', filter = null, sort = null, makeObject = (country, lang) => { var _country$i18n$lang; const content = (_country$i18n$lang = country.i18n[lang]) !== null && _country$i18n$lang !== void 0 ? _country$i18n$lang : country.i18n.en; return { selectedKey: country.iso, content }; } }) { const sortedCountries = countries.filter(country => { if (typeof filter === 'function') { return filter(country); } return !filter; }).sort(({ i18n: a }, { i18n: b }) => { var _String, _String$localeCompare; if (sort === 'Prioritized') { const indexA = _countries.prioritizedCountries.indexOf(a['en']); const indexB = _countries.prioritizedCountries.indexOf(b['en']); 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(country => makeObject(country, lang)); if (sortedCountries.length === 0) { return undefined; } return sortedCountries; } function countryFilter(country, filterCountries, ccFilter) { let result = true; if (ccFilter !== 'Prioritized') { switch (ccFilter) { case 'Scandinavia': case 'Nordic': { var _country$regions; result = (_country$regions = country.regions) === null || _country$regions === void 0 ? void 0 : _country$regions.includes(ccFilter); break; } case 'Europe': { result = country.continent.includes(ccFilter); break; } } } if (result && filterCountries) { result = filterCountries(country); } return result; } function makeCountryFilterSet(ccFilter) { return country => { var _country$regions2; switch (ccFilter) { case 'Scandinavia': case 'Nordic': return (_country$regions2 = country.regions) === null || _country$regions2 === void 0 ? void 0 : _country$regions2.includes(ccFilter); case 'Europe': return country.continent.includes(ccFilter); } return true; }; } SelectCountry._supportsSpacingProps = true; var _default = exports.default = SelectCountry; //# sourceMappingURL=SelectCountry.js.map