UNPKG

@readr-media/react-feedback

Version:

## Installation `yarn install`

156 lines (132 loc) 5.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = ThumbsField; var _react = _interopRequireWildcard(require("react")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _thumbField = _interopRequireDefault(require("./thumb-field")); var _useThumbsUp = _interopRequireDefault(require("../../hooks/use-thumbsUp")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } const Wrapper = _styledComponents.default.div.withConfig({ displayName: "thumbs-field__Wrapper", componentId: "sc-14l53zj-0" })(["display:flex;flex-direction:column;align-items:center;margin:20px 0;"]); const Title = _styledComponents.default.p.withConfig({ displayName: "thumbs-field__Title", componentId: "sc-14l53zj-1" })(["margin:0;color:", ";font-weight:", ";line-height:", ";font-size:", ";@media ", "{font-size:", ";}"], ({ theme }) => theme.thumbField.title.color, ({ theme }) => theme.thumbField.title.fontWeight, ({ theme }) => theme.thumbField.title.lineHeight, ({ theme }) => theme.thumbField.title.mobile.fontSize, ({ theme }) => theme.breakpoint.tablet, ({ theme }) => theme.thumbField.title.tablet.fontSize); const ThumbWrapper = _styledComponents.default.div.withConfig({ displayName: "thumbs-field__ThumbWrapper", componentId: "sc-14l53zj-2" })(["margin-top:20px;display:flex;"]); /** * @typedef {import('../../../typedef').SingleField} SingleField * @typedef {import('../../../typedef').ThumbsFieldProps} ThumbsFieldProps * * @param {Object} props * @param {string} props.formId * @param {SingleField} props.field * @return {JSX.Element} */ function ThumbsField({ formId, field }) { const [thumbUpChecked, setThumbUpChecked] = (0, _react.useState)(false); const [thumbDownChecked, setThumbDownChecked] = (0, _react.useState)(false); const [thumbUpPressing, setThumbUpPressing] = (0, _react.useState)(false); const [thumbDownPressing, setThumbDownPressing] = (0, _react.useState)(false); const timerRef = (0, _react.useRef)(null); const initialMounted = (0, _react.useRef)(true); const { thumbsUp, giveThumbUp } = (0, _useThumbsUp.default)(formId, field.id, field.identifier); (0, _react.useEffect)(() => { if (initialMounted.current) { initialMounted.current = !initialMounted.current; return; } if (timerRef.current) { clearTimeout(timerRef.current); } timerRef.current = setTimeout(() => { let thumbValue = null; if (thumbUpChecked) { thumbValue = true; } if (thumbDownChecked) { thumbValue = false; } giveThumbUp(thumbValue); timerRef.current = null; }, 1000); }, [thumbUpChecked, thumbDownChecked]); const thumbUpRadioClicked = () => { setThumbUpPressing(false); setThumbUpChecked(thumbUpChecked => { if (thumbUpChecked) { return false; } else { setThumbDownChecked(false); return true; } }); }; const thumbDownRadioClicked = () => { setThumbDownPressing(false); setThumbDownChecked(thumbDownChecked => { if (thumbDownChecked) { return false; } else { setThumbUpChecked(false); return true; } }); }; /** @type {ThumbsFieldProps} */ const thumbUpProps = { thumbsUp: true, onMouseDown: () => setThumbUpPressing(true), onMouseUp: thumbUpRadioClicked, checked: thumbUpChecked, pressing: thumbUpPressing, label: field.thumbUpLabel ?? '符合', statistic: thumbsUp ? thumbsUp.thumbUp : null }; /** @type {ThumbsFieldProps} */ const thumbDownProps = { thumbsUp: false, onMouseDown: () => setThumbDownPressing(true), onMouseUp: thumbDownRadioClicked, checked: thumbDownChecked, pressing: thumbDownPressing, label: field.thumbDownLabel ?? '不符合', statistic: thumbsUp ? thumbsUp.thumbDown : null }; return /*#__PURE__*/(0, _jsxRuntime.jsxs)(Wrapper, { children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Title, { children: field.name }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(ThumbWrapper, { children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_thumbField.default, { ...thumbUpProps }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_thumbField.default, { ...thumbDownProps })] })] }); }