UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

260 lines (259 loc) 7.79 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 { prioritizedCountries } from "../../constants/countries.js"; import useCountries from "./useCountries.js"; import { useFieldProps } from "../../hooks/index.js"; import FieldBlock from "../../FieldBlock/index.js"; import useTranslation from "../../hooks/useTranslation.js"; function SelectCountry(props) { var _props$width; const sharedContext = useContext(SharedContext); const fieldBlockContext = useContext(FieldBlockContext); const { label: defaultLabel, placeholder: defaultPlaceholder, errorRequired } = useTranslation().SelectCountry; const lang = (sharedContext.locale || LOCALE).split('-')[0]; const { countries } = useCountries(); const getCountryObjectByIso = useCallback(value => { const country = countries.find(({ iso }) => value === iso); if (country?.i18n) { country['name'] = country.i18n[lang]; } return country; }, [countries, lang]); const provideAdditionalArgs = useCallback(value => { const country = getCountryObjectByIso(value); if (country?.iso) { return country; } }, [getCountryObjectByIso]); 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, countries: ccFilter = 'Prioritized', hasError, disabled, size, value, width, noAnimation, autoComplete, htmlAttributes, handleFocus, handleBlur, handleChange, updateValue, setDisplayValue, forceUpdate, filterCountries } = useFieldProps(preparedProps); const dataRef = useRef(null); const langRef = useRef(lang); const wasFilled = useRef(false); const filter = useCallback(country => { return countryFilter(country, filterCountries, ccFilter); }, [ccFilter, filterCountries]); 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 = useCallback(({ data }) => { const newValue = data?.selectedKey; const country = getCountryObjectByIso(newValue); if (country?.iso) { handleChange(country.iso, country); } }, [getCountryObjectByIso, handleChange]); const fillData = useCallback(() => { if (!wasFilled.current) { wasFilled.current = true; dataRef.current = getCountryData({ countries, lang: langRef.current, filter, sort: ccFilter }); forceUpdate(); } }, [ccFilter, countries, 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 country = countries.find(({ i18n }) => Object.values(i18n).some(s => s.toLowerCase().includes(search))); if (country?.iso) { setHidden(); handleChange(country.iso); } } }, [countries, handleChange]); useMemo(() => { if (path || itemPath) { setDisplayValue(getCountryObjectByIso(value)?.i18n?.[langRef.current]); } }, [getCountryObjectByIso, itemPath, path, setDisplayValue, value]); const fieldBlockProps = { forId: id, className: classnames('dnb-forms-field-select-country', 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: 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))); } export 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 }) => { if (sort === 'Prioritized') { const indexA = prioritizedCountries.indexOf(a['en']); const indexB = 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(a[lang])?.localeCompare?.(b[lang], 'nb'); }).map(country => makeObject(country, lang)); if (sortedCountries.length === 0) { return undefined; } return sortedCountries; } export function countryFilter(country, filterCountries, ccFilter) { let result = true; if (ccFilter !== 'Prioritized') { switch (ccFilter) { case 'Scandinavia': case 'Nordic': { result = country.regions?.includes(ccFilter); break; } case 'Europe': { result = country.continent.includes(ccFilter); break; } } } if (result && filterCountries) { result = filterCountries(country); } return result; } export function makeCountryFilterSet(ccFilter) { return country => { switch (ccFilter) { case 'Scandinavia': case 'Nordic': return country.regions?.includes(ccFilter); case 'Europe': return country.continent.includes(ccFilter); } return true; }; } SelectCountry._supportsSpacingProps = true; export default SelectCountry; //# sourceMappingURL=SelectCountry.js.map