UNPKG

@coko/client

Version:

Client side common code for coko apps

150 lines (146 loc) 12.3 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _styledComponents = _interopRequireWildcard(require("styled-components")); var _lodash = require("lodash"); var _antd = require("antd"); var _toolkit = require("../../toolkit"); var _Empty = _interopRequireDefault(require("./Empty")); var _excluded = ["async", "className", "debounceTimeout", "filterOption", "notFoundContent", "onSearch", "showSearch", "id", "isOpen", "virtual", "wrapOptionText"]; var _templateObject, _templateObject2, _templateObject3, _templateObject4; /* stylelint-disable indentation */ /* stylelint-disable selector-combinator-space-before */ /* stylelint-disable selector-descendant-combinator-no-non-space */ /* stylelint-disable string-quotes */ 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 _getRequireWildcardCache(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; } function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } var SelectWrapper = _styledComponents["default"].span(_templateObject || (_templateObject = _taggedTemplateLiteral([""]))); var StyledSelect = (0, _styledComponents["default"])(_antd.Select)(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n width: 100%;\n\n &.ant-select-focused {\n outline: ", "px solid\n ", ";\n outline-offset: 1px;\n }\n\n .ant-select-arrow {\n color: ", ";\n }\n\n &.ant-select.ant-select-disabled > .ant-select-selector,\n &.ant-select-multiple.ant-select-disabled\n > .ant-select-selector\n .ant-select-selection-item-content {\n background-color: ", ";\n color: ", ";\n }\n"])), function (props) { return "".concat(props.theme.lineWidth * 4); }, (0, _toolkit.th)('colorPrimaryBorder'), (0, _toolkit.th)('colorBorder'), (0, _toolkit.th)('colorBackgroundHue'), function (props) { return "".concat(props.theme.colorText, "cc"); }); var StyledDropdown = _styledComponents["default"].div(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n .ant-select-item-option-active {\n background-color: ", ";\n outline: 2px solid ", ";\n outline-offset: -2px;\n\n &.ant-select-item-option-selected[role='option'][aria-selected='true'] {\n text-decoration: underline;\n }\n }\n\n .ant-select-item-option-selected[role='option'][aria-selected='true'] {\n background-color: ", ";\n color: ", ";\n\n .ant-select-item-option-state {\n color: ", ";\n }\n }\n\n .ant-select-item-option-content {\n /* outline: 2px solid ", "; */\n ", "\n }\n"])), (0, _toolkit.th)('colorBackgroundHue'), (0, _toolkit.th)('colorPrimary'), (0, _toolkit.th)('colorPrimary'), (0, _toolkit.th)('colorTextReverse'), (0, _toolkit.th)('colorTextReverse'), (0, _toolkit.th)('colorPrimary'), function (props) { return props.wrapOptionText && (0, _styledComponents.css)(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n white-space: normal;\n "]))); }); var Select = function Select(props) { var async = props.async, className = props.className, debounceTimeout = props.debounceTimeout, filterOption = props.filterOption, notFoundContent = props.notFoundContent, onSearch = props.onSearch, showSearch = props.showSearch, id = props.id, isOpen = props.isOpen, virtual = props.virtual, wrapOptionText = props.wrapOptionText, rest = _objectWithoutProperties(props, _excluded); var selectRef = (0, _react.useRef)(null); var _useState = (0, _react.useState)(isOpen), _useState2 = _slicedToArray(_useState, 2), open = _useState2[0], setOpen = _useState2[1]; var _useState3 = (0, _react.useState)({}), _useState4 = _slicedToArray(_useState3, 2), ariaAttributes = _useState4[0], setAriaAttributes = _useState4[1]; var cleanUpInvalidAttrs = function cleanUpInvalidAttrs() { var _selectRef$current, _selectRef$current2, _selectRef$current3, _selectRef$current4, _selectRef$current5, _selectRef$current6; // store invalid attrs in local state setAriaAttributes({ 'aria-controls': (_selectRef$current = selectRef.current) === null || _selectRef$current === void 0 ? void 0 : _selectRef$current.querySelector('input[role="combobox"]').getAttribute('aria-controls'), 'aria-owns': (_selectRef$current2 = selectRef.current) === null || _selectRef$current2 === void 0 ? void 0 : _selectRef$current2.querySelector('input[role="combobox"]').getAttribute('aria-owns'), 'aria-activedescendant': (_selectRef$current3 = selectRef.current) === null || _selectRef$current3 === void 0 ? void 0 : _selectRef$current3.querySelector('input[role="combobox"]').getAttribute('aria-activedescendant') }); // remove them from the DOM node (_selectRef$current4 = selectRef.current) === null || _selectRef$current4 === void 0 ? void 0 : _selectRef$current4.querySelector('input[role="combobox"]').removeAttribute('aria-controls'); (_selectRef$current5 = selectRef.current) === null || _selectRef$current5 === void 0 ? void 0 : _selectRef$current5.querySelector('input[role="combobox"]').removeAttribute('aria-owns'); (_selectRef$current6 = selectRef.current) === null || _selectRef$current6 === void 0 ? void 0 : _selectRef$current6.querySelector('input[role="combobox"]').removeAttribute('aria-activedescendant'); }; (0, _react.useEffect)(function () { var innerWrapper = selectRef.current.querySelector('.ant-select'); innerWrapper.removeAttribute('aria-required'); // hack to fix accessibility errors // apply with delay to make sure attrs are already there setTimeout(function () { cleanUpInvalidAttrs(); }, 500); }, []); (0, _react.useEffect)(function () { if (open && !!ariaAttributes) { // reapply the stored aria attributes after opening input for the first time Object.keys(ariaAttributes).forEach(function (attr) { selectRef.current.querySelector('input[role="combobox"]').setAttribute(attr, ariaAttributes[attr]); }); setAriaAttributes(null); } }, [open]); var handleSearch = function handleSearch(searchValue) { onSearch(searchValue); }; // const useDebounce = async ? true : debounce var searchFunc = async ? (0, _lodash.debounce)(handleSearch, debounceTimeout) : handleSearch; var customDropdownRender = function customDropdownRender(menu) { return /*#__PURE__*/_react["default"].createElement(StyledDropdown, { "data-testid": "select-dropdown", wrapOptionText: wrapOptionText }, menu); }; return /*#__PURE__*/_react["default"].createElement(SelectWrapper, { className: className, ref: selectRef }, /*#__PURE__*/_react["default"].createElement(StyledSelect, _extends({ dropdownRender: customDropdownRender, filterOption: async && !filterOption ? false : filterOption, id: id, notFoundContent: !notFoundContent && async ? null : notFoundContent, onDropdownVisibleChange: function onDropdownVisibleChange(o) { return setOpen(o); }, onSearch: onSearch && searchFunc, open: open, showSearch: showSearch || !!onSearch, virtual: virtual }, rest))); }; Select.propTypes = { async: _propTypes["default"].bool, // debounce: PropTypes.bool, debounceTimeout: _propTypes["default"].number, notFoundContent: _propTypes["default"].element, isOpen: _propTypes["default"].bool, virtual: _propTypes["default"].bool, wrapOptionText: _propTypes["default"].bool }; Select.defaultProps = { async: false, // debounce: false, debounceTimeout: 500, notFoundContent: /*#__PURE__*/_react["default"].createElement(_Empty["default"], { description: "No Data", image: _Empty["default"].PRESENTED_IMAGE_SIMPLE, role: "status" }), isOpen: false, virtual: false, wrapOptionText: false }; var _default = Select; exports["default"] = _default;