@open-formulieren/formio-builder
Version:
An opinionated Formio webform builder for Open Forms
34 lines (33 loc) • 1.96 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { useFormikContext } from 'formik';
import { useContext } from 'react';
import { FormattedMessage, useIntl } from 'react-intl';
import useAsync from 'react-use/esm/useAsync';
import Select from '../../../../components/formio/select';
import { BuilderContext } from '../../../../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 = useIntl();
const { values, setFieldValue } = useFormikContext();
const { getServices } = useContext(BuilderContext);
const { value: options = [], loading } = useAsync(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 (_jsx(Select, { name: "openForms.service", label: _jsx(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 }));
};
export default ReferenceListsServiceSelect;