@shakthillc/components
Version:
React generic components for shakthi products
252 lines (230 loc) • 7.84 kB
JavaScript
import _toConsumableArray from "babel-runtime/helpers/toConsumableArray";
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 from "react";
import PropTypes from "prop-types";
import style from "./UserSelector.module.css";
import Badge from "./Badge";
import { forEach } from "lodash";
var UserSelector = function (_React$Component) {
_inherits(UserSelector, _React$Component);
function UserSelector(props) {
_classCallCheck(this, UserSelector);
var _this = _possibleConstructorReturn(this, (UserSelector.__proto__ || _Object$getPrototypeOf(UserSelector)).call(this, props));
_this.onChange = _this.onChange.bind(_this);
_this.handleFocus = _this.handleFocus.bind(_this);
_this.handleBlur = _this.handleBlur.bind(_this);
_this.onKeyPress = _this.onKeyPress.bind(_this);
_this.onBadgeClick = _this.onBadgeClick.bind(_this);
_this.state = {
value: "",
details: _this.props.details || [],
helperText: false
};
return _this;
}
_createClass(UserSelector, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;
var _props$selectedValue = this.props.selectedValue,
selectedValue = _props$selectedValue === undefined ? [] : _props$selectedValue;
if (selectedValue && selectedValue.length > 0) {
var details = selectedValue.map(function (data) {
var mailFlag = _this2.mailValidator(data);
return {
email: data,
isValid: mailFlag === "#FFD4D1" ? 0 : 1,
color: mailFlag
};
});
this.setState({ details: details });
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
var _this3 = this;
var selectedValue = this.props.selectedValue;
if (selectedValue && prevProps.selectedValue.length != selectedValue.length) {
var formattedDetails = selectedValue.map(function (data) {
var mailFlag = _this3.mailValidator(data);
return {
email: data,
isValid: mailFlag === "#FFD4D1" ? 0 : 1,
color: mailFlag
};
});
this.setState({ details: formattedDetails });
}
}
}, {
key: "onChange",
value: function onChange(e) {
if (e.target.value !== ",") {
this.setState({ value: e.target.value });
}
}
}, {
key: "handleFocus",
value: function handleFocus(e) {
var onFocus = this.props.onFocus;
onFocus && onFocus(this.state.details);
}
}, {
key: "mailValidator",
value: function mailValidator(value) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value)) return "#E5E9EC";
return "#FFD4D1";
}
}, {
key: "statusFlag",
value: function statusFlag(data) {
var flag = true;
data.forEach(function (data) {
if (data.color === "#FFD4D1") {
flag = false;
return flag;
}
});
return flag;
}
}, {
key: "onKeyPress",
value: function onKeyPress(e) {
var onKeyup = this.props.onKeyup;
var _state = this.state,
value = _state.value,
details = _state.details;
var tempDetails = [].concat(_toConsumableArray(details));
var mailFlag = "";
var formattedResult = tempDetails.map(function (data) {
return data.email;
});
if (e.key === "Enter" || e.key === "Tab" || e.which === 32 || e.which === 44 && value.trim() != "") {
if (!formattedResult.includes(value)) {
mailFlag = this.mailValidator(value);
tempDetails.push({
email: value,
color: mailFlag,
isValid: mailFlag === "#FFD4D1" ? 0 : 1
});
value = "";
this.setState({ value: value, details: tempDetails, helperText: false });
var _formattedResult = tempDetails.map(function (data) {
return data.email;
});
onKeyup && onKeyup(_formattedResult, this.statusFlag(tempDetails));
} else {
this.setState({ value: "", helperText: true });
}
}
}
}, {
key: "onBadgeClick",
value: function onBadgeClick(text) {
var onKeyup = this.props.onKeyup;
var _state2 = this.state,
value = _state2.value,
details = _state2.details;
var tempDetails = details.filter(function (data) {
return data.email != text;
});
this.setState({ details: tempDetails });
var formattedResult = tempDetails.map(function (data) {
return data.email;
});
onKeyup && onKeyup(formattedResult, this.statusFlag(tempDetails));
}
}, {
key: "handleBlur",
value: function handleBlur(e) {
var onBlur = this.props.onBlur;
onBlur && onBlur(this.state.details);
}
}, {
key: "render",
value: function render() {
var _this4 = this;
var _props = this.props,
_props$placeHolder = _props.placeHolder,
placeHolder = _props$placeHolder === undefined ? "Enter text" : _props$placeHolder,
_props$isMandatory = _props.isMandatory,
isMandatory = _props$isMandatory === undefined ? false : _props$isMandatory,
label = _props.label,
help = _props.help,
_props$type = _props.type,
type = _props$type === undefined ? "text" : _props$type,
onChange = _props.onChange,
onKeyPress = _props.onKeyPress,
value = _props.value,
isPopupOpen = _props.isPopupOpen;
var helperText = this.state.helperText;
return React.createElement(
"div",
{ className: style["input-holder"] },
label && React.createElement(
"p",
{ className: style["input-holder__header"] },
label
),
React.createElement(
"div",
{
className: style["input-div"],
style: isMandatory === true ? { border: "solid 1px #FA383E" } : {}
},
this.state.details.map(function (data, i) {
return React.createElement(Badge, {
key: i,
onClick: _this4.onBadgeClick,
text: data.email,
color: data.color
});
}),
React.createElement("input", {
// type={type}
onFocus: this.handleFocus,
onBlur: this.handleBlur,
onKeyPress: this.onKeyPress,
onChange: onChange || this.onChange,
className: style["input-area"],
placeholder: placeHolder,
value: value || this.state.value
})
),
(help || helperText) && React.createElement(
"div",
{
className: isMandatory === true ? style["input-holder__helper--red"] : style["input-holder__helper"]
},
helperText && "The given email Id already exists" || help
)
);
}
}]);
return UserSelector;
}(React.Component);
export default UserSelector;
UserSelector.propTypes = {
placeHolder: PropTypes.string,
label: PropTypes.string,
help: PropTypes.string,
icon: PropTypes.string,
value: PropTypes.string,
onKeyup: PropTypes.func,
isMandatory: PropTypes.bool
};
UserSelector.defaultProps = {
icon_position: "left",
placeHolder: "Enter text",
value: "",
isMandatory: false,
type: "text",
onKeyup: function onKeyup() {
//console.log("keyup");
}
};