@shakthillc/components
Version:
React generic components for shakthi products
112 lines (101 loc) • 3.36 kB
JavaScript
import _Object$getPrototypeOf from "babel-runtime/core-js/object/get-prototype-of";
import _classCallCheck from "babel-runtime/helpers/classCallCheck";
import _createClass from "babel-runtime/helpers/createClass";
import _possibleConstructorReturn from "babel-runtime/helpers/possibleConstructorReturn";
import _inherits from "babel-runtime/helpers/inherits";
import React, { Component } from "react";
import PropTypes from "prop-types";
import style from "./DropList.module.css";
var DropList = function (_Component) {
_inherits(DropList, _Component);
function DropList(props) {
_classCallCheck(this, DropList);
var _this = _possibleConstructorReturn(this, (DropList.__proto__ || _Object$getPrototypeOf(DropList)).call(this, props));
_this.state = { isOpen: false, selectedOption: {} };
_this.handleClick = _this.handleClick.bind(_this);
_this.hideDropdownMenu = _this.hideDropdownMenu.bind(_this);
return _this;
}
_createClass(DropList, [{
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
var options = this.props.options;
if (options && options != prevProps.options) {
this.setState({ options: options });
}
}
}, {
key: "handleClick",
value: function handleClick() {
var _this2 = this;
this.setState({ isOpen: !this.state.isOpen }, function () {
return document.addEventListener("click", _this2.hideDropdownMenu);
});
}
}, {
key: "hideDropdownMenu",
value: function hideDropdownMenu() {
var _this3 = this;
this.setState({ isOpen: false }, function () {
return document.removeEventListener("click", _this3.hideDropdownMenu);
});
}
}, {
key: "render",
value: function render() {
var _state = this.state,
isOpen = _state.isOpen,
selectedOption = _state.selectedOption;
var _props = this.props,
options = _props.options,
_onClick = _props.onClick,
text = _props.text;
return React.createElement(
"div",
null,
React.createElement(
"div",
{ className: style["droplist"], onClick: this.handleClick },
text
),
isOpen && React.createElement(
"div",
{ className: style["droplist__menu"] },
options.map(function (data) {
return React.createElement(
"span",
{
className: style["droplist__menuitems"],
key: data.value || data,
value: "as",
onClick: function onClick() {
return _onClick(data.label || data);
}
},
data.label || data
);
})
)
);
}
}]);
return DropList;
}(Component);
DropList.defaultProps = {
options: [{ value: "sample1", label: "SAMPLE-1" }, { value: "sample2", label: "SAMPLE-2" }, { value: "sample3", label: "SAMPLE-3" }],
text: "...",
dataId: "dropdownComp",
onClick: function onClick(data) {
console.log(data);
}
};
DropList.propTypes = {
options: PropTypes.arrayOf({
value: PropTypes.string,
label: PropTypes.number
}),
handleClick: PropTypes.func,
handleSelect: PropTypes.func,
onClick: PropTypes.func
};
export default DropList;