UNPKG

camelot-unchained

Version:
79 lines (67 loc) 4.4 kB
/** * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ "use strict"; /** * Materialize dropdown inspired quickselect list. * * *requires materialize js & css to be included on your html page. * * USAGE: * * class MyQuickSelect extends React.Component<any, any> { * generateActiveView = (item: any) => { * return <div>{item.foo}</div>; * } * generateListView = (item: any) => { * return <div>{item.foo}</div>; * } * onSelectedItemChanged = (item: any) => { * console.log('selected item is ' + item.foo); * } * render() { * let items = [{foo:'Hello'},{foo:'World'}]; * return <QuickSelect items={items} activeViewComponentGenerator={this.generateActiveView} listViewComponentGenerator={this.generateListView} onSelectedItemChanged={this.onSelectedChannelChanged} />; * } * } * */ var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; } 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; } var React = require('react'); var QuickSelect = function (_React$Component) { _inherits(QuickSelect, _React$Component); function QuickSelect(props) { _classCallCheck(this, QuickSelect); var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(QuickSelect).call(this, props)); _this.uniqueId = 'QuickSelect-' + QuickSelect.idCounter++; _this.onItemSelect = function (item, itemIndex) { _this.setState({ selectedIndex: itemIndex }); _this.props.onSelectedItemChanged(item); }; _this.buildListItem = function (item, itemIndex) { return React.createElement("div", { key: itemIndex, onClick: _this.onItemSelect.bind(_this, item, itemIndex), className: 'quickselect-auto-width' }, _this.props.listViewComponentGenerator(item)); }; _this.state = { selectedIndex: 0 }; return _this; } _createClass(QuickSelect, [{ key: 'render', value: function render() { if (this.props.items.length == 0) return React.createElement("div", null, "No Elements"); var selectedIndex = this.props.selectedItemIndex !== undefined ? this.props.selectedItemIndex : this.state.selectedIndex; return React.createElement("div", null, React.createElement("div", { className: 'dropdown-button quickselect-auto-width', "data-beloworigin": 'true', "data-constrainwidth": 'false', "data-verticaloffset": '0', "data-activates": this.uniqueId, "data-style": 'quickselect-default' }, this.props.activeViewComponentGenerator(this.props.items[selectedIndex])), React.createElement("div", { id: this.uniqueId, className: 'quickselect-default' }, this.props.items.map(this.buildListItem))); } }]); return QuickSelect; }(React.Component); QuickSelect.idCounter = 0; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = QuickSelect;