UNPKG

@coko/client

Version:

Client side common code for coko apps

86 lines (80 loc) 7.52 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 _styledComponents = _interopRequireWildcard(require("styled-components")); var _propTypes = _interopRequireDefault(require("prop-types")); var _antd = require("antd"); var _lodash = require("lodash"); var _toolkit = require("../../toolkit"); var _excluded = ["children", "className", "autoFocus"]; var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5, _templateObject6; 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 _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 colors = { danger: 'colorError', error: 'colorError', success: 'colorSuccess' // warn: 'colorWarning', }; var StyledButton = (0, _styledComponents["default"])(_antd.Button)(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n box-shadow: none;\n font-size: ", ";\n /* let lineHeight expand the button height */\n height: unset;\n line-height: ", ";\n ", ";\n\n ", "\n padding: 0 ", ";\n"])), (0, _toolkit.th)('fontSizeBase'), (0, _toolkit.th)('lineHeightBase'), function (props) { return props.direction === 'rtl' && (0, _styledComponents.css)(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n direction: rtl;\n\n .anticon + span {\n margin-right: 8px;\n margin-left: 0;\n }\n "]))); }, function (props) { var status = props.status, theme = props.theme, type = props.type, ghost = props.ghost, disabled = props.disabled; if (disabled) return null; if (!Object.keys(colors).includes(status)) { if (type === 'primary' && !ghost) { return (0, _styledComponents.css)(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n &:hover,\n &:focus,\n &:active {\n background-color: ", " !important;\n }\n "])), (0, _toolkit.darken)('colorPrimary', 0.25)); } return (0, _styledComponents.css)(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n &:hover,\n &:focus,\n &:active {\n border-color: ", " !important;\n color: ", " !important;\n }\n "])), (0, _toolkit.darken)('colorPrimary', 0.25), (0, _toolkit.darken)('colorPrimary', 0.25)); } var color = theme[colors[status]]; // primary if (type === 'primary') return (0, _styledComponents.css)(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["\n background-color: ", ";\n border-color: ", ";\n color: ", ";\n\n &:hover,\n &:focus,\n &:active {\n border-color: ", ";\n color: ", ";\n }\n\n &:hover,\n &:focus {\n background-color: ", " !important;\n }\n\n &:active {\n background-color: ", " !important;\n }\n "])), color, color, theme.colorTextReverse, color, theme.colorTextReverse, (0, _toolkit.darken)(color, 0.25), (0, _toolkit.darken)(color, 0.25)); // non-primary return (0, _styledComponents.css)(_templateObject6 || (_templateObject6 = _taggedTemplateLiteral(["\n color: ", ";\n border-color: ", ";\n\n &:hover,\n &:focus {\n color: ", ";\n border-color: ", ";\n }\n\n &:active {\n color: ", ";\n border-color: ", ";\n }\n "])), color, color, (0, _toolkit.darken)(color, 0.25), (0, _toolkit.darken)(color, 0.25), (0, _toolkit.darken)(color, 0.25), (0, _toolkit.darken)(color, 0.25)); }, (0, _toolkit.grid)(4)); /** * API is the same as https://ant.design/components/button/#API, except for the * `danger` prop, which is ommited in favour of `status`, described below. */ var Button = function Button(props) { var children = props.children, className = props.className, autoFocus = props.autoFocus, rest = _objectWithoutProperties(props, _excluded); var passProps = (0, _lodash.omit)(rest, 'danger'); var buttonRef = (0, _react.useRef)(null); (0, _react.useEffect)(function () { if (autoFocus) { var _buttonRef$current; (_buttonRef$current = buttonRef.current) === null || _buttonRef$current === void 0 ? void 0 : _buttonRef$current.focus(); } }, []); return /*#__PURE__*/_react["default"].createElement(StyledButton, _extends({ className: className, ref: buttonRef }, passProps), children); }; Button.propTypes = { status: _propTypes["default"].oneOf(['error', 'danger', 'success']), autoFocus: _propTypes["default"].bool }; Button.defaultProps = { status: null, autoFocus: false }; var _default = Button; exports["default"] = _default;