UNPKG

zmp-react

Version:

Build full featured iOS & Android apps using ZMP & React

122 lines (100 loc) 4.91 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.default = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _react = _interopRequireWildcard(require("react")); var _useIsomorphicLayoutEffect = require("../shared/use-isomorphic-layout-effect"); var _utils = require("../shared/utils"); var _mixins = require("../shared/mixins"); var _zmp = require("../shared/zmp"); var _watchProp = require("../shared/watch-prop"); 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; } var ListIndex = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) { var _zmpListIndex = (0, _react.useRef)(null); var className = props.className, id = props.id, style = props.style, children = props.children, _props$init = props.init, init = _props$init === void 0 ? true : _props$init, listEl = props.listEl, _props$indexes = props.indexes, indexes = _props$indexes === void 0 ? 'auto' : _props$indexes, _props$scrollList = props.scrollList, scrollList = _props$scrollList === void 0 ? true : _props$scrollList, _props$label = props.label, label = _props$label === void 0 ? false : _props$label, _props$iosItemHeight = props.iosItemHeight, iosItemHeight = _props$iosItemHeight === void 0 ? 14 : _props$iosItemHeight, _props$mdItemHeight = props.mdItemHeight, mdItemHeight = _props$mdItemHeight === void 0 ? 14 : _props$mdItemHeight, _props$auroraItemHeig = props.auroraItemHeight, auroraItemHeight = _props$auroraItemHeig === void 0 ? 14 : _props$auroraItemHeig; var extraAttrs = (0, _utils.getExtraAttrs)(props); var elRef = (0, _react.useRef)(null); var update = function update() { if (!_zmpListIndex.current) return; _zmpListIndex.current.update(); }; var scrollListToIndex = function scrollListToIndex(indexContent) { if (!_zmpListIndex.current) return; _zmpListIndex.current.scrollListToIndex(indexContent); }; (0, _react.useImperativeHandle)(ref, function () { return { el: elRef.current, zmpListIndex: function zmpListIndex() { return _zmpListIndex.current; }, update: update, scrollListToIndex: scrollListToIndex }; }); (0, _watchProp.watchProp)(indexes, function (newValue) { if (!_zmpListIndex.current) return; _zmpListIndex.current.params.indexes = newValue; update(); }); var onMount = function onMount() { if (!init) return; (0, _zmp.zmpready)(function () { _zmpListIndex.current = _zmp.zmp.listIndex.create({ el: elRef.current, listEl: listEl, indexes: indexes, iosItemHeight: iosItemHeight, mdItemHeight: mdItemHeight, auroraItemHeight: auroraItemHeight, scrollList: scrollList, label: label, on: { select: function select(index, itemContent, itemIndex) { (0, _utils.emit)(props, 'listIndexSelect', itemContent, itemIndex); } } }); }); }; var onDestroy = function onDestroy() { if (_zmpListIndex.current && _zmpListIndex.current.destroy) { _zmpListIndex.current.destroy(); } _zmpListIndex.current = null; }; (0, _useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(function () { onMount(); return onDestroy; }, []); var classes = (0, _utils.classNames)(className, 'list-index', (0, _mixins.colorClasses)(props)); return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({ id: id, style: style, className: classes, ref: elRef }, extraAttrs), children); }); ListIndex.displayName = 'zmp-list-index'; var _default = ListIndex; exports.default = _default;