UNPKG

@zohodesk/components

Version:

In this Package, we Provide Some Basic Components to Build Web App

269 lines (225 loc) • 11.3 kB
"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.CardContent = CardContent; exports.CardFooter = CardFooter; exports.CardHeader = CardHeader; exports["default"] = Card; var _react = _interopRequireWildcard(require("react")); var _hooks = require("@zohodesk/hooks"); var _propTypes = require("./props/propTypes"); var _defaultProps = require("./props/defaultProps"); var _Layout = require("../Layout"); var _Config = require("../../Provider/Config"); var _CardModule = _interopRequireDefault(require("../../Card/Card.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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } 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 _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } /* eslint-disable react/forbid-component-props */ /* isscroll header border change use ref scroll logic remove from here scroll logic move to virtualizer list */ /* performance handling pending in card component moving to ref instead of setState */ function CardHeader(props) { var isScroll = props.isScroll, children = props.children, dataId = props.dataId, dataSelectorId = props.dataSelectorId, customClass = props.customClass; var cardHeader = (0, _react.useRef)(null); function getCardHeaderDom(ele) { cardHeader.current = ele; } function setScrollClassName() { if (!cardHeader.current) { return null; } } return /*#__PURE__*/_react["default"].createElement(_Layout.Box, { className: "".concat(isScroll ? _CardModule["default"].scroll : _CardModule["default"].notScroll, " ").concat(customClass), eleRef: getCardHeaderDom, dataId: dataId, dataSelectorId: dataSelectorId }, children); } CardHeader.propTypes = _propTypes.CardHeader_propTypes; CardHeader.defaultProps = _defaultProps.CardHeader_defaultProps; function CardContent(props) { var onScroll = props.onScroll, eleRef = props.eleRef, children = props.children, scroll = props.scroll, isScrollAttribute = props.isScrollAttribute, dataId = props.dataId, dataSelectorId = props.dataSelectorId, shrink = props.shrink, customClass = props.customClass, preventParentScroll = props.preventParentScroll; return /*#__PURE__*/_react["default"].createElement(_Layout.Box, { flexible: true, onScroll: onScroll, eleRef: eleRef, scroll: scroll, preventParentScroll: preventParentScroll, isScrollAttribute: isScrollAttribute, dataId: dataId, shrink: shrink, className: customClass, dataSelectorId: dataSelectorId }, children); } CardContent.propTypes = _propTypes.CardContent_propTypes; CardContent.defaultProps = _defaultProps.CardContent_defaultProps; function Card(props) { var onClick = props.onClick, children = props.children, isScrollAttribute = props.isScrollAttribute, dataId = props.dataId, eleRef = props.eleRef, _props$customClass = props.customClass, customClass = _props$customClass === void 0 ? '' : _props$customClass, htmlId = props.htmlId, a11y = props.a11y, from = props.from, limit = props.limit, fetchData = props.fetchData, noMoreData = props.noMoreData, scrollAt = props.scrollAt, noNeedUpScroll = props.noNeedUpScroll, isPercentageScroll = props.isPercentageScroll, onScroll = props.onScroll; var role = a11y.role; var _useState = (0, _react.useState)(false), _useState2 = _slicedToArray(_useState, 2), isScroll = _useState2[0], setScroll = _useState2[1]; var fromRef = (0, _react.useRef)(from); var limitRef = (0, _react.useRef)(limit); var to = (0, _react.useRef)(from + 3 * limit); var lastScrollTop = (0, _react.useRef)(0); var isFetching = (0, _react.useRef)(false); var scrollDirection = (0, _react.useRef)(null); (0, _hooks.useEffectCallOnlyAfterState)(function () { fromRef.current = from; }, [from]); function onCardScroll(e) { var scrollContainerObj = e.currentTarget; var scrollHeight = scrollContainerObj.scrollHeight; var scrollTop = scrollContainerObj.scrollTop; var offsetHeight = scrollContainerObj.offsetHeight; onScroll && onScroll(e); if (scrollTop > lastScrollTop.current) { scrollDirection.current = 'down'; } else { scrollDirection.current = 'up'; } lastScrollTop.current = scrollTop; if (fetchData && !isFetching.current) { if (scrollDirection.current === 'down' && !noMoreData) { var scrollingPercentage = (scrollTop + offsetHeight) / (scrollHeight - scrollAt) * 100; var prefetch = isPercentageScroll ? scrollingPercentage >= (0, _Config.getLibraryConfig)('scrollFetchLimit') : scrollHeight - scrollAt <= scrollTop + offsetHeight; if (prefetch) { isFetching.current = true; fetchData(fromRef.current + limitRef.current, limitRef.current + to.current, scrollDirection.current).then(function () { to.current = limitRef.current + to.current; isFetching.current = false; }, function () { isFetching.current = false; }); } } else { if (0 >= scrollTop - scrollAt && fromRef.current !== 0 && !noNeedUpScroll) { isFetching.current = true; fetchData(fromRef.current - limitRef.current, to.current - limitRef.current, scrollDirection.current).then(function () { to.current = to.current - limitRef.current; isFetching.current = false; }, function () { isFetching.current = false; }); } } } if (fetchData && !noNeedUpScroll) { if (fromRef.current !== 0 && scrollTop === 0 && !noNeedUpScroll) { scrollContainerObj.scrollTop = scrollTop + offsetHeight / 3; } else if (scrollHeight === scrollTop + offsetHeight && !noMoreData) { scrollContainerObj.scrollTop = scrollTop - offsetHeight / 2; } } // if (isScrollShadow) { // this.onSetScroll(); // } } // setScroll(isScroll) { // let { isScroll: stateIsScroll } = this.state; // if (isScroll && !stateIsScroll) { // this.setState({ isScroll: true }, () => { // this.onClearScroll(); // }); // } else if (!isScroll && stateIsScroll) { // this.setState({ isScroll: false }); // } // } return /*#__PURE__*/_react["default"].createElement(_Layout.Container, { className: "".concat(customClass), isScrollAttribute: isScrollAttribute, onClick: onClick, dataId: dataId, eleRef: eleRef }, _react["default"].Children.map(children, function (child) { if (!child) { return child; } if (child.type === CardHeader || props.childTypes && child.type === props.childTypes.cardHeader) { return /*#__PURE__*/_react["default"].cloneElement(child, { isScroll: isScroll }); } else if (child.type === CardContent || props.childTypes && child.type === props.childTypes.cardContent) { return /*#__PURE__*/_react["default"].createElement(_Layout.Box, { id: htmlId, role: role, flexible: true, scroll: child.props.scroll, preventParentScroll: child.props.preventParentScroll, onScroll: onCardScroll, eleRef: child.props.eleRef, isScrollAttribute: child.props.isScrollAttribute, dataId: child.props.dataId, shrink: child.props.shrink, className: child.props.customClass, dataSelectorId: child.props.dataSelectorId }, child.props.children); } return child; })); } Card.propTypes = _propTypes.Card_propTypes; Card.defaultProps = _defaultProps.Card_defaultProps; // if (__DOCS__) { // Card.docs = { // componentGroup: 'Template', // folderName: 'Style Guide', // external: true, // description: ' ' // }; // } function CardFooter(props) { var children = props.children, dataId = props.dataId, customClass = props.customClass, dataSelectorId = props.dataSelectorId; return /*#__PURE__*/_react["default"].createElement(_Layout.Box, { className: "".concat(customClass), dataId: dataId, dataSelectorId: dataSelectorId }, children); } CardFooter.propTypes = _propTypes.CardFooter_propTypes; CardFooter.defaultProps = _defaultProps.CardFooter_defaultProps;