UNPKG

polestar-ui-kit

Version:

## Install

252 lines (199 loc) 10 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } require("core-js/modules/es.date.to-string"); require("core-js/modules/es.function.name"); require("core-js/modules/es.object.get-prototype-of"); require("core-js/modules/es.object.set-prototype-of"); require("core-js/modules/es.object.to-string"); require("core-js/modules/es.reflect.construct"); require("core-js/modules/es.regexp.constructor"); require("core-js/modules/es.regexp.exec"); require("core-js/modules/es.regexp.to-string"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _classnames = _interopRequireDefault(require("classnames")); 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 _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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var Login = function (_React$Component) { _inherits(Login, _React$Component); var _super = _createSuper(Login); function Login(props) { var _this; _classCallCheck(this, Login); _this = _super.call(this, props); _this.isUsernameValid = function () { var username = _this.state.username || ''; return username !== '' && new RegExp(_this.props.usernamePattern).test(username); }; _this.isPasswordValid = function () { var password = _this.state.password || ''; return password !== '' && new RegExp(_this.props.passwordPattern).test(password); }; _this.handleChange = function (e) { switch (e.target.name) { case _this.props.useridName: _this.props.onChange({ fieldName: _this.props.useridName, fieldValue: e.target.value, isValid: _this.isUsernameValid() }); return _this.setState({ username: e.target.value }); case _this.props.passwordName: _this.props.onChange({ fieldName: _this.props.passwordName, fieldValue: e.target.value, isValid: _this.isPasswordValid() }); return _this.setState({ password: e.target.value }); default: return null; } }; _this.handleKeyPress = function (e) { if (e.charCode === 13) { _this.handleSubmit(); } }; _this.handleSubmit = function () { var canSubmit = true; if (!_this.isUsernameValid()) { console.error("".concat(_this.props.useridName, " \uD544\uB4DC\uAC12\uC774 \uC785\uB825\uB418\uC9C0 \uC54A\uC558\uAC70\uB098 usernamePattern props\uC5D0 \uC815\uC758\uB41C \uD328\uD134\uACFC \uB2E4\uB985\uB2C8\uB2E4.")); canSubmit = false; _this[_this.props.useridName].focus(); return; } if (!_this.isPasswordValid()) { console.error("".concat(_this.props.passwordName, " \uD544\uB4DC\uAC12\uC774 \uC785\uB825\uB418\uC9C0 \uC54A\uC558\uAC70\uB098 usernamePattern props\uC5D0 \uC815\uC758\uB41C \uD328\uD134\uACFC \uB2E4\uB985\uB2C8\uB2E4.")); canSubmit = false; _this[_this.props.passwordName].focus(); } if (_this.props.onSubmit(_this.state) === false) { canSubmit = false; } if (canSubmit) { _this.clearFields(); _this.props.loginProcess(_this.state); } }; _this.clearFields = function () { _this.setState({ username: '', password: '' }); }; _this.state = { username: '', password: '' }; return _this; } _createClass(Login, [{ key: "render", value: function render() { var _this2 = this; var _this$props = this.props, className = _this$props.className, width = _this$props.width; return _react["default"].createElement("div", { className: (0, _classnames["default"])('polestar login container', className), style: { width: width } }, _react["default"].createElement("div", { className: "polestar login content" }, _react["default"].createElement("label", { htmlFor: this.props.useridName, className: "polestar login label" }, this.props.useridLableText, _react["default"].createElement("input", { type: "text", className: "polestar login input username", name: this.props.useridName, value: this.state.username, placeholder: this.props.usernamePlaceholder, onChange: this.handleChange, onKeyPress: this.handleKeyPress, ref: function ref(_ref) { _this2[_this2.props.useridName] = _ref; } })), _react["default"].createElement("label", { htmlFor: this.props.passwordName, className: "polestar login label" }, this.props.passwordLableText, _react["default"].createElement("input", { type: "password", className: "polestar login input password", name: this.props.passwordName, value: this.state.password, placeholder: this.props.passwordPlaceholder, onChange: this.handleChange, onKeyPress: this.handleKeyPress, ref: function ref(_ref2) { _this2[_this2.props.passwordName] = _ref2; } }))), _react["default"].createElement("div", { className: "polestar login footer" }, _react["default"].cloneElement(this.props.loginButton, { className: 'polestar login button', onClick: this.handleSubmit }))); } }]); return Login; }(_react["default"].Component); Login.propTypes = { width: _propTypes["default"].number, useridName: _propTypes["default"].string, useridLableText: _propTypes["default"].string, usernamePlaceholder: _propTypes["default"].string, usernamePattern: _propTypes["default"].string, passwordName: _propTypes["default"].string, passwordLableText: _propTypes["default"].string, passwordPlaceholder: _propTypes["default"].string, passwordPattern: _propTypes["default"].string, loginButton: _propTypes["default"].element, className: _propTypes["default"].string, onChange: _propTypes["default"].func, onSubmit: _propTypes["default"].func, loginProcess: _propTypes["default"].func }; Login.defaultProps = { width: 300, useridName: 'userId', useridLableText: 'UserId', usernamePlaceholder: 'Username', usernamePattern: 'a-zA-Z0-9', passwordName: 'password', passwordLableText: 'Password', passwordPlaceholder: 'Password', passwordPattern: 'a-zA-Z0-9', loginButton: _react["default"].createElement("button", { type: "button" }, "Login"), onChange: function onChange(field) { console.log('onChange ->', field); }, onSubmit: function onSubmit(fields) { console.log('onSubmit ->', fields); }, loginProcess: function loginProcess(fields) { console.info('디폴트 loginProcess 함수가 호출되었습니다. ' + '실제 로그인 과정을 처리할 loginProcess 함수를 정의해서 Login 컴포넌트의 props로 전달해주세요.', fields); } }; var _default = Login; exports["default"] = _default;