UNPKG

@enact/ui

Version:

A collection of simplified unstyled cross-platform UI components for Enact

253 lines (250 loc) 10.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.IconButtonDecorator = exports.IconButtonBase = exports.IconButton = void 0; var _deprecate = _interopRequireDefault(require("@enact/core/internal/deprecate")); var _propTypes = _interopRequireDefault(require("@enact/core/internal/prop-types")); var _kind = _interopRequireDefault(require("@enact/core/kind")); var _propTypes2 = _interopRequireDefault(require("prop-types")); var _compose = _interopRequireDefault(require("ramda/src/compose")); var _react = require("react"); var _ComponentOverride = _interopRequireDefault(require("../ComponentOverride")); var _ForwardRef = _interopRequireDefault(require("../ForwardRef")); var _Touchable = _interopRequireDefault(require("../Touchable")); var _IconButtonModule = _interopRequireDefault(require("./IconButton.module.css")); var _jsxRuntime = require("react/jsx-runtime"); var _excluded = ["buttonComponent", "children", "componentRef", "css", "flip", "icon", "iconComponent", "size"]; /** * An {@link ui/Icon.Icon|Icon} that acts like a {@link ui/Button.Button|Button}. * * You may specify an image or a font-based icon by setting the `children` to either the path * to the image or a string from an {@link ui/Icon.Icon.iconList|iconList}. This is unstyled, * but can easily be extended and customized by a theme or application. * * @module ui/IconButton * @exports IconButton * @exports IconButtonBase * @exports IconButtonDecorator * @deprecated Will be removed in 5.0.0. Use {@link ui/Button} instead. */ /** * A ui-styled button without any behavior. * * @class IconButtonBase * @memberof ui/IconButton * @ui * @public */ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } 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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } 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; } var IconButtonBase = exports.IconButtonBase = (0, _kind["default"])({ name: 'ui:IconButton', propTypes: /** @lends ui/IconButton.IconButtonBase.prototype */{ /** * This is the root component used to render the button and will receive all props except * `icon`. * * @type {Component|Element} * @required * @public */ buttonComponent: _propTypes["default"].componentOverride.isRequired, /** * The component used to render the {@link ui/IconButton.IconButtonBase.icon|icon}. * * This component will receive the `flip` and `size` property set on the `IconButton` as well as the * `icon` class to customize its styling. * * @type {Component} * @required * @public */ iconComponent: _propTypes["default"].component.isRequired, /** * Additional children that follow the icon. * * If `icon` isn't specified but `children` is, `children` is used as the icon's value. * * @see {@link ui/Icon.IconBase.children} * @type {Node} * @public */ children: _propTypes2["default"].node, /** * Called with a reference to the root component. * * When using {@link ui/IconButton.IconButton}, the `ref` prop is forwarded to this * component as `componentRef`. * * @type {Object|Function} * @public */ componentRef: _propTypes["default"].ref, /** * Customizes the component by mapping the supplied collection of CSS class names to the * corresponding internal elements and states of this component. * * The following classes are supported: * * * `iconButton` - The root component class * * `icon` - The {@link ui/IconButton.IconButtonBase.iconComponent|icon component} class * * `large` - Applied when `size` prop is `'large'` * * `small` - Applied when `size` prop is `'small'` * * `pressed` - Applied when `pressed` prop is `true` * * @type {Object} * @public */ css: _propTypes2["default"].object, /** * Disables IconButton. * * When `true`, the button is shown as disabled and does not generate * `onClick` {@link /docs/developer-guide/glossary/#event|events}. * * @type {Boolean} * @default false * @public */ disabled: _propTypes2["default"].bool, /** * Flip the icon horizontally, vertically or both. * * @type {('both'|'horizontal'|'vertical')} * @public */ flip: _propTypes2["default"].string, /** * The icon displayed within the IconButton. * * If not specified, `children` is used as the icon value instead. * * @type {String} * @public */ icon: _propTypes2["default"].string, /** * Applies the `pressed` CSS class. * * @type {Boolean} * @default false * @public */ pressed: _propTypes2["default"].bool, /** * Applies the `selected` CSS class. * * @type {Boolean} * @default false * @public */ selected: _propTypes2["default"].bool, /** * The size of the button. * * Applies the CSS class which can be customized by * {@link /docs/developer-guide/theming/|theming}. * * @type {String} * @public */ size: _propTypes2["default"].string }, defaultProps: { disabled: false, pressed: false, selected: false }, styles: { css: _IconButtonModule["default"], className: 'iconButton', publicClassNames: true }, computed: { className: function className(_ref) { var size = _ref.size, styler = _ref.styler; return styler.append(size); } }, render: (0, _deprecate["default"])(function (_ref2) { var buttonComponent = _ref2.buttonComponent, children = _ref2.children, componentRef = _ref2.componentRef, css = _ref2.css, flip = _ref2.flip, icon = _ref2.icon, Icon = _ref2.iconComponent, size = _ref2.size, rest = _objectWithoutProperties(_ref2, _excluded); // To support the simpler use case of only specifying the icon as the children within // <IconButton>, this falls back on using children if icon isn't specified. if (!icon && children) { icon = children; children = null; } return (0, _ComponentOverride["default"])(_objectSpread(_objectSpread({}, rest), {}, { component: buttonComponent, size: size, minWidth: false, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Icon, { flip: flip, size: size, className: css.icon, children: icon }, "icon")].concat(_toConsumableArray(_react.Children.toArray(children))), ref: componentRef })); }, { name: 'ui/IconButton', replacedBy: 'ui/Button', until: '5.0.0' }) }); /** * A higher-order component that adds universal button behaviors to an {@link ui/IconButton.IconButtonBase|IconButtonBase}. * * @hoc * @memberof ui/IconButton * @mixes ui/ForwardRef.ForwardRef * @mixes ui/Touchable.Touchable * @public */ var IconButtonDecorator = exports.IconButtonDecorator = (0, _compose["default"])((0, _ForwardRef["default"])({ prop: 'componentRef' }), (0, _Touchable["default"])({ activeProp: 'pressed' })); /** * A minimally styled, but interactive, Button ready for customization by a theme. * * Example: * ``` * <IconButton size="small"> * plus * </IconButton> * ``` * * @class IconButton * @extends ui/IconButton.IconButtonBase * @mixes ui/IconButton.IconButtonDecorator * @omit componentRef * @memberof ui/IconButton * @ui * @public */ var IconButton = exports.IconButton = IconButtonDecorator(IconButtonBase); var _default = exports["default"] = IconButton;