UNPKG

@mopinion/survey

Version:

Collect customer feedback with the Mopinion survey library

120 lines (119 loc) 6.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = MatrixLikert; var _preact = require("preact"); var _form = require("../../form"); var _HandleTabIndex = _interopRequireDefault(require("./HandleTabIndex")); var _excluded = ["children", "block", "state", "setNestedValue"]; function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(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 (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } function MatrixLikert(_ref) { var children = _ref.children, block = _ref.block, state = _ref.state, setNestedValue = _ref.setNestedValue, props = _objectWithoutProperties(_ref, _excluded); var _block$properties = block.properties, elements = _block$properties.elements, values = _block$properties.values, weights = _block$properties.weights; var _useFormProps = (0, _form.useFormProps)(), isMultiple = _useFormProps.isMultiple, multipleIndex = _useFormProps.multipleIndex; return (0, _preact.h)("table", _extends({ "class": "table table-striped ".concat(block.typeName, "-table"), role: "radiogroup" }, block.properties.required && { 'aria-required': true }, { "aria-labelledby": "block-title-".concat(block.typeName, "-").concat(block.id) }), (0, _preact.h)("thead", null, (0, _preact.h)("tr", null, (0, _preact.h)("th", { "class": "".concat(block.typeName, "-subject-label"), "aria-hidden": "true" }), Object.keys(values).map(function (key) { return (0, _preact.h)("th", { "class": "".concat(block.typeName, "-answer-label") }, (0, _preact.h)("div", null, (0, _preact.h)("span", null, values[key].label))); }), weights && (0, _preact.h)("th", { "class": "".concat(block.typeName, "-spacer"), "aria-hidden": "true" }), weights && Object.keys(weights).map(function (key) { return (0, _preact.h)("th", { "class": "".concat(block.typeName, "-answer-label") }, (0, _preact.h)("div", null, (0, _preact.h)("span", null, weights[key].label))); }))), (0, _preact.h)("tbody", null, Object.keys(elements).map(function (key, i) { var id = "".concat(block.typeName, "_").concat(block.id, "_").concat(i + 1); var element = elements[key]; return (0, _preact.h)("tr", null, (0, _preact.h)("td", { "class": "".concat(block.typeName, "-subject") }, elements[key].label), Object.keys(values).map(function (vKey, vI) { var _element$data_field, _valueElement$value; var valueDataField = block.typeName === 'matrix' ? (_element$data_field = element.data_field) === null || _element$data_field === void 0 ? void 0 : _element$data_field.value : element.data_field; var valueElement = values[vKey]; var value = (_valueElement$value = valueElement.value) !== null && _valueElement$value !== void 0 ? _valueElement$value : valueElement.label; return (0, _preact.h)("td", { "class": "".concat(block.typeName, "-answer"), "data-label": values[vKey].label }, (0, _preact.h)("div", { "class": "".concat(block.typeName, "-answer-inner") }, (0, _preact.h)(_HandleTabIndex["default"], { component: "input", title: elements[key].label + ', ' + values[vKey].label, ariaLabel: elements[key].label + ', ' + values[vKey].label, type: "radio", name: "".concat(id).concat(block.typeName === 'matrix' ? '_value' : '').concat(isMultiple ? '_' + multipleIndex : ''), id: "".concat(id).concat(block.typeName === 'matrix' ? '_value' : '', "_").concat(vI).concat(isMultiple ? '_' + multipleIndex : ''), value: value, checked: state[valueDataField].value === value, onChange: function onChange(e) { setNestedValue(valueDataField, e.target.value); } }), (0, _preact.h)("label", { "for": "".concat(id).concat(block.typeName === 'matrix' ? '_value' : '', "_").concat(vI).concat(isMultiple ? '_' + multipleIndex : '') }, (0, _preact.h)("span", { style: { display: 'none' } }, elements[key].label + ', ' + values[vKey].label)))); }), weights && (0, _preact.h)("td", { "class": "".concat(block.typeName, "-spacer") }), weights && Object.keys(weights).map(function (wKey, wI) { var _element$data_field2, _weightElement$value; var weightDataField = block.typeName === 'matrix' ? (_element$data_field2 = element.data_field) === null || _element$data_field2 === void 0 ? void 0 : _element$data_field2.weight : element.data_field; var weightElement = weights[wKey]; var value = (_weightElement$value = weightElement.value) !== null && _weightElement$value !== void 0 ? _weightElement$value : weightElement.label; return (0, _preact.h)("td", { "class": "".concat(block.typeName, "-answer"), "data-label": weights[wKey].label }, (0, _preact.h)("div", { "class": "".concat(block.typeName, "-answer-inner") }, (0, _preact.h)(_HandleTabIndex["default"], { component: "input", type: "radio", title: elements[key].label + ', ' + weights[wKey].label, ariaLabel: elements[key].label + ', ' + weights[wKey].label, name: "".concat(id, "_weight").concat(isMultiple ? '_' + multipleIndex : ''), id: "".concat(id, "_weight_").concat(wI).concat(isMultiple ? '_' + multipleIndex : ''), value: weights[wKey].value ? weights[wKey].value : weights[wKey].label, checked: state[weightDataField].value === value, onChange: function onChange(e) { setNestedValue(weightDataField, e.target.value); } }), (0, _preact.h)("label", { "for": "".concat(id, "_weight_").concat(wI).concat(isMultiple ? '_' + multipleIndex : '') }, (0, _preact.h)("span", { style: { display: 'none' } }, elements[wKey].label + ', ' + weights[wKey].label)))); }), (0, _preact.h)("input", { type: "hidden", id: id })); }))); } //# sourceMappingURL=MatrixLikert.js.map