zmp-react
Version:
Build full featured iOS & Android apps using ZMP & React
104 lines (93 loc) • 3.43 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import React, { forwardRef, useRef, useImperativeHandle } from 'react';
import { useIsomorphicLayoutEffect } from '../shared/use-isomorphic-layout-effect';
import { classNames, getExtraAttrs, emit } from '../shared/utils';
import { colorClasses } from '../shared/mixins';
import { zmpready, zmp } from '../shared/zmp';
import { watchProp } from '../shared/watch-prop';
var ListIndex = /*#__PURE__*/forwardRef(function (props, ref) {
var _zmpListIndex = 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 = getExtraAttrs(props);
var elRef = 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);
};
useImperativeHandle(ref, function () {
return {
el: elRef.current,
zmpListIndex: function zmpListIndex() {
return _zmpListIndex.current;
},
update: update,
scrollListToIndex: scrollListToIndex
};
});
watchProp(indexes, function (newValue) {
if (!_zmpListIndex.current) return;
_zmpListIndex.current.params.indexes = newValue;
update();
});
var onMount = function onMount() {
if (!init) return;
zmpready(function () {
_zmpListIndex.current = 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) {
emit(props, 'listIndexSelect', itemContent, itemIndex);
}
}
});
});
};
var onDestroy = function onDestroy() {
if (_zmpListIndex.current && _zmpListIndex.current.destroy) {
_zmpListIndex.current.destroy();
}
_zmpListIndex.current = null;
};
useIsomorphicLayoutEffect(function () {
onMount();
return onDestroy;
}, []);
var classes = classNames(className, 'list-index', colorClasses(props));
return /*#__PURE__*/React.createElement("div", _extends({
id: id,
style: style,
className: classes,
ref: elRef
}, extraAttrs), children);
});
ListIndex.displayName = 'zmp-list-index';
export default ListIndex;