@open-formulieren/formio-builder
Version:
An opinionated Formio webform builder for Open Forms
52 lines (51 loc) • 2.58 kB
JavaScript
"use strict";
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 usePrevious_1 = __importDefault(require("react-use/esm/usePrevious"));
const select_1 = __importDefault(require("../../../components/formio/select"));
const context_1 = require("../../../context");
function isAttributeOptions(options) {
return options !== undefined;
}
/**
* Fetch the available validator plugins and display them in a multiselect.
*
* This requires an async function `getPrefillAttributes` 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 PrefillAttributeSelect = () => {
const fieldName = 'prefill.attribute';
const intl = (0, react_intl_1.useIntl)();
const { values, setFieldValue } = (0, formik_1.useFormikContext)();
const [{ value: attribute }] = (0, formik_1.useField)('prefill.attribute');
const { getPrefillAttributes } = (0, react_1.useContext)(context_1.BuilderContext);
const { plugin } = values.prefill;
const previousPlugin = (0, usePrevious_1.default)(plugin);
const { value: options, loading, error, } = (0, useAsync_1.default)(async () => {
if (!plugin) {
setFieldValue(fieldName, '');
return [];
}
if (attribute && previousPlugin && plugin !== previousPlugin) {
setFieldValue(fieldName, '');
}
return await getPrefillAttributes(plugin);
}, [plugin]);
if (error) {
throw error;
}
const tooltip = intl.formatMessage({ id: "vzhWgR", defaultMessage: [{ type: 0, value: "Specify the attribute holding the pre-fill data." }] });
const _options = isAttributeOptions(options) ? options : [];
return ((0, jsx_runtime_1.jsx)(select_1.default, { name: fieldName, label: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: 'sgmcmf', defaultMessage: [{ type: 0, value: "Plugin attribute" }] }), tooltip: tooltip, isLoading: loading, isClearable: true, options: _options, valueProperty: "id", emptyValue: "" }));
};
exports.default = PrefillAttributeSelect;