@zohodesk/dot
Version:
In this Library, we Provide Some Basic Components to Build Your Application
253 lines (215 loc) • 14.8 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 = _interopRequireWildcard(require("react"));
var _defaultProps = require("./props/defaultProps");
var _propTypes = require("./props/propTypes");
var _Icon = _interopRequireDefault(require("@zohodesk/icons/es/Icon"));
var _Label = _interopRequireDefault(require("@zohodesk/components/lib/Label/Label"));
var _Radio = _interopRequireDefault(require("@zohodesk/components/lib/Radio/Radio"));
var _ValidationMessage = _interopRequireDefault(require("../ValidationMessage/ValidationMessage"));
var _FieldsModule = _interopRequireDefault(require("../Fields.module.css"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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 RadioField = /*#__PURE__*/function (_PureComponent) {
_inherits(RadioField, _PureComponent);
var _super = _createSuper(RadioField);
function RadioField(props) {
var _this;
_classCallCheck(this, RadioField);
_this = _super.call(this, props);
_this.data = {
radios: {},
focus: _this.handleFocus
};
_this.handleChange = _this.handleChange.bind(_assertThisInitialized(_this));
_this.updateData = _this.updateData.bind(_assertThisInitialized(_this));
_this.handleFocus = _this.handleFocus.bind(_assertThisInitialized(_this));
return _this;
}
_createClass(RadioField, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this$props = this.props,
getRef = _this$props.getRef,
id = _this$props.id;
getRef && getRef(this.data, id);
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this$props2 = this.props,
getRef = _this$props2.getRef,
id = _this$props2.id;
if (getRef !== prevProps.getRef) {
getRef && getRef(this.data, id);
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
var _this$props3 = this.props,
getRef = _this$props3.getRef,
id = _this$props3.id;
getRef && getRef(null, id);
}
}, {
key: "handleChange",
value: function handleChange(value) {
var _this$props4 = this.props,
id = _this$props4.id,
onChange = _this$props4.onChange;
onChange && onChange(id, value);
}
}, {
key: "updateData",
value: function updateData(ele, val) {
this.data.radios[val] = ele;
}
}, {
key: "handleFocus",
value: function handleFocus() {
var _this$props5 = this.props,
selectedValue = _this$props5.selectedValue,
options = _this$props5.options;
var firstRadioValue = options[0].value;
if (selectedValue) {
this.data.radios[selectedValue].focus();
} else {
this.data.radios[firstRadioValue].focus();
}
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props6 = this.props,
labelName = _this$props6.labelName,
id = _this$props6.id,
isMandatory = _this$props6.isMandatory,
options = _this$props6.options,
validationMessage = _this$props6.validationMessage,
validationPalette = _this$props6.validationPalette,
errorType = _this$props6.errorType,
isDisabled = _this$props6.isDisabled,
title = _this$props6.title,
labelPalette = _this$props6.labelPalette,
labelSize = _this$props6.labelSize,
size = _this$props6.size,
selectedValue = _this$props6.selectedValue,
isActive = _this$props6.isActive,
dataId = _this$props6.dataId,
dataSelectorId = _this$props6.dataSelectorId,
validationRuleMessage = _this$props6.validationRuleMessage,
validationRulePalette = _this$props6.validationRulePalette,
isReadOnly = _this$props6.isReadOnly,
isBoxStyle = _this$props6.isBoxStyle,
variant = _this$props6.variant,
customProps = _this$props6.customProps;
var _customProps$LabelPro = customProps.LabelProps,
LabelProps = _customProps$LabelPro === void 0 ? {} : _customProps$LabelPro,
_customProps$RadioPro = customProps.RadioProps,
RadioProps = _customProps$RadioPro === void 0 ? {} : _customProps$RadioPro,
_customProps$InfoIcon = customProps.InfoIconProps,
InfoIconProps = _customProps$InfoIcon === void 0 ? {} : _customProps$InfoIcon,
_customProps$Validati = customProps.ValidationMessageProps1,
ValidationMessageProps1 = _customProps$Validati === void 0 ? {} : _customProps$Validati,
_customProps$Validati2 = customProps.ValidationMessageProps2,
ValidationMessageProps2 = _customProps$Validati2 === void 0 ? {} : _customProps$Validati2;
return /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(_FieldsModule["default"].container, " ").concat(isDisabled ? _FieldsModule["default"].disabled : isReadOnly ? _FieldsModule["default"].readonly : ''),
"data-title": isDisabled ? title : null,
"data-selector-id": dataSelectorId
}, labelName && /*#__PURE__*/_react["default"].createElement(_Label["default"], _extends({
text: labelName,
size: "medium",
id: id,
palette: isMandatory ? 'mandatory' : isDisabled ? 'primary' : 'default',
customClass: "".concat(_FieldsModule["default"].fieldLabel, " ").concat(isMandatory ? _FieldsModule["default"].labelMandatory : ''),
dataId: isDisabled ? "".concat(dataId, "_label_disabled") : isMandatory ? "".concat(dataId, "_label_mandatory") : "".concat(dataId, "_label")
}, LabelProps)), /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(_FieldsModule["default"].fieldContainer, " ").concat(isBoxStyle ? _FieldsModule["default"].radiosWrapper : '', " ").concat(labelName ? _FieldsModule["default"].fieldMargin_medium : '', " ").concat(_FieldsModule["default"].radioContainer)
}, options.map(function (option, index) {
var text = option.text,
value = option.value,
_option$disabled = option.disabled,
disabled = _option$disabled === void 0 ? false : _option$disabled,
tooltip = option.tooltip,
infoTooltip = option.infoTooltip;
var isDisabledState = disabled || isDisabled;
var isChecked = selectedValue == value;
return /*#__PURE__*/_react["default"].createElement("span", {
key: index,
className: "".concat(!isBoxStyle ? _FieldsModule["default"].radio : '', " ").concat(_FieldsModule["default"].radioWrap)
}, /*#__PURE__*/_react["default"].createElement(_Radio["default"], _extends({
id: index,
value: value,
name: id,
text: text,
labelPalette: labelPalette,
labelSize: labelSize,
active: isActive || isBoxStyle && isChecked,
disabled: isDisabledState,
disabledTitle: tooltip,
title: tooltip,
onChange: _this2.handleChange,
getRef: _this2.updateData,
size: size,
checked: isChecked,
dataId: dataId,
isReadOnly: isReadOnly,
variant: variant
}, RadioProps, {
a11y: _objectSpread({
tabIndex: !!selectedValue ? isChecked ? '0' : '-1' : index === 0 ? '0' : '-1'
}, RadioProps.a11y),
customClass: _objectSpread({
customRadioWrap: isBoxStyle ? "".concat(_FieldsModule["default"].radioBox, " ").concat(!isDisabledState ? _FieldsModule["default"].hoverableRadioBox : '', " ").concat(isChecked ? _FieldsModule["default"].radioBoxActive : '') : ''
}, RadioProps.customClass)
}), !!infoTooltip ? /*#__PURE__*/_react["default"].createElement(_Icon["default"], _extends({
name: "ZD-GN-info",
size: "16",
title: infoTooltip,
iconClass: _FieldsModule["default"].infoIcon
}, InfoIconProps)) : null));
})), validationMessage && /*#__PURE__*/_react["default"].createElement(_ValidationMessage["default"], _extends({
text: validationMessage,
palette: validationPalette,
type: errorType,
dataId: "".concat(dataId, "_ValidationMessage")
}, ValidationMessageProps1)), validationRuleMessage && /*#__PURE__*/_react["default"].createElement(_ValidationMessage["default"], _extends({
text: validationRuleMessage,
palette: validationRulePalette,
type: errorType,
dataId: "".concat(dataId, "_ValidationRuleMessage")
}, ValidationMessageProps2)));
}
}]);
return RadioField;
}(_react.PureComponent);
exports["default"] = RadioField;
RadioField.propTypes = _propTypes.propTypes;
RadioField.defaultProps = _defaultProps.defaultProps; // if (__DOCS__) {
// RadioField.docs = {
// componentGroup: 'Form Fields',
// folderName: 'General'
// };
// }