@zohodesk/components
Version:
Dot UI is a customizable React component library built to deliver a clean, accessible, and developer-friendly UI experience. It offers a growing set of reusable components designed to align with modern design systems and streamline application development
233 lines (202 loc) • 11.2 kB
JavaScript
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _defaultProps = require("./props/defaultProps");
var _propTypes = require("./props/propTypes");
var _TextBoxModule = _interopRequireDefault(require("./TextBox.module.css"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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); Object.defineProperty(Constructor, "prototype", { writable: false }); 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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : 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; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } 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 { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
var TextBox = /*#__PURE__*/function (_React$PureComponent) {
_inherits(TextBox, _React$PureComponent);
var _super = _createSuper(TextBox);
function TextBox(props) {
var _this;
_classCallCheck(this, TextBox);
_this = _super.call(this, props);
_this.onChange = _this.onChange.bind(_assertThisInitialized(_this));
_this.inputRef = _this.inputRef.bind(_assertThisInitialized(_this));
_this.setFocus = _this.setFocus.bind(_assertThisInitialized(_this));
_this.handleFocus = _this.handleFocus.bind(_assertThisInitialized(_this));
_this.handleBlur = _this.handleBlur.bind(_assertThisInitialized(_this));
_this.handlePreventTextBoxScroll = _this.handlePreventTextBoxScroll.bind(_assertThisInitialized(_this));
return _this;
}
_createClass(TextBox, [{
key: "handleFocus",
value: function handleFocus(e) {
var _this$props = this.props,
id = _this$props.id,
value = _this$props.value,
name = _this$props.name,
onFocus = _this$props.onFocus;
onFocus && onFocus(id, value, name, e);
}
}, {
key: "handleBlur",
value: function handleBlur(e) {
var _this$props2 = this.props,
id = _this$props2.id,
value = _this$props2.value,
name = _this$props2.name,
onBlur = _this$props2.onBlur;
onBlur && onBlur(id, value, name, e);
}
}, {
key: "inputRef",
value: function inputRef(ref) {
var inputRef = this.props.inputRef;
this.inputEle = ref;
inputRef && inputRef(ref);
}
}, {
key: "setFocus",
value: function setFocus() {
if (this.inputEle) {
this.inputEle.focus({
preventScroll: true
});
}
}
}, {
key: "onChange",
value: function onChange(e) {
e.preventDefault();
var value = e.target.value;
var _this$props3 = this.props,
onChange = _this$props3.onChange,
isReadOnly = _this$props3.isReadOnly;
onChange && !isReadOnly && onChange(value, e);
}
}, {
key: "handlePreventTextBoxScroll",
value: function handlePreventTextBoxScroll() {
this.inputEle.scrollLeft = 0;
}
}, {
key: "render",
value: function render() {
var _this$props4 = this.props,
type = _this$props4.type,
name = _this$props4.name,
id = _this$props4.id,
maxLength = _this$props4.maxLength,
placeHolder = _this$props4.placeHolder,
size = _this$props4.size,
onKeyUp = _this$props4.onKeyUp,
isReadOnly = _this$props4.isReadOnly,
isDisabled = _this$props4.isDisabled,
onKeyDown = _this$props4.onKeyDown,
variant = _this$props4.variant,
onClick = _this$props4.onClick,
needBorder = _this$props4.needBorder,
value = _this$props4.value,
dataId = _this$props4.dataId,
autofocus = _this$props4.autofocus,
needReadOnlyStyle = _this$props4.needReadOnlyStyle,
needAppearance = _this$props4.needAppearance,
isClickable = _this$props4.isClickable,
onKeyPress = _this$props4.onKeyPress,
needEffect = _this$props4.needEffect,
autoComplete = _this$props4.autoComplete,
borderColor = _this$props4.borderColor,
onMouseDown = _this$props4.onMouseDown,
htmlId = _this$props4.htmlId,
a11y = _this$props4.a11y,
customClass = _this$props4.customClass,
isFocus = _this$props4.isFocus,
isScrollPrevent = _this$props4.isScrollPrevent,
customProps = _this$props4.customProps,
dataSelectorId = _this$props4.dataSelectorId;
var ariaLabel = a11y.ariaLabel,
ariaAutocomplete = a11y.ariaAutocomplete,
ariaControls = a11y.ariaControls,
ariaExpanded = a11y.ariaExpanded,
role = a11y.role,
ariaDescribedby = a11y.ariaDescribedby,
ariaHaspopup = a11y.ariaHaspopup,
ariaRequired = a11y.ariaRequired,
ariaLabelledby = a11y.ariaLabelledby,
ariaInvalid = a11y.ariaInvalid,
ariaOwns = a11y.ariaOwns,
ariaActivedescendant = a11y.ariaActivedescendant,
ariaReadonly = a11y.ariaReadonly,
ariaMultiselectable = a11y.ariaMultiselectable;
var options = {};
if (isReadOnly) {
options.readOnly = true;
}
if (isDisabled) {
options.disabled = true;
}
if (autofocus) {
options.autoFocus = true;
}
if (!autoComplete) {
options.autoComplete = 'off';
}
var classList = needAppearance ? "".concat(_TextBoxModule["default"].container, " ").concat(_TextBoxModule["default"][size], " ").concat(_TextBoxModule["default"][variant], " ").concat(needBorder ? _TextBoxModule["default"].border : '', " ").concat(isDisabled && !needEffect || isReadOnly && !needEffect ? '' : _TextBoxModule["default"].effect, " ").concat(isFocus ? _TextBoxModule["default"].focus : '') : "".concat(_TextBoxModule["default"].basic);
value = value === null || value === undefined ? '' : value;
return /*#__PURE__*/_react["default"].createElement("input", _extends({
"aria-label": ariaLabel,
"aria-invalid": ariaInvalid,
"aria-autocomplete": ariaAutocomplete,
"aria-controls": ariaControls,
"aria-expanded": ariaExpanded,
"aria-describedby": ariaDescribedby,
role: role,
"aria-haspopup": ariaHaspopup,
"aria-required": ariaRequired,
"aria-labelledby": ariaLabelledby,
"aria-owns": ariaOwns,
"aria-activedescendant": ariaActivedescendant,
"aria-readonly": ariaReadonly,
"aria-multiselectable": ariaMultiselectable,
className: "".concat(isReadOnly && needReadOnlyStyle ? _TextBoxModule["default"].readonly : '', " ").concat(isClickable ? _TextBoxModule["default"].pointer : '', " ").concat(classList, " ").concat(_TextBoxModule["default"]["borderColor_".concat(borderColor)], " ").concat(isScrollPrevent ? _TextBoxModule["default"].inputDotted : '', " ").concat(customClass ? customClass : ''),
"data-id": "".concat(dataId),
"data-test-id": "".concat(dataId),
"data-selector-id": dataSelectorId,
id: htmlId || id,
maxLength: maxLength,
name: name,
onBlur: this.handleBlur,
onChange: this.onChange,
onClick: onClick,
onFocus: this.handleFocus,
onKeyDown: onKeyDown,
onKeyUp: onKeyUp,
placeholder: placeHolder,
ref: this.inputRef,
type: type,
value: value,
onScroll: isScrollPrevent ? this.handlePreventTextBoxScroll : null,
onKeyPress: onKeyPress,
onMouseDown: onMouseDown
}, options, customProps));
}
}]);
return TextBox;
}(_react["default"].PureComponent);
exports["default"] = TextBox;
TextBox.defaultProps = _defaultProps.defaultProps;
TextBox.propTypes = _propTypes.propTypes; // if (__DOCS__) {
// Textbox.docs = {
// componentGroup: 'Form Elements',
// folderName: 'Style Guide',
// external: true,
// description: ' '
// };
// }