@open-formulieren/formio-builder
Version:
An opinionated Formio webform builder for Open Forms
39 lines (38 loc) • 2.34 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const formik_1 = require("formik");
const react_1 = require("react");
const react_intl_1 = require("react-intl");
const useAsync_1 = __importDefault(require("react-use/esm/useAsync"));
const select_1 = __importDefault(require("../../../../components/formio/select"));
const context_1 = require("../../../../context");
/**
* Fetch the available Reference lists services and display them in a Select
*
* The selected service is used at runtime to retrieve options to populate a Select
*
* This requires an async function `getServices` to be provided to the
* BuilderContext which is responsible for retrieving the list of available plugins.
*
* If a fetch error occurs, it is thrown during rendering - you should provide your
* own error boundary to catch this.
*/
const ReferenceListsServiceSelect = () => {
const intl = (0, react_intl_1.useIntl)();
const { values, setFieldValue } = (0, formik_1.useFormikContext)();
const { getServices } = (0, react_1.useContext)(context_1.BuilderContext);
const { value: options = [], loading } = (0, useAsync_1.default)(async () => {
var _a;
const options = await getServices('referenceLists');
if (options.length === 1 && !((_a = values === null || values === void 0 ? void 0 : values.openForms) === null || _a === void 0 ? void 0 : _a.service)) {
setFieldValue('openForms.service', options[0].slug);
}
return options;
}, [getServices, setFieldValue]); // values is deliberately excluded from the dependency array
return ((0, jsx_runtime_1.jsx)(select_1.default, { name: "openForms.service", label: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: 'gSSyoc', defaultMessage: [{ type: 0, value: "Reference lists service" }] }), tooltip: intl.formatMessage({ id: "u5fu3R", defaultMessage: [{ type: 0, value: "The identifier of the reference lists service from which the options will be retrieved." }] }), isLoading: loading, valueProperty: "slug", options: options, required: true }));
};
exports.default = ReferenceListsServiceSelect;