@shakthillc/components
Version:
React generic components for shakthi products
242 lines (214 loc) • 9.58 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
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 _react = require("react");
var _react2 = _interopRequireDefault(_react);
var _propTypes = require("prop-types");
var _propTypes2 = _interopRequireDefault(_propTypes);
var _InputTextModule = require("./InputText.module.css");
var _InputTextModule2 = _interopRequireDefault(_InputTextModule);
var _Icon = require("@material-ui/core/Icon");
var _Icon2 = _interopRequireDefault(_Icon);
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 _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 InputText = function (_React$Component) {
_inherits(InputText, _React$Component);
function InputText(props) {
_classCallCheck(this, InputText);
var _this = _possibleConstructorReturn(this, (InputText.__proto__ || Object.getPrototypeOf(InputText)).call(this, props));
_this.state = {
value: "",
passwordIcon: "visibility",
passwordField: "password"
};
_this.onChange = _this.onChange.bind(_this);
_this.handleFocus = _this.handleFocus.bind(_this);
_this.handleBlur = _this.handleBlur.bind(_this);
_this.handleIcon = _this.handleIcon.bind(_this);
return _this;
}
_createClass(InputText, [{
key: "componentDidMount",
value: function componentDidMount() {
this.setState({
value: this.props.value
});
}
}, {
key: "onChange",
value: function onChange(e) {
var onKeyup = this.props.onKeyup;
this.setState({ value: e.target.value });
onKeyup && onKeyup(e.target.value);
}
}, {
key: "handleFocus",
value: function handleFocus(e) {
var onFocus = this.props.onFocus;
//console.log("Focus",e.target.value)
onFocus && onFocus(e.target.value);
}
}, {
key: "handleBlur",
value: function handleBlur(e) {
var onBlur = this.props.onBlur;
//console.log("blur",e.target.value)
onBlur && onBlur(e.target.value);
}
}, {
key: "handleIcon",
value: function handleIcon() {
var passwordIcon = this.state.passwordIcon === "visibility_off" ? "visibility" : "visibility_off";
var passwordField = passwordIcon === "visibility" ? "password" : "text";
this.setState({ passwordIcon: passwordIcon, passwordField: passwordField });
}
}, {
key: "render",
value: function render() {
var _state = this.state,
passwordIcon = _state.passwordIcon,
passwordField = _state.passwordField;
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,
icon = _props.icon,
_props$type = _props.type,
type = _props$type === undefined ? "text" : _props$type,
onChange = _props.onChange,
value = _props.value,
icon_position = _props.icon_position,
_props$readOnly = _props.readOnly,
readOnly = _props$readOnly === undefined ? false : _props$readOnly,
_props$id = _props.id,
id = _props$id === undefined ? "defaultTextId" : _props$id;
var text_style = "input-area__icon--" + icon_position + readOnly;
var text_stylemain = readOnly ? "input-area__readonly" : "input-area";
var borderStyle = isMandatory == true ? _InputTextModule2.default["input-enableborder"] : "";
if (type === "password") {
return _react2.default.createElement(
"div",
{ className: _InputTextModule2.default["input-holder"] },
label && _react2.default.createElement(
"p",
{ "data-id": "label" + id, className: _InputTextModule2.default["input-holder__header"] },
label
),
_react2.default.createElement(
"div",
null,
_react2.default.createElement(
"div",
{ style: { position: "relative" } },
_react2.default.createElement(
"i",
{
onClick: this.handleIcon,
className: _InputTextModule2.default["input-holder__icon--right"]
},
_react2.default.createElement(
_Icon2.default,
{ style: { fontSize: 18 } },
passwordIcon
)
),
_react2.default.createElement("input", {
type: passwordField,
onFocus: this.handleFocus,
onBlur: this.handleBlur,
onChange: onChange || this.onChange,
style: this.props.style,
className: icon === undefined ? _InputTextModule2.default[text_stylemain] + " " + borderStyle : _InputTextModule2.default[text_style] + " " + borderStyle,
placeholder: placeHolder,
value: value === undefined || value === null ? this.state.value : value,
readOnly: readOnly,
"data-id": id
})
),
help && _react2.default.createElement(
"p",
{
"data-id": "error" + id,
className: isMandatory === true ? _InputTextModule2.default["input-holder__helper--red"] : _InputTextModule2.default["input-holder__helper"]
},
help
)
)
);
} else {
return _react2.default.createElement(
"div",
{ className: _InputTextModule2.default["input-holder"] },
label && _react2.default.createElement(
"p",
{ "data-id": "label" + id, className: _InputTextModule2.default["input-holder__header"] },
label
),
_react2.default.createElement(
"div",
{ style: { position: "relative" } },
icon && _react2.default.createElement(
"i",
{
className: icon_position === "right" ? _InputTextModule2.default["input-holder__icon--right"] : _InputTextModule2.default["input-holder__icon"]
},
_react2.default.createElement(
_Icon2.default,
{ style: { fontSize: 18 } },
icon
)
),
_react2.default.createElement("input", {
type: type,
onFocus: this.handleFocus,
onBlur: this.handleBlur,
onChange: onChange || this.onChange,
style: this.props.style
//style={isMandatory === true ? { border: "solid 2px #FA383E" } : {}}
, className: icon === undefined ? _InputTextModule2.default[text_stylemain] + " " + borderStyle : _InputTextModule2.default[text_style] + " " + borderStyle,
placeholder: placeHolder,
value: value === undefined || value === null ? this.state.value : value,
readOnly: readOnly,
"data-id": id
})
),
help && _react2.default.createElement(
"p",
{
"data-id": "error" + id,
className: isMandatory === true ? _InputTextModule2.default["input-holder__helper--red"] : _InputTextModule2.default["input-holder__helper"]
},
help
)
);
}
}
}]);
return InputText;
}(_react2.default.Component);
exports.default = InputText;
InputText.propTypes = {
placeHolder: _propTypes2.default.string,
label: _propTypes2.default.string,
help: _propTypes2.default.string,
icon: _propTypes2.default.string,
value: _propTypes2.default.string,
onKeyup: _propTypes2.default.func,
isMandatory: _propTypes2.default.bool
};
InputText.defaultProps = {
icon_position: "left",
placeHolder: "Enter text",
isMandatory: false,
type: "text",
onKeyup: function onKeyup() {
//console.log("keyup");
}
};