@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
268 lines (222 loc) • 13.4 kB
JavaScript
;
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 _propTypes = _interopRequireDefault(require("prop-types"));
var _ResizeObserver = _interopRequireDefault(require("./ResizeObserver"));
var _Common = require("../utils/Common");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
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 ResizeComponent = /*#__PURE__*/function (_React$Component) {
_inherits(ResizeComponent, _React$Component);
var _super = _createSuper(ResizeComponent);
function ResizeComponent(props) {
var _this;
_classCallCheck(this, ResizeComponent);
_this = _super.call(this, props);
_this.noSpaceForChildren = false;
_this.childrenCurrentList = [];
_this.widthCheck = (0, _Common.debounce)(_this.widthCheck.bind(_assertThisInitialized(_this)), 10); // this.widthCheck = this.widthCheck.bind(this);
_this.onResize = _this.onResize.bind(_assertThisInitialized(_this));
_this.onResizeMutation = _this.onResizeMutation.bind(_assertThisInitialized(_this));
_this.tabsObserver = new _ResizeObserver["default"](_this.onResize);
_this.tabObserver = new MutationObserver(_this.onResizeMutation);
_this.reset = _this.reset.bind(_assertThisInitialized(_this));
_this.constructChildren = _this.constructChildren.bind(_assertThisInitialized(_this));
_this.querySelector = _this.querySelector.bind(_assertThisInitialized(_this));
return _this;
}
_createClass(ResizeComponent, [{
key: "querySelector",
value: function querySelector() {
if (this.props.wrapperDivRef && this.props.wrapperDivRef.current) {
return this.props.wrapperDivRef.current.querySelectorAll('[data-responsive="true"]');
} else {
return [];
}
}
}, {
key: "onResizeMutation",
value: function onResizeMutation(mutations) {
var _this2 = this;
// console.log(mutations);
var childrenList = this.props.childrenList;
var _iterator = _createForOfIteratorHelper(mutations),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var mutation = _step.value;
if (mutation.type === 'childList') {
if (mutation.addedNodes.length) {
var newAdded = false;
mutation.addedNodes.forEach(function (mut) {
// Here we can listen for icon nodes. Confirm with bk
if (mut.attributes && mut.attributes['data-responsive'] && (childrenList.length !== _this2.querySelector().length || _this2.childrenCurrentList.length !== _this2.querySelector().length)) {
newAdded = true;
}
});
if (newAdded) {
//console.log('A child node has been added ', mutation);
this.onResize();
}
} else if (mutation.removedNodes.length) {// ?? removed nodes ku check pannanum ah ??
// console.log('A child node has been removed ', mutation);
}
} else if (mutation.type === 'attributes') {//console.log('The ' + mutation.attributeName + ' attribute was modified.', mutation);
} else if (mutation.type === 'characterData') {// To Call While Changing InnerText, Remove Below line If Required. But It might affect Performance.
// this.onResize();
}
} // mutations.forEach(mutation => {
// if (mutation.target === foo &&
// mutation.attributeName === 'style' &&
// oldWidth !== foo.style.width) {
// foo.textContent = 'Width changed from ' +
// oldWidth + ' to ' + foo.style.width;
// oldWidth = foo.style.width;
// }
// });
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
if (this.props.wrapperDivRef && this.props.wrapperDivRef.current) {
this.tabsObserver.observe(this.props.wrapperDivRef.current);
this.tabObserver.observe(this.props.wrapperDivRef.current, {
characterData: true,
attributes: true,
childList: true,
subtree: true,
attributeFilter: ['data-responsive']
});
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.reset();
}
}, {
key: "reset",
value: function reset() {
this.childrenCurrentList = [];
this.tabsObserver.disconnect();
this.tabObserver && this.tabObserver.disconnect();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
if (this.childrenCurrentList.length === 0) {
this.childrenCurrentList = this.querySelector();
this.widthCheck();
}
if (this.props.resizeId !== prevProps.resizeId) {
// Completely reset the observers and set new observer if id is changed
this.reset();
if (this.props.wrapperDivRef && this.props.wrapperDivRef.current) {
this.tabsObserver.observe(this.props.wrapperDivRef.current);
this.tabObserver.observe(this.props.wrapperDivRef.current, {
characterData: true,
attributes: true,
childList: true,
subtree: true,
attributeFilter: ['data-responsive']
});
}
}
}
}, {
key: "onResize",
value: function onResize() {
// if (this.childrenCurrentList.length !== 0) {
this.childrenCurrentList = [];
this.constructChildren(0, false); //}
} // shouldComponentUpdate() {
// return this.childrenCurrentList.length === 0
// }
}, {
key: "widthCheck",
value: function widthCheck() {
var wrapperDivRef = this.props.wrapperDivRef.current;
var moreDivRef = this.props.moreDivRef.current;
if (wrapperDivRef && this.childrenCurrentList.length > 0) {
var childrenWidthList = this.childrenCurrentList; // this.childrenCurrentList = childrenWidthList;
var moreWidth = (0, _Common.getElementSpace)(moreDivRef).neededSpace; //console.log(moreWidth,'moreWidth');
var totalWidth = (0, _Common.getElementSpace)(wrapperDivRef).availableInsideSpace - moreWidth;
var _childrenTotalWidth = 0;
var dataCount = 0;
this.noSpaceForChildren = false;
if (totalWidth <= moreWidth) {
this.noSpaceForChildren = true;
} else {
for (var i = 0; i < childrenWidthList.length; i++) {
var currentWidth = (0, _Common.getElementSpace)(childrenWidthList[i]).neededSpace;
_childrenTotalWidth += currentWidth; // console.log(_childrenTotalWidth, currentWidth, totalWidth)
if (i === childrenWidthList.length - 1 && moreWidth >= currentWidth) {
totalWidth += moreWidth;
}
if (totalWidth <= _childrenTotalWidth) {
dataCount = i;
break;
}
}
}
this.constructChildren(dataCount, true);
} else {
this.constructChildren(0, false);
}
}
}, {
key: "constructChildren",
value: function constructChildren() {
var dataCount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var responsive = arguments.length > 1 ? arguments[1] : undefined;
var _this$props = this.props,
childrenList = _this$props.childrenList,
getData = _this$props.getData; // If the data count is zero we assume that we can render all the items - data count will be zero only when we want to calculate the whole list width
dataCount = dataCount || childrenList && childrenList.length;
getData({
responsiveHook: responsive,
validListCount: this.noSpaceForChildren ? 0 : dataCount
});
}
}, {
key: "render",
value: function render() {
return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, this.props.children);
}
}]);
return ResizeComponent;
}(_react["default"].Component);
exports["default"] = ResizeComponent;
ResizeComponent.propTypes = {
children: _propTypes["default"].node,
getData: _propTypes["default"].func,
resizeId: _propTypes["default"].number,
childrenList: _propTypes["default"].array,
wrapperDivRef: _propTypes["default"].any,
moreDivRef: _propTypes["default"].any
};
ResizeComponent.defaultProps = {
getData: function getData() {},
resizeId: null
};