@indec/form-builder
Version:
Form builder
117 lines (116 loc) • 6.94 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _formik = require("formik");
var _SubQuestions = _interopRequireDefault(require("../SubQuestions"));
var _TextField = _interopRequireDefault(require("../TextField"));
var _buildQuestions = require("../../utils/buildQuestions");
var _getLastId = _interopRequireDefault(require("../../utils/getLastId"));
var _Icons = require("../Icons");
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["isMultiple", "name", "values", "subQuestions", "options", "disabled", "warnings", "show", "section"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
function Wrapper(_ref) {
var isMultiple = _ref.isMultiple,
name = _ref.name,
values = _ref.values,
_ref$subQuestions = _ref.subQuestions,
subQuestions = _ref$subQuestions === void 0 ? [] : _ref$subQuestions,
_ref$options = _ref.options,
options = _ref$options === void 0 ? [] : _ref$options,
_ref$disabled = _ref.disabled,
disabled = _ref$disabled === void 0 ? false : _ref$disabled,
_ref$warnings = _ref.warnings,
warnings = _ref$warnings === void 0 ? {} : _ref$warnings,
show = _ref.show,
section = _ref.section,
props = _objectWithoutProperties(_ref, _excluded);
if (!show) {
return null;
}
var Component;
if (isMultiple) {
Component = /*#__PURE__*/(0, _jsxRuntime.jsx)(_formik.FieldArray, {
name: name,
render: function render(helpers) {
return values.answer.map(function (answer, index) {
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
className: "fb:flex fb:flex-row fb:gap-4 fb:mb-4 fb:items-start",
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_formik.Field, _objectSpread(_objectSpread({}, props), {}, {
options: options,
name: "".concat(name, ".").concat(index, ".value"),
disabled: disabled,
warnings: warnings
})), /*#__PURE__*/(0, _jsxRuntime.jsx)(_SubQuestions["default"], {
values: {
answer: answer,
id: answer.id
},
subQuestions: subQuestions,
disabled: disabled,
warnings: warnings,
name: "".concat(name, ".").concat(index, ".specifications"),
section: section
}), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
className: "fb:flex fb:flex-row fb:gap-2 fb:items-center fb:pt-8",
children: [values.answer.length === index + 1 && !disabled && /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
type: "button",
onClick: function onClick() {
return helpers.push({
id: (0, _getLastId["default"])(values.answer) + 1,
value: '',
specifications: (0, _buildQuestions.getSubQuestions)(subQuestions)
});
},
className: "fb:p-2 fb:rounded-full fb:text-blue-600 fb:hover:bg-blue-50 fb:focus:outline-none fb:focus:ring-2 fb:focus:ring-blue-500 fb:focus:ring-offset-2",
"aria-label": "Add item",
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icons.PlusCircleIcon, {})
}), index > 0 && !disabled && /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
type: "button",
onClick: function onClick() {
return helpers.remove(index);
},
className: "fb:p-2 fb:rounded-full fb:text-red-600 fb:hover:bg-red-50 fb:focus:outline-none fb:focus:ring-2 fb:focus:ring-red-500 fb:focus:ring-offset-2",
"aria-label": "Delete item",
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icons.DeleteIcon, {})
})]
})]
}, answer.id);
});
}
});
} else {
Component = /*#__PURE__*/(0, _jsxRuntime.jsx)(_formik.Field, _objectSpread(_objectSpread({}, props), {}, {
options: options,
name: "".concat(name, ".value"),
disabled: disabled,
warnings: warnings
}));
}
if (subQuestions.length > 0 && options.length > 0 && !isMultiple) {
Component = /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
className: "fb:flex fb:flex-col fb:sm:flex-row fb:gap-4",
children: [Component, /*#__PURE__*/(0, _jsxRuntime.jsx)(_SubQuestions["default"], {
values: values,
subQuestions: subQuestions,
disabled: disabled,
Component: _TextField["default"],
warnings: warnings,
name: "".concat(name, ".specifications"),
section: section
})]
});
}
return Component;
}
var _default = exports["default"] = Wrapper;