UNPKG

@enact/ui

Version:

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

289 lines (284 loc) 11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.ButtonDecorator = exports.ButtonBase = exports.Button = void 0; 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 _ComponentOverride = _interopRequireDefault(require("../ComponentOverride")); var _ForwardRef = _interopRequireDefault(require("../ForwardRef")); var _Touchable = _interopRequireDefault(require("../Touchable")); var _ButtonModule = _interopRequireDefault(require("./Button.module.css")); var _jsxRuntime = require("react/jsx-runtime"); var _excluded = ["children", "componentRef", "css", "decoration", "disabled", "icon"]; function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } 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(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : 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); } /** * Unstyled button components and behaviors to be customized by a theme or application. * * @module ui/Button * @exports Button * @exports ButtonBase * @exports ButtonDecorator */ /** * A basic button component structure without any behaviors applied to it. * * @class ButtonBase * @memberof ui/Button * @ui * @public */ var ButtonBase = exports.ButtonBase = (0, _kind["default"])({ name: 'ui:Button', propTypes: /** @lends ui/Button.ButtonBase.prototype */{ /** * Called with a reference to the root component. * * When using {@link ui/Button.Button}, 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: * * * `button` - The root component class * * `bg` - The background node of the button * * `client` - The content node of the button * * `hasIcon` - Applied when there is an `icon` present * * `icon` - The icon node, when `icon` is set * * `large` - Applied when `size` prop is `'large'` * * `minWidth` - Applied when `minWidth` prop is `true` * * `pressed` - Applied when `pressed` prop is `true` * * `selected` - Applied when `selected` prop is `true` * * `small` - Applied when `size` prop is `'small'` * * @type {Object} * @public */ css: _propTypes2["default"].object, /** * Additional DOM nodes which may be necessary for decorating the Button. * * @type {Node} * @private */ decoration: _propTypes2["default"].node, /** * Applies the `disabled` class. * * When `true`, the button is shown as disabled. * * @type {Boolean} * @default false * @public */ disabled: _propTypes2["default"].bool, /** * The icon displayed within the Button. * * The icon will be displayed before the natural reading order of the text, regardless * of locale. Any string that is valid for its {@link ui/Button.Button.iconComponent} is * valid here. If `icon` is specified as a string and `iconButton` is undefined, the icon is * not rendered. * * This also supports a custom icon, in the form of a DOM node or a Component, * with the caveat that if you supply a custom icon, you are responsible for sizing and * locale positioning of the custom component. * * Setting this to `true` means the `iconComponent` will be rendered but with empty content. * This may be useful if you've customized the `iconComponent` to render the icon content * externally. * * @type {Node|Boolean} * @public */ icon: _propTypes2["default"].oneOfType([_propTypes2["default"].node, _propTypes2["default"].bool]), /** * The component used to render the {@link ui/Button.ButtonBase.icon|icon}. * * This component will receive the `icon` class to customize its styling. * If {@link ui/Button.ButtonBase.icon|icon} is not assigned or is false, this component * will not be rendered. * * If this is a component rather than an HTML element string, this component will also * receive the `size` and `iconFlip` (as `flip`) properties and should be configured to * handle it. * * @type {Component|Node} * @public */ iconComponent: _propTypes["default"].componentOverride, /** * Flips the icon. * * @see {@link ui/Icon.IconBase.flip} * @type {String} * @public */ iconFlip: _propTypes2["default"].string, /** * Enforces a minimum width for the component. * * Applies the `minWidth` CSS class which can be customized by * {@link /docs/developer-guide/theming/|theming}. * * @type {Boolean} * @default true * @public */ minWidth: _propTypes2["default"].bool, /** * Indicates the component is depressed. * * Applies the `pressed` CSS class which can be customized by * {@link /docs/developer-guide/theming/|theming}. * * @type {Boolean} * @default false * @public */ pressed: _propTypes2["default"].bool, /** * Indicates the component is selected. * * Applies the `selected` CSS class which can be customized by * {@link /docs/developer-guide/theming/|theming}. * * @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, minWidth: true, pressed: false, selected: false }, styles: { css: _ButtonModule["default"], className: 'button', publicClassNames: true }, computed: { className: function className(_ref) { var icon = _ref.icon, minWidth = _ref.minWidth, pressed = _ref.pressed, selected = _ref.selected, size = _ref.size, styler = _ref.styler; return styler.append({ hasIcon: !!icon, minWidth: minWidth, pressed: pressed, selected: selected }, size); }, icon: function icon(_ref2) { var css = _ref2.css, _icon = _ref2.icon, iconComponent = _ref2.iconComponent, iconFlip = _ref2.iconFlip, size = _ref2.size; if (_icon == null || _icon === false) return; // Establish the base collection of props for the most basic `iconComponent` type, an // HTML element string. var props = { className: css.icon, component: iconComponent }; // Add in additional props that any Component supplied to `iconComponent` should be // configured to handle. if (typeof iconComponent !== 'string') { props.size = size; props.flip = iconFlip; } if (_icon !== true) { props.children = _icon; } return /*#__PURE__*/(0, _jsxRuntime.jsx)(_ComponentOverride["default"], _objectSpread({}, props)); } }, render: function render(_ref3) { var children = _ref3.children, componentRef = _ref3.componentRef, css = _ref3.css, decoration = _ref3.decoration, disabled = _ref3.disabled, icon = _ref3.icon, rest = _objectWithoutProperties(_ref3, _excluded); delete rest.iconComponent; delete rest.iconFlip; delete rest.minWidth; delete rest.pressed; delete rest.selected; delete rest.size; return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", _objectSpread(_objectSpread({ role: "button" }, rest), {}, { "aria-disabled": disabled, disabled: disabled, ref: componentRef, children: [decoration ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: css.decoration, children: decoration }) : null, /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: css.bg }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", { className: css.client, children: [icon, children] })] })); } }); /** * A higher-order component that adds touch support to a {@link ui/Button.ButtonBase|ButtonBase}. * * @hoc * @memberof ui/Button * @mixes ui/Touchable.Touchable * @public */ var ButtonDecorator = exports.ButtonDecorator = (0, _compose["default"])((0, _ForwardRef["default"])({ prop: 'componentRef' }), (0, _Touchable["default"])({ activeProp: 'pressed' })); /** * A minimally-styled button component with touch support. * * @class Button * @memberof ui/Button * @extends ui/Button.ButtonBase * @mixes ui/Button.ButtonDecorator * @omit componentRef * @ui * @public */ var Button = exports.Button = ButtonDecorator(ButtonBase); var _default = exports["default"] = Button;