UNPKG

@vtex/styleguide

Version:

> VTEX Styleguide React components ([Docs](https://vtex.github.io/styleguide))

369 lines (327 loc) 12.8 kB
'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 _config = require('vtex-tachyons/config.json'); var _config2 = _interopRequireDefault(_config); 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 Input = function (_Component) { _inherits(Input, _Component); function Input(props) { _classCallCheck(this, Input); var _this = _possibleConstructorReturn(this, (Input.__proto__ || Object.getPrototypeOf(Input)).call(this, props)); _this.handleChange = function (event) { _this.props.onChange && _this.props.onChange(event); }; _this.handleKeyPress = function (event) { _this.props.onKeyPress && _this.props.onKeyPress(event); }; _this.handleKeyDown = function (event) { _this.props.onKeyDown && _this.props.onKeyDown(event); }; _this.handleKeyUp = function (event) { _this.props.onKeyUp && _this.props.onKeyUp(event); }; _this.handleFocus = function (event) { _this.setState({ active: true }); _this.props.onFocus && _this.props.onFocus(event); }; _this.handleBlur = function (event) { _this.setState({ active: false }); _this.props.onBlur && _this.props.onBlur(event); }; _this.state = { active: false }; return _this; } _createClass(Input, [{ key: 'render', value: function render() { var _props = this.props, errorMessage = _props.errorMessage, error = _props.error, label = _props.label, size = _props.size, token = _props.token, helpText = _props.helpText, dataAttributes = _props.dataAttributes, prefix = _props.prefix, suffixIcon = _props.suffixIcon, groupBottom = _props.groupBottom; var active = this.state.active; var inputAriaInvalid = error || errorMessage ? 'true' : undefined; var dataAttrs = {}; var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = Object.keys(dataAttributes)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var key = _step.value; dataAttrs['data-' + key] = dataAttributes[key]; } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } var widthClass = 'w-100'; var box = 'ma0 border-box'; var borderRadius = 'br2 ' + (groupBottom ? 'br--top' : ''); var border = 'bw1 ' + borderRadius + ' b--solid outline-0'; // On bw1 change, update config.borderRadius[1] below accordingly var topBottomHeight = _config2.default.borderRadius[1] * 2; // 2 is top AND BOTTOM var prefixAndSuffixPosition = _config2.default.borderRadius[1] + 'rem'; var calcPrefixAndSuffixHeight = 'calc(100% - ' + topBottomHeight + 'rem)'; var typography = 'near-black'; var classes = widthClass + ' ' + box + ' ' + border + ' ' + typography + ' '; var labelClasses = 'vtex-input__label db mb3 w-100 '; var prefixAndSuffixClasses = 'vtex-input__prefix absolute gray fw5 flex items-center '; if (token) { classes += 'code '; } if (active) { classes += 'b--gray '; } else { classes += 'b--light-gray '; if (!this.props.disabled) { classes += 'hover-b--silver '; } } if (error || errorMessage) { classes += 'b--red hover-b--red '; } if (this.props.disabled) { classes += 'bg-light-gray bg-light-silver b--light-silver silver '; } else { classes += 'bg-white '; } switch (size) { case 'large': classes += 'f5 pv4 ' + (prefix ? 'pl8 pr6' : 'ph6') + ' ' + (suffixIcon ? 'pr8' : ''); labelClasses += 'f5 '; prefixAndSuffixClasses += 'ph4 f5'; // iconSize = 18 break; case 'x-large': classes += 'f4 pv5 ' + (prefix ? 'pl8 pr7' : 'ph7') + ' ' + (suffixIcon ? 'pr8' : ''); labelClasses += 'f5 '; prefixAndSuffixClasses += 'ph5 f4 '; // iconSize = 22 break; default: classes += 'f6 pv3 ' + (prefix ? 'pl7 pr5' : 'ph5') + ' ' + (suffixIcon ? 'pr7' : ''); labelClasses += 'f6 '; prefixAndSuffixClasses += 'ph3 fw5 f6 '; // iconSize = 16 break; } return _react2.default.createElement( 'label', { className: 'vtex-input w-100' }, label && _react2.default.createElement( 'span', { className: labelClasses }, label ), _react2.default.createElement( 'div', { className: 'flex vtex-input-prefix__group relative' }, prefix && _react2.default.createElement( 'span', { style: { height: calcPrefixAndSuffixHeight, top: prefixAndSuffixPosition, left: prefixAndSuffixPosition }, className: prefixAndSuffixClasses }, prefix ), _react2.default.createElement('input', Object.assign({}, dataAttrs, { ref: this.props.forwardedRef, onBlur: this.handleBlur, onFocus: this.handleFocus, onChange: this.handleChange, onKeyPress: this.handleKeyPress, onKeyDown: this.handleKeyDown, onKeyUp: this.handleKeyUp, className: classes, disabled: this.props.disabled, accept: this.props.accept, autoComplete: this.props.autoComplete, autoCorrect: this.props.autoCorrect, autoFocus: this.props.autoFocus, autoSave: this.props.autoSave, 'aria-invalid': inputAriaInvalid, defaultValue: this.props.defaultValue, inputMode: this.props.inputMode, list: this.props.list, max: this.props.max, maxLength: this.props.maxLength, min: this.props.min, minLength: this.props.minLength, multiple: this.props.multiple, name: this.props.name, pattern: this.props.pattern, placeholder: this.props.placeholder, readOnly: this.props.readOnly, required: this.props.required, spellCheck: this.props.spellCheck, src: this.props.src, step: this.props.step, tabIndex: this.props.tabIndex, type: this.props.type, value: this.props.value, id: this.props.id })), suffixIcon && _react2.default.createElement( 'span', { style: { height: calcPrefixAndSuffixHeight, top: prefixAndSuffixPosition, right: prefixAndSuffixPosition }, className: prefixAndSuffixClasses }, suffixIcon ) ), errorMessage && _react2.default.createElement( 'div', { className: 'red f6 mt3 lh-title' }, errorMessage ), helpText && _react2.default.createElement( 'div', { className: 'mid-gray f6 mt3 lh-title' }, helpText ) ); } }]); return Input; }(_react.Component); Input.defaultProps = { autoFocus: false, token: false, dataAttributes: {}, disabled: false, label: '', multiple: false, readOnly: false, error: false, size: 'regular', prefix: '', type: 'text' }; Input.propTypes = { /** Error highlight */ error: _propTypes2.default.bool, /** Error message */ errorMessage: _propTypes2.default.string, /** If the input is an API Key, App Key or App Token */ token: _propTypes2.default.bool, /** Help text */ helpText: _propTypes2.default.node, /** Input size */ size: _propTypes2.default.oneOf(['regular', 'large', 'x-large']), /** Label */ label: _propTypes2.default.string, /** Prefix */ prefix: _propTypes2.default.string, /** Internal prop used for ref forwarding */ forwardedRef: _propTypes2.default.func, /** Spec attribute */ accept: _propTypes2.default.string, /** Spec attribute */ disabled: _propTypes2.default.bool, /** Spec attribute */ autoComplete: _propTypes2.default.string, /** Spec attribute */ autoCorrect: _propTypes2.default.string, /** Spec attribute */ autoFocus: _propTypes2.default.bool, /** Spec attribute */ autoSave: _propTypes2.default.string, /** List of data attributes as a object like `{'locale': 'en-US'}` */ dataAttributes: _propTypes2.default.object, /** Spec attribute */ defaultValue: _propTypes2.default.string, /** Whether the border should join with an element below */ groupBottom: _propTypes2.default.bool, /** Spec attribute */ id: _propTypes2.default.string, /** Spec attribute */ inputMode: _propTypes2.default.string, /** Spec attribute */ list: _propTypes2.default.string, /** Spec attribute */ max: _propTypes2.default.string, /** Spec attribute */ maxLength: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]), /** Spec attribute */ min: _propTypes2.default.string, /** Spec attribute */ minLength: _propTypes2.default.string, /** Spec attribute */ multiple: _propTypes2.default.bool, /** Spec attribute */ name: _propTypes2.default.string, /** Spec attribute */ pattern: _propTypes2.default.string, /** Spec attribute */ placeholder: _propTypes2.default.string, /** Spec attribute */ readOnly: _propTypes2.default.bool, /** Spec attribute */ required: _propTypes2.default.bool, /** Spec attribute */ spellCheck: _propTypes2.default.string, /** Spec attribute */ src: _propTypes2.default.string, /** Spec attribute */ step: _propTypes2.default.string, /** SuffixIcon attribute */ suffixIcon: _propTypes2.default.element, /** Spec attribute */ tabIndex: _propTypes2.default.string, /** Spec attribute */ type: _propTypes2.default.string, /** Spec attribute */ value: _propTypes2.default.string, /** onChange event */ onChange: _propTypes2.default.func, /** onKeyDown event */ onKeyDown: _propTypes2.default.func, /** onKeyPress event */ onKeyPress: _propTypes2.default.func, /** onKeyUp event */ onKeyUp: _propTypes2.default.func, /** onFocus event */ onFocus: _propTypes2.default.func, /** onBlur event */ onBlur: _propTypes2.default.func }; exports.default = _react2.default.forwardRef(function (props, ref) { return _react2.default.createElement(Input, Object.assign({}, props, { forwardedRef: ref })); });