strapi-plugin-country-select
Version:
A strapi custom field for selecting any country based on the ISO 3166-1 country code standard.
58 lines (57 loc) • 2 kB
JavaScript
import { jsx, jsxs } from "react/jsx-runtime";
import React from "react";
import { Field, Flex, Combobox, ComboboxOption } from "@strapi/design-system";
import { useIntl } from "react-intl";
import { g as getTranslation } from "./index-BDgiM56V.mjs";
const CountrySelect = React.forwardRef(({
value,
onChange,
name,
label,
labelAction,
required,
hint,
placeholder,
disabled,
error,
initialValue
}, forwardedRef) => {
const { formatMessage, messages } = useIntl();
const countries = messages[getTranslation("countries")] || "{}";
const parsedOptions = JSON.parse(countries);
const isValidValue = !value || parsedOptions.hasOwnProperty(value);
console.log(value);
return /* @__PURE__ */ jsx(
Field.Root,
{
name,
id: name,
error: !isValidValue ? formatMessage({ id: getTranslation("unsupported-country-code") }, { countryCode: value }) : error,
required,
hint,
children: /* @__PURE__ */ jsxs(Flex, { direction: "column", alignItems: "stretch", gap: 1, children: [
/* @__PURE__ */ jsx(Field.Label, { action: labelAction, children: label }),
/* @__PURE__ */ jsx(
Combobox,
{
ref: forwardedRef,
placeholder,
"aria-label": label,
"aria-disabled": disabled,
disabled,
value: isValidValue ? value : null,
onChange: (countryCode) => onChange(name, parsedOptions.hasOwnProperty(countryCode) ? countryCode : null),
onClear: () => onChange(name, null),
children: Object.entries(parsedOptions).sort(([, n1], [, n2]) => n1.localeCompare(n2)).map(([countryCode, countryName]) => /* @__PURE__ */ jsx(ComboboxOption, { value: countryCode, children: countryName }, countryCode))
}
),
/* @__PURE__ */ jsx(Field.Hint, {}),
/* @__PURE__ */ jsx(Field.Error, {})
] })
}
);
});
export {
CountrySelect as default
};
//# sourceMappingURL=index-awPr-TEw.mjs.map