@indec/form-builder
Version:
Form builder
126 lines (125 loc) • 8.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _FieldMessage = _interopRequireDefault(require("../FieldMessage"));
var _InputLabel = _interopRequireDefault(require("../InputLabel"));
var _Icons = require("../Icons");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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); }
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 RadioTable(_ref) {
var options = _ref.options,
label = _ref.label,
form = _ref.form,
field = _ref.field,
_ref$disabled = _ref.disabled,
disabled = _ref$disabled === void 0 ? false : _ref$disabled,
_ref$warnings = _ref.warnings,
warnings = _ref$warnings === void 0 ? {} : _ref$warnings;
var handleChange = function handleChange(e, option, subOption) {
var value;
if (subOption.exclusive) {
value = options.reduce(function (accumulator, currentValue) {
return _objectSpread(_objectSpread({}, accumulator), {}, _defineProperty({}, currentValue.name, option.id === currentValue.id ? e.target.value : undefined));
}, {});
} else {
var exclusiveOptions = options.reduce(function (acc, currentValue) {
if (currentValue.subOptions.some(function (currentSubOption) {
return currentSubOption.exclusive;
})) {
acc[currentValue.name] = currentValue.subOptions.filter(function (currentSubOption) {
return currentSubOption.exclusive;
});
}
return acc;
}, {});
value = _objectSpread(_objectSpread({}, field.value), {}, _defineProperty({}, option.name, e.target.value));
Object.keys(value).forEach(function (key) {
if (exclusiveOptions[key] && exclusiveOptions[key].some(function (currentSubOption) {
return currentSubOption.value === value[key];
})) {
value[key] = undefined;
}
});
}
form.setFieldValue(field.name, value);
form.setFieldTouched(field.name, false);
};
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
className: "fb:flex fb:flex-col fb:space-y-4",
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_InputLabel["default"], {
warnings: warnings,
form: form,
field: field,
label: label,
disabled: disabled
}), options.map(function (option) {
return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: "fb:space-y-2",
children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
className: "fb:flex fb:flex-col fb:sm:flex-row fb:sm:space-x-4 fb:space-y-2 fb:sm:space-y-0",
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: "fb:min-w-0 fb:sm:min-w-96 fb:sm:max-w-96 fb:overflow-hidden",
children: /*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
className: "fb:text-sm fb:font-medium fb:whitespace-normal ".concat(disabled ? 'fb:opacity-50 fb:text-gray-400' : 'fb:text-gray-900'),
children: option.title
})
}), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
className: "fb:flex fb:flex-row fb:flex-wrap fb:items-center fb:gap-4",
children: [option.subOptions.map(function (subOption) {
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("label", {
htmlFor: "".concat(field.name, "-").concat(option.name, "-").concat(subOption.value),
"data-testid": "subOption-".concat(option.id, "-").concat(subOption.id),
className: "fb:group fb:flex fb:items-center fb:space-x-4 fb:p-2 fb:rounded-lg fb:transition-all fb:duration-200 ".concat(disabled ? 'fb:opacity-50 fb:cursor-not-allowed' : 'fb:cursor-pointer'),
children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
className: "fb:relative fb:flex-shrink-0",
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
id: "".concat(field.name, "-").concat(option.name, "-").concat(subOption.value),
type: "radio",
name: "".concat(field.name, ".").concat(option.name),
value: subOption.value,
checked: subOption.value === field.value[option.name],
onChange: function onChange(e) {
return handleChange(e, option, subOption);
},
disabled: disabled,
className: "fb:sr-only"
}), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: "fb:w-6 fb:h-6 fb:border-2 fb:rounded-full fb:flex fb:items-center fb:justify-center fb:transition-all fb:duration-200 fb:transform ".concat(subOption.value === field.value[option.name] ? 'fb:bg-blue-600 fb:border-blue-600 fb:scale-100 fb:shadow-md' : 'fb:bg-white fb:border-gray-300 fb:scale-100', " ").concat(disabled ? 'fb:opacity-50' : '', " fb:focus-within:ring-2 fb:focus-within:ring-blue-500 fb:focus-within:ring-offset-2"),
children: subOption.value === field.value[option.name] && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: "fb:w-2 fb:h-2 fb:bg-white fb:rounded-full fb:transform fb:transition-transform fb:duration-150 fb:scale-110"
})
})]
}), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
className: "fb:select-none fb:font-medium fb:transition-colors fb:duration-200 ".concat(disabled ? 'fb:text-gray-400' : 'fb:text-gray-900'),
children: subOption.label
})]
}, subOption.value);
}), field.value[option.name] && !disabled && /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
type: "button",
onClick: function onClick() {
return form.setFieldValue("".concat(field.name, ".").concat(option.name), undefined);
},
className: "fb:p-1 fb:text-red-600 fb:hover:text-red-800 fb:hover:bg-red-50 fb:rounded fb:transition-colors fb:duration-200",
"data-testid": "clean-option-".concat(option.id),
"aria-label": "Clear selection",
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icons.DeleteIcon, {})
})]
})]
})
}, option.id);
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_FieldMessage["default"], {
warnings: warnings,
form: form,
field: field
})]
});
}
var _default = exports["default"] = RadioTable;