UNPKG

react-kube

Version:

Kube CSS in React Components

171 lines (143 loc) 6.95 kB
"use strict"; 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; }; })(); var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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) subClass.__proto__ = superClass; } var _react = require("react"); var _react2 = _interopRequireDefault(_react); var _classnames = require("classnames"); var _classnames2 = _interopRequireDefault(_classnames); var Filterbox = (function (_React$Component) { function Filterbox(props) { _classCallCheck(this, Filterbox); _get(Object.getPrototypeOf(Filterbox.prototype), "constructor", this).call(this, props); this.state = { show: this.props.show }; } _inherits(Filterbox, _React$Component); _createClass(Filterbox, [{ key: "componentDidMount", value: function componentDidMount() { var togglePosTop = _react2["default"].findDOMNode(this.refs.filterInput).offsetTop + _react2["default"].findDOMNode(this.refs.filterInput).offsetHeight / 2; var togglePosLeft = _react2["default"].findDOMNode(this.refs.filterInput).offsetLeft + _react2["default"].findDOMNode(this.refs.filterInput).offsetWidth - 25; this.setState({ togglePos: { left: togglePosLeft, top: togglePosTop } }); //Calculate width of the input field to equal the suggestions lists width this.inputWidth = _react2["default"].findDOMNode(this.refs.filterInput).offsetWidth; //mousedown listener, used to detect clicks outside of the dropdown window.addEventListener("mousedown", this.closeFilterbox.bind(this)); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { //mousedown click listener window.removeEventListener("mousedown", this.closeFilterbox.bind(this)); } }, { key: "closeFilterbox", value: function closeFilterbox() { if (!this.mouseOnFilterbox) { this.setState({ show: false }); } } }, { key: "toggleFilterbox", value: function toggleFilterbox() { var shown = this.state.show; this.setState({ show: !shown }); } }, { key: "handleMouseDown", value: function handleMouseDown() { this.mouseOnFilterbox = true; } }, { key: "handleMouseUp", value: function handleMouseUp() { this.mouseOnFilterbox = false; } }, { key: "handleChange", value: function handleChange(e) { // update input value this.setState({ value: e.target.value }); // when available, execute the onChange function of the parent this.props.onChange ? this.props.onChange(e.target.value) : null; //eslint-disable-line } }, { key: "handleSelectItem", value: function handleSelectItem(item) { // set value and hide filterbox this.setState({ show: false, value: item }); // when available, execute the onChange function of the parent this.props.onChange ? this.props.onChange(item) : null; //eslint-disable-line } }, { key: "render", value: function render() { var _this = this; var filterClasses = (0, _classnames2["default"])({ "filterbox": true }); var toggleClasses = { "filterbox-toggle": true }; var toggleStyle = { left: this.state.togglePos ? this.state.togglePos.left : 0, top: this.state.togglePos ? this.state.togglePos.top : 0 }; var listClasses = (0, _classnames2["default"])({ "filterbox-list": true }); var listStyle = { display: this.state.show ? "block" : "none", width: this.inputWidth }; var listItems = this.props.data.map(function (item, i) { return _react2["default"].createElement( "li", { key: i, onClick: _this.handleSelectItem.bind(_this, item) }, item ); }); return _react2["default"].createElement( "div", { className: (0, _classnames2["default"])(this.props.className, filterClasses), onMouseDown: this.handleMouseDown.bind(this), onMouseUp: this.handleMouseUp.bind(this), style: this.props.style }, _react2["default"].createElement("input", { className: this.props.inputClassName, id: this.props.id, onChange: this.handleChange.bind(this), placeholder: this.props.placeholder, ref: "filterInput", required: this.props.required, type: "text", value: this.state.value }), _react2["default"].createElement("span", { className: (0, _classnames2["default"])(this.props.toggleClassName, toggleClasses), onClick: this.toggleFilterbox.bind(this), ref: "filterToggle", style: toggleStyle }), _react2["default"].createElement( "ul", { className: (0, _classnames2["default"])(this.props.listClassName, listClasses), style: listStyle }, listItems ) ); } }]); return Filterbox; })(_react2["default"].Component); Filterbox.propTypes = { children: _react2["default"].PropTypes.node, className: _react2["default"].PropTypes.string, data: _react2["default"].PropTypes.array.isRequired, id: _react2["default"].PropTypes.string, inputClassName: _react2["default"].PropTypes.string, listClassName: _react2["default"].PropTypes.string, onChange: _react2["default"].PropTypes.func, placeholder: _react2["default"].PropTypes.string, required: _react2["default"].PropTypes.bool, show: _react2["default"].PropTypes.bool, style: _react2["default"].PropTypes.object, toggleClassName: _react2["default"].PropTypes.string }; Filterbox.defaultProps = { show: false }; module.exports = Filterbox;