@turbo-red/strapi-plugin-lookup-enum
Version:
Like enum field, but options are extracted from a field provided by a single-type content.
114 lines (113 loc) • 3.62 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const designSystem = require("@strapi/design-system");
const admin = require("@strapi/strapi/admin");
const react = require("react");
const reactIntl = require("react-intl");
function isMessageDescriptor(message) {
return message?.id !== void 0;
}
const makeDynamicEnum = (multiSelect) => (props) => {
const {
value,
onChange,
name,
label,
required,
hint,
attribute,
placeholder,
disabled,
type,
error
} = props;
const configuration = attribute.options;
const { formatMessage } = reactIntl.useIntl();
const [isLoading, setIsLoading] = react.useState(false);
const [options, setOptions] = react.useState([]);
const [optionsLoadingError, setLoadingError] = react.useState();
const translate = (message) => isMessageDescriptor(message) ? formatMessage(message) : message;
const loadEnumOptions = async () => {
if (isLoading) return;
setIsLoading(true);
try {
const { get } = admin.getFetchClient();
const res = await get(
`/content-manager/single-types/${configuration.lookupSingleType}`
);
if (!res.status || res.status < 400) {
const optionsString = res.data.data[configuration.lookupField];
if (optionsString) {
setOptions(optionsString.match(/\S+/g) || []);
}
} else {
setLoadingError(`Error, code: ${res.status}`);
}
} catch (err) {
setLoadingError(err?.message || err?.toString());
} finally {
setIsLoading(false);
}
};
react.useEffect(() => {
loadEnumOptions();
}, []);
const handleSelectChange = (value2) => onChange({
target: { name, type, value: value2 }
});
const clear = (event) => {
event.stopPropagation();
event.preventDefault();
onChange({
target: { name, type }
});
};
const selectOptionsList = options.map((opt) => {
return multiSelect ? /* @__PURE__ */ jsxRuntime.jsx(designSystem.MultiSelectOption, { value: opt, children: opt }, opt) : /* @__PURE__ */ jsxRuntime.jsx(designSystem.SingleSelectOption, { value: opt, children: opt }, opt);
});
return /* @__PURE__ */ jsxRuntime.jsxs(
designSystem.Field.Root,
{
name,
id: name,
hint: translate(hint),
error: translate(error || optionsLoadingError),
required,
children: [
/* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Label, { children: translate(label) }),
multiSelect ? /* @__PURE__ */ jsxRuntime.jsx(
designSystem.MultiSelect,
{
placeholder: translate(placeholder),
name,
onChange: (value2) => handleSelectChange(value2),
value,
disabled,
required,
onClear: clear,
withTags: true,
children: selectOptionsList
}
) : /* @__PURE__ */ jsxRuntime.jsx(
designSystem.SingleSelect,
{
placeholder: translate(placeholder),
name,
onChange: (value2) => handleSelectChange(`${value2}`),
value,
disabled,
required,
onClear: clear,
children: selectOptionsList
}
),
/* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Hint, {}),
/* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Error, {})
]
}
);
};
exports.default = makeDynamicEnum;
exports.makeDynamicEnum = makeDynamicEnum;
//# sourceMappingURL=DynamicEnum-CahF3saR.js.map