wix-style-react
Version:
172 lines (141 loc) • 6.94 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
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 _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; } }
import React from 'react';
import StatusAlertSmall from 'wix-ui-icons-common/StatusAlertSmall';
import LabelledElement from '../LabelledElement';
import Input from '../Input';
import Text from '../Text';
import PropTypes from 'prop-types';
import { classes } from './InputWithLabel.st.css';
import dataHooks from './dataHooks';
var getSuffixContainer = function getSuffixContainer(suffix) {
return suffix.map(function (item, index) {
return /*#__PURE__*/React.createElement("div", {
"data-hook": "suffix-container",
key: "suffix-container-".concat(index),
className: classes.groupIcon
}, item);
});
};
var InputWithLabel = /*#__PURE__*/function (_React$Component) {
_inherits(InputWithLabel, _React$Component);
var _super = _createSuper(InputWithLabel);
function InputWithLabel() {
_classCallCheck(this, InputWithLabel);
return _super.apply(this, arguments);
}
_createClass(InputWithLabel, [{
key: "render",
value: function render() {
var _this$props = this.props,
label = _this$props.label,
suffix = _this$props.suffix,
value = _this$props.value,
dataHook = _this$props.dataHook,
status = _this$props.status,
statusMessage = _this$props.statusMessage,
onChange = _this$props.onChange,
onFocus = _this$props.onFocus,
onBlur = _this$props.onBlur,
name = _this$props.name,
type = _this$props.type,
ariaLabel = _this$props.ariaLabel,
autoFocus = _this$props.autoFocus,
autocomplete = _this$props.autocomplete,
disabled = _this$props.disabled,
className = _this$props.className,
maxLength = _this$props.maxLength,
placeholder = _this$props.placeholder,
customInput = _this$props.customInput;
return /*#__PURE__*/React.createElement("div", {
"data-hook": dataHook,
className: status ? undefined : classes.statusMessagePlaceholder
}, /*#__PURE__*/React.createElement(LabelledElement, {
value: value,
label: label,
dataHook: dataHooks.labelledElement
}, /*#__PURE__*/React.createElement(Input, {
name: name,
type: type,
ariaLabel: ariaLabel,
autoFocus: autoFocus,
autocomplete: autocomplete,
disabled: disabled,
maxLength: maxLength,
placeholder: placeholder,
onChange: onChange,
onFocus: onFocus,
onBlur: onBlur,
dataHook: dataHooks.input,
className: className,
size: "large",
value: value,
suffix: suffix ? getSuffixContainer(suffix) : [],
status: status,
customInput: customInput,
hideStatusSuffix: true
})), status === Input.StatusError && statusMessage && /*#__PURE__*/React.createElement(Text, {
skin: "error",
size: "small",
weight: "normal",
className: classes.statusMessage
}, /*#__PURE__*/React.createElement("span", {
className: classes.statusMessageIcon
}, /*#__PURE__*/React.createElement(StatusAlertSmall, null)), /*#__PURE__*/React.createElement("span", {
"data-hook": dataHooks.errorMessage,
className: classes.errorMessageContent
}, statusMessage)));
}
}]);
return InputWithLabel;
}(React.Component);
_defineProperty(InputWithLabel, "propTypes", {
/** Sets a default value for those who want to use this component uncontrolled */
dataHook: PropTypes.string,
/** Pass a component you want to show as the suffix of the input, e.g., text string, icon. */
suffix: PropTypes.arrayOf(PropTypes.element),
/** Sets the field label */
label: PropTypes.string,
/** Sets input value */
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/** Specifies the status of a field */
status: PropTypes.oneOf(['error', 'warning', 'loading']),
/** Defines the message displayed when you hover over the status icon. If not given or empty there will be no tooltip. */
statusMessage: PropTypes.node,
/** Defines a standard input onFocus callback */
onFocus: PropTypes.func,
/** Defines a standard input onBlur callback */
onBlur: PropTypes.func,
/** Defines a standard input onChange callback */
onChange: PropTypes.func,
/** Reference element data when a form is submitted */
name: PropTypes.string,
/** Specifies the type of `<input/>` element to display. Default is text string. */
type: PropTypes.string,
/** Define a string that labels the current element in case where a text label is not visible on the screen */
ariaLabel: PropTypes.string,
/** Focus the element on mount (standard React input autoFocus). */
autoFocus: PropTypes.bool,
/** Sets the value of native autocomplete attribute (consult the [HTML spec](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-autocomplete) for possible values) */
autocomplete: PropTypes.string,
/** Specifies whether the input should be disabled or not */
disabled: PropTypes.bool,
/** Specifies a CSS class name to be appended to the component’s root element */
className: PropTypes.string,
/** Sets the maximum number of characters that can be entered into a field */
maxLength: PropTypes.number,
/** Sets a placeholder message to display */
placeholder: PropTypes.string,
/** Render a custom input component instead of the default html input tag */
customInput: PropTypes.elementType ? PropTypes.oneOfType([PropTypes.func, PropTypes.node, PropTypes.elementType]) : PropTypes.oneOfType([PropTypes.func, PropTypes.node])
});
_defineProperty(InputWithLabel, "defaultProps", {
statusMessage: ''
});
export default InputWithLabel;