IconPickerComponent.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. 'use strict';
  2. var _extends = Object.assign || 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; };
  3. function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
  4. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  5. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  6. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  7. /**
  8. * Licensed Materials - Property of IBM
  9. * IBM Cognos Products: Dashboard
  10. * (C) Copyright IBM Corp. 2019
  11. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  12. */
  13. define(['react', 'react-dom', 'prop-types', 'ca-ui-toolkit', 'dashboard-analytics/lib/@waca/core-client/js/core-client/ui/KeyCodes'], function (React, ReactDOM, PropTypes, Toolkit, KeyCodes) {
  14. var _class, _temp;
  15. var iconpickerClassName = 'iconpicker';
  16. var Flyout = Toolkit.Flyout,
  17. SVGIcon = Toolkit.SVGIcon,
  18. propsUtils = Toolkit.propsUtils,
  19. lodash = Toolkit.lodash;
  20. var sanitize = propsUtils.sanitize;
  21. var uniqueId = lodash.uniqueId;
  22. var Component = React.Component;
  23. /**
  24. * @class IconPickerComponent
  25. * @classdesc React component to display a flyout containing selectable icons
  26. */
  27. var IconPickerComponent = (_temp = _class = function (_Component) {
  28. _inherits(IconPickerComponent, _Component);
  29. function IconPickerComponent(props) {
  30. _classCallCheck(this, IconPickerComponent);
  31. var _this = _possibleConstructorReturn(this, _Component.call(this, props));
  32. _this._displayIcon = function (icon, index) {
  33. return React.createElement(
  34. 'div',
  35. { key: index,
  36. tabIndex: '0' // Make it keyboard focusable
  37. , className: iconpickerClassName + '__cell',
  38. onClick: function onClick() {
  39. return _this._handleSelect(icon);
  40. },
  41. onKeyDown: function onKeyDown(e) {
  42. return _this._handleKey(e, icon);
  43. },
  44. title: icon.label
  45. },
  46. React.createElement(SVGIcon, {
  47. iconId: icon.id,
  48. alt: icon.label,
  49. ariaLabelledby: uniqueId('icon-picker-svg'),
  50. verticalAlign: 'inherit'
  51. })
  52. );
  53. };
  54. _this.state = { open: true };
  55. _this._handleClose = _this._handleClose.bind(_this);
  56. _this._handleSelect = _this._handleSelect.bind(_this);
  57. _this._handleKey = _this._handleKey.bind(_this);
  58. _this._id = uniqueId('icon-picker__');
  59. return _this;
  60. }
  61. IconPickerComponent.prototype.render = function render() {
  62. var open = this.state.open;
  63. var _props = this.props,
  64. className = _props.className,
  65. style = _props.style,
  66. flyoutExtraOptions = _props.flyoutExtraOptions,
  67. triggerNode = _props.triggerNode,
  68. domNodeToAttachTo = _props.domNodeToAttachTo,
  69. others = _objectWithoutProperties(_props, ['className', 'style', 'flyoutExtraOptions', 'triggerNode', 'domNodeToAttachTo']);
  70. var orientation = this.props.orientation;
  71. if (typeof orientation === 'undefined') {
  72. orientation = document.body.dir === 'rtl' ? 'bottomLeft' : 'bottomRight';
  73. }
  74. return React.createElement(
  75. 'div',
  76. _extends({}, sanitize(others, IconPickerComponent), { className: iconpickerClassName + ' ' + className, style: style }),
  77. open && React.createElement(
  78. Flyout,
  79. _extends({
  80. id: this._id + '-flyout',
  81. 'data-tid': 'icon-picker__flyout'
  82. }, flyoutExtraOptions, {
  83. 'aria-hidden': open,
  84. placement: orientation,
  85. domNodeToAttachTo: domNodeToAttachTo,
  86. hideArrow: true,
  87. offset: 2,
  88. onClose: this._handleClose,
  89. className: iconpickerClassName + '__flyout',
  90. triggerNode: triggerNode
  91. }),
  92. this.props.icons.map(this._displayIcon)
  93. )
  94. );
  95. };
  96. IconPickerComponent.prototype._handleClose = function _handleClose() {
  97. this.setState(function (state) {
  98. return { open: !state.open };
  99. });
  100. this.props.onClose && this.props.onClose();
  101. };
  102. IconPickerComponent.prototype._handleSelect = function _handleSelect(icon) {
  103. this.props.onIconSelected && this.props.onIconSelected(icon);
  104. // close the flyout after selection
  105. this.setState({ open: false });
  106. };
  107. IconPickerComponent.prototype._handleKey = function _handleKey(e, icon) {
  108. if (e.keyCode === KeyCodes.ENTER || e.keyCode === KeyCodes.SPACE) {
  109. this._handleSelect(icon);
  110. // Prevent browser's default action: space does a PageDown
  111. e.preventDefault();
  112. }
  113. };
  114. return IconPickerComponent;
  115. }(Component), _class.propTypes = {
  116. /** Icon objects containing id and label to be displayed */
  117. icons: PropTypes.array.isRequired,
  118. /** Function that will get called when selecting an icon */
  119. onIconSelected: PropTypes.func,
  120. /** Function that will get called when flyout is closed */
  121. onClose: PropTypes.func,
  122. /** Flyout.propTypes.placement */
  123. orientation: Flyout.propTypes.placement,
  124. /** Custom class name(s) */
  125. className: PropTypes.string,
  126. /** Custom css styles */
  127. style: PropTypes.object,
  128. /** Trigger node to orient the flyout to. Defaults to the parent node of the flyout */
  129. triggerNode: PropTypes.object,
  130. /** Container node to place the flyout in */
  131. domNodeToAttachTo: PropTypes.object,
  132. /** Extra options to pass to Flyout */
  133. flyoutExtraOptions: PropTypes.object
  134. }, _class.defaultProps = {
  135. icons: [],
  136. className: '',
  137. style: {},
  138. flyoutExtraOptions: {}
  139. }, _temp);
  140. return IconPickerComponent;
  141. });
  142. //# sourceMappingURL=IconPickerComponent.js.map