UNPKG

@mopinion/survey

Version:

Collect customer feedback with the Mopinion survey library

90 lines (89 loc) 5.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = CommentBar; var _preact = require("preact"); var _hooks = require("preact/hooks"); var _components = require("../../../components"); var _form = require("../../form"); var _utils = require("../utils"); var _utils2 = require("../../../utils"); var _excluded = ["skipQuestion", "block", "elementIndex", "state", "setValue", "autoFocus", "errorMessage"]; function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } 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 CommentBar(_ref) { var _block$typeName, _block$id, _block$properties, _text$wcag$inputSendB, _text$wcag, _text$wcag$inputSendB2, _text$wcag2; var skipQuestion = _ref.skipQuestion, block = _ref.block, elementIndex = _ref.elementIndex, state = _ref.state, setValue = _ref.setValue, autoFocus = _ref.autoFocus, errorMessage = _ref.errorMessage, props = _objectWithoutProperties(_ref, _excluded); var _useState = (0, _hooks.useState)(state.value), _useState2 = _slicedToArray(_useState, 2), inputValue = _useState2[0], setInputValue = _useState2[1]; var inputRef = (0, _hooks.useRef)(null); var _useConfigStore = (0, _form.useConfigStore)(), text = _useConfigStore.text; var error = (0, _utils.showConversationalError)({ block: block, state: state }); function handleUpdate() { if (block.properties.required || inputValue) { setValue(inputValue); } if (!block.properties.required && !inputValue) { skipQuestion(); } } (0, _hooks.useEffect)(function () { try { inputRef.current.focus(); } catch (e) {} }, []); var id = "element-".concat((_block$typeName = block.typeName) !== null && _block$typeName !== void 0 ? _block$typeName : '', "-").concat((_block$id = block.id) !== null && _block$id !== void 0 ? _block$id : block.data_field); return (0, _preact.h)("div", { className: (0, _utils2.makeClassName)('comment-bar-wrap') }, (0, _preact.h)("label", { "for": id, className: (0, _utils2.makeClassName)('comment-bar-hidden-label') }, block.title), (0, _preact.h)("textarea", { className: (0, _utils2.makeClassName)('comment-bar', error ? { prefix: false, className: 'error' } : null), id: id, value: inputValue, onInput: function onInput(e) { return setInputValue(e.target.value); }, ref: inputRef, placeholder: ((_block$properties = block.properties) === null || _block$properties === void 0 ? void 0 : _block$properties.placeholder) || '' }), (state === null || state === void 0 ? void 0 : state.isLoading) && (0, _preact.h)(_components.Spinner, null), (0, _preact.h)("button", { type: "button", className: (0, _utils2.makeClassName)({ prefix: false, className: 'btn' }, { prefix: false, className: 'btn btn-primary' }, 'comment-bar-button'), onClick: handleUpdate, "aria-label": (_text$wcag$inputSendB = text === null || text === void 0 || (_text$wcag = text.wcag) === null || _text$wcag === void 0 ? void 0 : _text$wcag.inputSendButtonDescription) !== null && _text$wcag$inputSendB !== void 0 ? _text$wcag$inputSendB : 'send' }, (0, _preact.h)(_form.SvgIcon, { icon: "paper-plane-o", alt: (_text$wcag$inputSendB2 = text === null || text === void 0 || (_text$wcag2 = text.wcag) === null || _text$wcag2 === void 0 ? void 0 : _text$wcag2.inputSendButtonDescription) !== null && _text$wcag$inputSendB2 !== void 0 ? _text$wcag$inputSendB2 : 'send' }))); } //# sourceMappingURL=CommentBar.js.map