lucid-ui
Version:
A UI component library from AppNexus.
89 lines (82 loc) • 3.49 kB
JavaScript
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" && Symbol.iterator in Object(iter)) 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; }
import React from 'react';
import createClass from 'create-react-class';
import { IconSelect, ClockIcon } from '../../../index';
export default createClass({
getInitialState: function getInitialState() {
return {
selectedIcons: [{
id: 'item2',
isPartial: true
}]
};
},
isSelected: function isSelected(id) {
return this.state.selectedIcons.some(function (_ref) {
var selectedId = _ref.id;
return id === selectedId;
});
},
isPartiallySelected: function isPartiallySelected(id) {
return this.state.selectedIcons.some(function (_ref2) {
var selectedId = _ref2.id,
isPartial = _ref2.isPartial;
return id === selectedId && isPartial;
});
},
handleSelect: function handleSelect(selectedId) {
var selectedIcons = this.state.selectedIcons;
if (this.isPartiallySelected(selectedId)) {
// if partially selected, remove from the list
this.setState({
selectedIcons: selectedIcons.filter(function (_ref3) {
var id = _ref3.id;
return id !== selectedId;
})
});
} else if (this.isSelected(selectedId)) {
// if already selected, ensure isPartial is true
this.setState({
selectedIcons: selectedIcons.map(function (_ref4) {
var id = _ref4.id,
isPartial = _ref4.isPartial;
return {
id: id,
isPartial: isPartial || id === selectedId
};
})
});
} else {
// add selected icon to list
this.setState({
selectedIcons: [].concat(_toConsumableArray(selectedIcons), [{
id: selectedId,
isPartial: false
}])
});
}
},
render: function render() {
return /*#__PURE__*/React.createElement(IconSelect, {
onSelect: this.handleSelect,
items: [{
id: 'item1',
icon: /*#__PURE__*/React.createElement(ClockIcon, null),
isSelected: this.isSelected('item1'),
isPartial: this.isPartiallySelected('item1'),
label: 'Foo Bar'
}, {
id: 'item2',
icon: /*#__PURE__*/React.createElement(ClockIcon, null),
isSelected: this.isSelected('item2'),
isPartial: this.isPartiallySelected('item2'),
label: 'Bax Tar'
}]
});
}
});