lucid-ui
Version:
A UI component library from AppNexus.
296 lines (248 loc) • 15.3 kB
JavaScript
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); }
import _omit from "lodash/omit";
import _isNil from "lodash/isNil";
import _assign from "lodash/assign";
import _isNumber from "lodash/isNumber";
import _get from "lodash/get";
import _map from "lodash/map";
import _first from "lodash/first";
function _extends() { _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; }; return _extends.apply(this, arguments); }
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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
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; }
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import { findTypes, getFirst } from '../../util/component-types';
import { buildModernHybridComponent } from '../../util/state-management';
import * as reducers from './SingleSelect.reducers';
import { DropMenuDumb as DropMenu } from '../DropMenu/DropMenu';
import ChevronIcon from '../Icon/ChevronIcon/ChevronIcon';
var cx = lucidClassNames.bind('&-SingleSelect');
var any = PropTypes.any,
bool = PropTypes.bool,
func = PropTypes.func,
node = PropTypes.node,
number = PropTypes.number,
object = PropTypes.object,
shape = PropTypes.shape,
string = PropTypes.string,
oneOfType = PropTypes.oneOfType;
/** Placeholder Child Component */
var Placeholder = function Placeholder(_props) {
return null;
};
Placeholder.displayName = 'SingleSelect.Placeholder';
Placeholder.peek = {
description: "\n Content this is displayed when nothing is selected.\n "
};
Placeholder.propName = 'Placeholder';
/** Option Child Component */
var Selected = function Selected(_props) {
return null;
};
Selected.displayName = 'SingleSelect.Option.Selected';
Selected.peek = {
description: "\n Customizes the rendering of the Option when it is selected\n and is displayed instead of the Placeholder.\n "
};
Selected.propName = 'Selected';
Selected.propTypes = {};
var Option = function Option(_props) {
return null;
};
Option.displayName = 'SingleSelect.Option';
Option.peek = {
description: "\n A selectable option in the list.\n "
};
Option.Selected = Selected;
Option.propName = 'Option';
Option.propTypes = _objectSpread({
Selected: any
}, DropMenu.Option.propTypes);
Option.defaultProps = DropMenu.Option.defaultProps;
var OptionGroup = function OptionGroup(_props) {
return null;
};
OptionGroup.displayName = 'SingleSelect.OptionGroup';
OptionGroup.peek = {
description: "\n Groups `Option`s together with a non-selectable heading.\n "
};
OptionGroup.propName = 'OptionGroup';
OptionGroup.propTypes = DropMenu.OptionGroup.propTypes;
OptionGroup.defaultProps = DropMenu.OptionGroup.defaultProps;
var defaultProps = {
hasReset: true,
isSelectionHighlighted: true,
isDisabled: false,
isInvisible: false,
selectedIndex: null,
showIcon: true,
DropMenu: DropMenu.defaultProps
};
var SingleSelect = /*#__PURE__*/function (_React$Component) {
_inherits(SingleSelect, _React$Component);
var _super = _createSuper(SingleSelect);
function SingleSelect() {
_classCallCheck(this, SingleSelect);
return _super.apply(this, arguments);
}
_createClass(SingleSelect, [{
key: "UNSAFE_componentWillMount",
value: function UNSAFE_componentWillMount() {
// preprocess the options data before rendering
var _DropMenu$preprocessO = DropMenu.preprocessOptionData(this.props, SingleSelect),
optionGroups = _DropMenu$preprocessO.optionGroups,
flattenedOptionsData = _DropMenu$preprocessO.flattenedOptionsData,
ungroupedOptionData = _DropMenu$preprocessO.ungroupedOptionData,
optionGroupDataLookup = _DropMenu$preprocessO.optionGroupDataLookup;
this.setState({
optionGroups: optionGroups,
flattenedOptionsData: flattenedOptionsData,
ungroupedOptionData: ungroupedOptionData,
optionGroupDataLookup: optionGroupDataLookup
});
}
}, {
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps) {
// only preprocess options data when it changes (via new props) - better performance than doing this each render
var _DropMenu$preprocessO2 = DropMenu.preprocessOptionData(nextProps, SingleSelect),
optionGroups = _DropMenu$preprocessO2.optionGroups,
flattenedOptionsData = _DropMenu$preprocessO2.flattenedOptionsData,
ungroupedOptionData = _DropMenu$preprocessO2.ungroupedOptionData,
optionGroupDataLookup = _DropMenu$preprocessO2.optionGroupDataLookup;
this.setState({
optionGroups: optionGroups,
flattenedOptionsData: flattenedOptionsData,
ungroupedOptionData: ungroupedOptionData,
optionGroupDataLookup: optionGroupDataLookup
});
}
}, {
key: "render",
value: function render() {
var _this$props = this.props,
style = _this$props.style,
className = _this$props.className,
hasReset = _this$props.hasReset,
isDisabled = _this$props.isDisabled,
isInvisible = _this$props.isInvisible,
isSelectionHighlighted = _this$props.isSelectionHighlighted,
selectedIndex = _this$props.selectedIndex,
maxMenuHeight = _this$props.maxMenuHeight,
onSelect = _this$props.onSelect,
showIcon = _this$props.showIcon,
dropMenuProps = _this$props.DropMenu;
var direction = dropMenuProps.direction,
isExpanded = dropMenuProps.isExpanded,
flyOutStyle = dropMenuProps.flyOutStyle;
var _this$state = this.state,
optionGroups = _this$state.optionGroups,
optionGroupDataLookup = _this$state.optionGroupDataLookup,
ungroupedOptionData = _this$state.ungroupedOptionData,
flattenedOptionsData = _this$state.flattenedOptionsData;
var placeholderProps = _first(_map(findTypes(this.props, SingleSelect.Placeholder), 'props'));
var placeholder = _get(placeholderProps, 'children', 'Select');
var isItemSelected = _isNumber(selectedIndex);
var isHighlighted = !isDisabled && isItemSelected && isSelectionHighlighted || isExpanded && isSelectionHighlighted;
var isNullOptionSelected = selectedIndex === null;
return /*#__PURE__*/React.createElement(DropMenu, _extends({}, dropMenuProps, {
isDisabled: isDisabled,
selectedIndices: _isNumber(selectedIndex) ? [selectedIndex] : [],
className: cx('&', className),
onSelect: onSelect,
style: style,
flyOutStyle: _assign({}, flyOutStyle, !_isNil(maxMenuHeight) ? {
maxHeight: maxMenuHeight
} : null),
ContextMenu: {
directonOffset: isNullOptionSelected ? -1 : 0
}
}), /*#__PURE__*/React.createElement(DropMenu.Control, null, /*#__PURE__*/React.createElement("div", {
tabIndex: 0,
className: cx('&-Control', {
'&-Control-is-highlighted': isHighlighted,
'&-Control-is-selected': isHighlighted,
'&-Control-is-expanded': isExpanded,
'&-Control-is-disabled': isDisabled,
'&-Control-is-invisible': isInvisible,
'&-Control-is-null-option': isNullOptionSelected
})
}, /*#__PURE__*/React.createElement("span", _extends({}, !isItemSelected ? placeholderProps : null, {
className: cx('&-Control-content', !isItemSelected ? _get(placeholderProps, 'className') : null)
}), isItemSelected ? _get(getFirst(flattenedOptionsData[selectedIndex].optionProps, SingleSelect.Option.Selected), 'props.children') || flattenedOptionsData[selectedIndex].optionProps.children : placeholder), showIcon && /*#__PURE__*/React.createElement(ChevronIcon, {
size: 12,
direction: isExpanded ? direction : 'down'
}))), hasReset && isItemSelected ? /*#__PURE__*/React.createElement(DropMenu.NullOption, placeholderProps, placeholder) : null, // for each option group passed in, render a DropMenu.OptionGroup, any label will be included in it's children, render each option inside the group
_map(optionGroups, function (optionGroupProps, optionGroupIndex) {
return /*#__PURE__*/React.createElement(DropMenu.OptionGroup, _extends({
key: 'SingleSelectOptionGroup' + optionGroupIndex
}, optionGroupProps), optionGroupProps.children, _map(_get(optionGroupDataLookup, optionGroupIndex), function (_ref) {
var optionProps = _ref.optionProps,
optionIndex = _ref.optionIndex;
return /*#__PURE__*/React.createElement(DropMenu.Option, _extends({
key: 'SingleSelectOption' + optionIndex
}, _omit(optionProps, 'Selected')));
}));
}).concat( // then render all the ungrouped options at the end
_map(ungroupedOptionData, function (_ref2) {
var optionProps = _ref2.optionProps,
optionIndex = _ref2.optionIndex;
return /*#__PURE__*/React.createElement(DropMenu.Option, _extends({
key: 'SingleSelectOption' + optionIndex
}, _omit(optionProps, 'Selected')));
})));
}
}]);
return SingleSelect;
}(React.Component);
_defineProperty(SingleSelect, "displayName", 'SingleSelect');
_defineProperty(SingleSelect, "peek", {
description: "\n\t\t\t\t`SingleSelect` is a dropdown list. \n\t\t\t\t",
notes: {
overview: "\n\t\t\t\t\t\tA dropdown list. When you click on the trigger a dropdown menu appears, allows you to choose one option, and execute relevant actions.\n\t\t\t\t\t",
intendedUse: "\n\t\t\t\t\t\tAllow users to select one option from a list of 3-10 options.\n\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t**Styling notes**\n\t\t\t\t\t\t\n\t\t\t\t\t\t- Use the default style in forms. The blue outline helps users clearly see that a selection has been made.\n\t\t\t\t\t\t- Use `isSelectedHighlighted='false'` if the default selection is All or a null state. See the `no selection highlighting` example.\n\t\t\t\t\t\t- Use `isInvisible` for filters within a full page table header. See the `invisible` example.\n\t\t\t\t\t",
technicalRecommendations: "\n\t\t\t\t\t"
},
categories: ['controls', 'selectors'],
madeFrom: ['DropMenu']
});
_defineProperty(SingleSelect, "defaultProps", defaultProps);
_defineProperty(SingleSelect, "reducers", reducers);
_defineProperty(SingleSelect, "Placeholder", Placeholder);
_defineProperty(SingleSelect, "Option", Option);
_defineProperty(SingleSelect, "Selected", Selected);
_defineProperty(SingleSelect, "OptionGroup", OptionGroup);
_defineProperty(SingleSelect, "NullOption", DropMenu.NullOption);
_defineProperty(SingleSelect, "FixedOption", DropMenu.FixedOption);
_defineProperty(SingleSelect, "propTypes", {
children: node,
className: string,
style: object,
isSelectionHighlighted: bool,
hasReset: bool,
isDisabled: bool,
isInvisible: bool,
selectedIndex: number,
maxMenuHeight: oneOfType([number, string]),
showIcon: bool,
DropMenu: shape(DropMenu.propTypes),
onSelect: func,
Placeholder: any,
Option: any,
OptionGroup: any
});
export default buildModernHybridComponent(SingleSelect, {
reducers: reducers
});
export { SingleSelect as SingleSelectDumb };