wix-style-react
Version:
160 lines (125 loc) • 5.93 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
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 PropTypes from 'prop-types';
import Text from '../Text';
import { st, classes } from './LabelledElement.st.css';
import { FontUpgradeContext } from '../FontUpgrade/context';
import DataHooks from './dataHooks';
import { generateID } from '../utils/generateId';
var LabelledElement = /*#__PURE__*/function (_React$Component) {
_inherits(LabelledElement, _React$Component);
var _super = _createSuper(LabelledElement);
function LabelledElement() {
var _this;
_classCallCheck(this, LabelledElement);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "state", {
hasFocus: false,
inputId: "labelled-element-".concat(generateID()),
value: undefined
});
_defineProperty(_assertThisInitialized(_this), "_isInputEmpty", function () {
return _this._isInputControlled() ? !_this.props.value : !_this.state.value;
});
_defineProperty(_assertThisInitialized(_this), "_isInputControlled", function () {
return typeof _this.props.value !== 'undefined';
});
_defineProperty(_assertThisInitialized(_this), "_handleFocus", function (event) {
var children = _this.props.children;
var childrenOnFocus = children && children.props.onFocus;
_this.setState({
hasFocus: true
});
childrenOnFocus && childrenOnFocus(event);
});
_defineProperty(_assertThisInitialized(_this), "_handleBlur", function (event) {
var children = _this.props.children;
var childrenOnBlur = children && children.props.onBlur;
_this.setState({
hasFocus: false
});
childrenOnBlur && childrenOnBlur(event);
});
_defineProperty(_assertThisInitialized(_this), "_handleOnChange", function (event) {
var children = _this.props.children;
var childrenOnChange = children && children.props.onChange;
if (!_this._isInputControlled()) {
_this.setState({
value: event.target.value
});
}
childrenOnChange && childrenOnChange(event);
});
_defineProperty(_assertThisInitialized(_this), "_placeLabelOnTop", function () {
return _this.state.hasFocus || !_this._isInputEmpty();
});
_defineProperty(_assertThisInitialized(_this), "_getPlaceholder", function (placeholder) {
return _this._placeLabelOnTop() ? placeholder : '';
});
return _this;
}
_createClass(LabelledElement, [{
key: "render",
value: function render() {
var _this2 = this;
var inputId = this.state.inputId;
var _this$props = this.props,
dataHook = _this$props.dataHook,
label = _this$props.label,
children = _this$props.children;
var labelTop = this._placeLabelOnTop();
return /*#__PURE__*/React.createElement(FontUpgradeContext.Consumer, null, function (_ref) {
var isMadefor = _ref.active;
return /*#__PURE__*/React.createElement("div", {
className: classes.root,
"data-hook": dataHook
}, /*#__PURE__*/React.createElement("label", {
"data-hook": DataHooks.label,
"data-top": labelTop,
htmlFor: inputId,
className: st(classes.label, {
labelTop: labelTop
})
}, /*#__PURE__*/React.createElement(Text, {
size: "medium",
light: !labelTop,
secondary: !labelTop,
weight: isMadefor ? 'thin' : 'normal',
className: classes.labelText
}, label)), children && /*#__PURE__*/React.createElement("div", {
"data-hook": DataHooks.childrenWrapper
}, /*#__PURE__*/React.cloneElement(children, {
id: inputId,
onFocus: _this2._handleFocus,
onBlur: _this2._handleBlur,
onChange: _this2._handleOnChange,
/** should have a different height than regular large input */
className: classes.inputContainer,
placeholder: children.props.placeholder ? _this2._getPlaceholder(children.props.placeholder) : undefined
})));
});
}
}]);
return LabelledElement;
}(React.Component);
_defineProperty(LabelledElement, "displayName", 'LabelledElement');
_defineProperty(LabelledElement, "propTypes", {
/** Specifies a data-hook for tests */
dataHook: PropTypes.string,
/** Sets the field label. */
label: PropTypes.string,
/** Specifies the value of rendered child input. */
value: PropTypes.string
});
export default LabelledElement;