UNPKG

@pubsweet/ui

Version:

React component library for use in pubsweet apps

155 lines (115 loc) 12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _reactTagAutocomplete = _interopRequireDefault(require("react-tag-autocomplete")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _uiToolkit = require("@pubsweet/ui-toolkit"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _templateObject() { var data = _taggedTemplateLiteral(["\n .", " {\n /* clicking anywhere will focus the input */\n cursor: text;\n padding: ", " 0 0 ", ";\n position: relative;\n }\n\n .", ".", " {\n border-color: ", ";\n }\n\n .", " {\n display: inline;\n }\n\n .", " {\n border: 0;\n cursor: pointer;\n display: inline-block;\n font-family: ", ";\n\n /* match the font styles */\n margin: 0 ", " calc(", " * 3) 0;\n padding: 0.1em 0.3em;\n\n ", ";\n }\n\n .", "::after {\n color: ", ";\n content: '\\2715';\n display: inline-block;\n font-size: ", ";\n font-weight: 600;\n line-height: ", ";\n margin-left: ", ";\n padding: ", " 0 0;\n width: ", ";\n }\n\n .", ":hover, .", ":focus {\n text-decoration: line-through;\n\n ", " &::after {\n color: ", ";\n }\n }\n .", " {\n display: inline-block;\n\n /* match tag layout */\n margin: 0 0 calc(", " * 3) 0;\n\n /* prevent autoresize overflowing the container */\n max-width: calc(", " * 15);\n padding: ", " ", ";\n }\n\n @media screen and (min-width: 30em) {\n .", " {\n /* this will become the offsetParent for suggestions */\n position: relative;\n }\n }\n\n .", " input {\n border: 0;\n border-bottom: ", " dashed ", ";\n color: ", ";\n font-family: ", ";\n\n /* match the font styles */\n font-size: inherit;\n line-height: inherit;\n\n /* remove styles and layout from this element */\n margin: 0;\n\n /* prevent autoresize overflowing the container */\n max-width: 100%;\n min-width: calc(", " * 15);\n outline: none;\n padding: 0;\n\n &::placeholder {\n font-family: ", ";\n color: ", ";\n }\n\n &::-ms-clear {\n display: none;\n }\n\n &:focus,\n &:hover {\n border-bottom: ", " dashed ", ";\n }\n }\n"], ["\n .", " {\n /* clicking anywhere will focus the input */\n cursor: text;\n padding: ", " 0 0 ", ";\n position: relative;\n }\n\n .", ".", " {\n border-color: ", ";\n }\n\n .", " {\n display: inline;\n }\n\n .", " {\n border: 0;\n cursor: pointer;\n display: inline-block;\n font-family: ", ";\n\n /* match the font styles */\n margin: 0 ", " calc(", " * 3) 0;\n padding: 0.1em 0.3em;\n\n ", ";\n }\n\n .", "::after {\n color: ", ";\n content: '\\\\2715';\n display: inline-block;\n font-size: ", ";\n font-weight: 600;\n line-height: ", ";\n margin-left: ", ";\n padding: ", " 0 0;\n width: ", ";\n }\n\n .", ":hover, .", ":focus {\n text-decoration: line-through;\n\n ", " &::after {\n color: ", ";\n }\n }\n .", " {\n display: inline-block;\n\n /* match tag layout */\n margin: 0 0 calc(", " * 3) 0;\n\n /* prevent autoresize overflowing the container */\n max-width: calc(", " * 15);\n padding: ", " ", ";\n }\n\n @media screen and (min-width: 30em) {\n .", " {\n /* this will become the offsetParent for suggestions */\n position: relative;\n }\n }\n\n .", " input {\n border: 0;\n border-bottom: ", " dashed ", ";\n color: ", ";\n font-family: ", ";\n\n /* match the font styles */\n font-size: inherit;\n line-height: inherit;\n\n /* remove styles and layout from this element */\n margin: 0;\n\n /* prevent autoresize overflowing the container */\n max-width: 100%;\n min-width: calc(", " * 15);\n outline: none;\n padding: 0;\n\n &::placeholder {\n font-family: ", ";\n color: ", ";\n }\n\n &::-ms-clear {\n display: none;\n }\n\n &:focus,\n &:hover {\n border-bottom: ", " dashed ", ";\n }\n }\n"]); _templateObject = function _templateObject() { return data; }; return data; } function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } // TODO: separate tags when pasted // TODO: allow tags to be edited // react-tag-autocomplete expects classnames on these keys var classNames = { root: 'react-tags', rootFocused: 'is-focused', selected: 'react-tags__selected', selectedTag: 'react-tags__selected-tag', selectedTagName: 'react-tags__selected-tag-name', search: 'react-tags__search', searchInput: 'react-tags__search-input', suggestions: 'react-tags__suggestions', suggestionActive: 'is-active', suggestionDisabled: 'is-disabled' }; var TagsUnstyled = /*#__PURE__*/function (_React$Component) { _inherits(TagsUnstyled, _React$Component); var _super = _createSuper(TagsUnstyled); function TagsUnstyled(props) { var _this; _classCallCheck(this, TagsUnstyled); _this = _super.call(this, props); _defineProperty(_assertThisInitialized(_this), "handleDelete", function (index) { var tags = _this.state.tags; tags.splice(index, 1); _this.setState({ tags: tags }); _this.props.onChange(tags); }); _defineProperty(_assertThisInitialized(_this), "handleAddition", function (tag) { var tags = _this.state.tags; tags.push(tag); _this.setState({ tags: tags }); _this.props.onChange(tags); }); _this.state = { tags: props.value || [] }; return _this; } _createClass(TagsUnstyled, [{ key: "render", value: function render() { var tags = this.state.tags; var _this$props = this.props, fromStyledWrapper = _this$props.className, name = _this$props.name, suggestions = _this$props.suggestions, placeholder = _this$props.placeholder; return /*#__PURE__*/_react["default"].createElement("div", { className: fromStyledWrapper }, /*#__PURE__*/_react["default"].createElement(_reactTagAutocomplete["default"], { allowNew: true, autofocus: false // TODO: enable these when react-tag-autocomplete update is released // delimiters={[]} // delimiterChars={[',', ';']} , classNames: classNames, handleAddition: this.handleAddition, handleDelete: this.handleDelete, name: name, placeholder: placeholder, suggestions: suggestions, tags: tags })); } }]); return TagsUnstyled; }(_react["default"].Component); var realBorder = function realBorder(color, colorBack) { return "\n background: linear-gradient(\n ".concat(colorBack, " 0,\n ").concat(colorBack, " 1.2em,\n ").concat(color, " 1.2em,\n ").concat(color, " 1.25em,\n ").concat(colorBack, " 1.25em,\n ").concat(colorBack, " 2em\n )\n no-repeat;\n text-shadow: 0.05em 0.05em 0 ").concat(colorBack, ", -0.05em -0.05em 0 ").concat(colorBack, ",\n -0.05em 0.05em 0 ").concat(colorBack, ", 0.05em -0.05em 0 ").concat(colorBack, ";\n"); }; var Tags = (0, _styledComponents["default"])(TagsUnstyled)(_templateObject(), classNames.root, (0, _uiToolkit.th)('gridUnit'), (0, _uiToolkit.th)('gridUnit'), classNames.root, classNames.rootFocused, (0, _uiToolkit.th)('colorPrimary'), classNames.selected, classNames.selectedTag, (0, _uiToolkit.th)('fontReading'), (0, _uiToolkit.th)('gridUnit'), (0, _uiToolkit.th)('gridUnit'), function (props) { return realBorder(props.theme.colorBorder, props.theme.colorBackground); }, classNames.selectedTag, (0, _uiToolkit.th)('colorBorder'), (0, _uiToolkit.th)('fontSizeBaseSmall'), (0, _uiToolkit.th)('lineHeightBaseSmall'), (0, _uiToolkit.th)('gridUnit'), (0, _uiToolkit.th)('gridUnit'), (0, _uiToolkit.th)('gridUnit'), classNames.selectedTag, classNames.selectedTag, function (props) { return realBorder('transparent', props.theme.colorBackground); }, (0, _uiToolkit.th)('colorError'), classNames.search, (0, _uiToolkit.th)('gridUnit'), (0, _uiToolkit.th)('gridUnit'), (0, _uiToolkit.th)('gridUnit'), (0, _uiToolkit.th)('gridUnit'), classNames.search, classNames.search, (0, _uiToolkit.th)('borderWidth'), (0, _uiToolkit.th)('colorBorder'), (0, _uiToolkit.th)('colorText'), (0, _uiToolkit.th)('fontReading'), (0, _uiToolkit.th)('gridUnit'), (0, _uiToolkit.th)('fontInterface'), (0, _uiToolkit.th)('colorTextPlaceholder'), (0, _uiToolkit.th)('borderWidth'), (0, _uiToolkit.th)('colorPrimary')); /** * @component */ var _default = Tags; exports["default"] = _default;