@nutui/nutui-react-taro
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
250 lines (249 loc) • 10.8 kB
JavaScript
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
import React, { useEffect, useRef, useState, createContext, useMemo } from "react";
import Taro, { nextTick, createSelectorQuery } from "@tarojs/taro";
import { ScrollView, View, Text } from "@tarojs/components";
import classNames from "classnames";
import { ComponentDefaults } from "../../utils/typings";
import { harmony } from "../../utils/platform-taro";
import useUuid from "../../hooks/use-uuid";
export var elevatorContext = /*#__PURE__*/ createContext({});
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
height: '200px',
floorKey: 'title',
list: [],
sticky: false,
spaceHeight: 23,
titleHeight: 35,
showKeys: true,
className: 'weapp-elevator'
});
export var Elevator = function(props) {
var _ref = _object_spread({}, defaultProps, props), height = _ref.height, floorKey = _ref.floorKey, list = _ref.list, sticky = _ref.sticky, spaceHeight = _ref.spaceHeight, titleHeight = _ref.titleHeight, showKeys = _ref.showKeys, className = _ref.className, style = _ref.style, onItemClick = _ref.onItemClick, onIndexClick = _ref.onIndexClick, children = _ref.children, rest = _object_without_properties(_ref, [
"height",
"floorKey",
"list",
"sticky",
"spaceHeight",
"titleHeight",
"showKeys",
"className",
"style",
"onItemClick",
"onIndexClick",
"children"
]);
var uuid = useUuid();
var classPrefix = 'nut-elevator';
var listview = useRef(null);
var initData = {
anchorIndex: 0,
listHeight: [],
listGroup: [],
scrollY: 0
};
var touchState = useRef({
y1: 0,
y2: 0
});
var _useState = _sliced_to_array(useState({}), 2), currentData = _useState[0], setCurrentData = _useState[1];
var _useState1 = _sliced_to_array(useState(''), 2), currentKey = _useState1[0], setCurrentKey = _useState1[1];
var _useState2 = _sliced_to_array(useState(0), 2), codeIndex = _useState2[0], setCodeIndex = _useState2[1];
var _useState3 = _sliced_to_array(useState(false), 2), scrollStart = _useState3[0], setScrollStart = _useState3[1];
var state = useRef(initData);
var scrolling = useRef(false);
var _useState4 = _sliced_to_array(useState(0), 2), scrollTop = _useState4[0], setScrollTop = _useState4[1];
var _useState5 = _sliced_to_array(useState(0), 2), scrollY = _useState5[0], setScrollY = _useState5[1];
var resetScrollState = function() {
setScrollStart(false);
};
var getData = function(el) {
if (!el.dataset.index) {
return '0';
}
return el.dataset.index;
};
var calculateHeight = function() {
var height = 0;
state.current.listHeight.push(height);
for(var i = 0; i < state.current.listGroup.length; i++){
var query = createSelectorQuery();
query.selectAll(".".concat(classPrefix, "-").concat(uuid, " .nut-elevator-item-").concat(i)).boundingClientRect();
// eslint-disable-next-line no-loop-func
query.exec(function(res) {
if (res[0][0]) height += res[0][0].height;
state.current.listHeight.push(height);
});
}
};
var scrollTo = function(index) {
if (!index && index !== 0) {
return;
}
if (!state.current.listHeight.length) {
calculateHeight();
}
var cacheIndex = index;
if (index < 0) {
cacheIndex = 0;
}
if (index > state.current.listHeight.length - 2) {
cacheIndex = Math.max(0, state.current.listHeight.length - 2);
}
setCodeIndex(cacheIndex);
var scrollTop = state.current.listHeight[cacheIndex];
setScrollTop(scrollTop);
if (sticky && scrollY !== scrollTop) {
setScrollY(Math.floor(scrollTop) > 0 ? 1 : 0);
}
};
var touchMove = function(e) {
var firstTouch = e.touches[0];
touchState.current.y2 = firstTouch.pageY;
var delta = (touchState.current.y2 - touchState.current.y1) / spaceHeight || 0;
var cacheIndex = state.current.anchorIndex + Math.round(delta);
setCodeIndex(cacheIndex);
scrollTo(cacheIndex);
};
var touchEnd = function() {
resetScrollState();
};
var touchStart = function(e) {
var index = Number(getData(e.target));
var firstTouch = e.touches[0];
scrolling.current = true;
touchState.current.y1 = firstTouch.pageY;
state.current.anchorIndex = +index;
setScrollStart(true);
setCodeIndex(index);
scrollTo(index);
};
var handleClickItem = function(key, item) {
onItemClick && onItemClick(key, item);
setCurrentData(item);
setCurrentKey(key);
};
var handleClickIndex = function(key) {
onIndexClick && onIndexClick(key);
};
var setListGroup = function() {
if (listview.current) {
createSelectorQuery().selectAll(".".concat(classPrefix, "-").concat(uuid, " .nut-elevator-list-item")).node(function(el) {
state.current.listGroup = _to_consumable_array(Object.keys(el));
calculateHeight();
}).exec();
}
};
var listViewScroll = function(e) {
var listHeight = state.current.listHeight;
if (!listHeight.length) {
calculateHeight();
}
var target = e.target;
var scrollTop = target.scrollTop;
state.current.scrollY = Math.floor(scrollTop);
Taro.getEnv() === 'WEB' && setScrollTop(scrollTop);
if (sticky && scrollTop !== scrollY) {
setScrollY(Math.floor(scrollTop) > 0 ? 1 : 0);
}
if (scrolling.current) return;
for(var i = 0; i < listHeight.length - 1; i++){
var height1 = listHeight[i];
var height2 = listHeight[i + 1];
if (state.current.scrollY >= height1 && state.current.scrollY < height2) {
return setCodeIndex(i);
}
}
};
var getWrapStyle = useMemo(function() {
var calcHeight = Number.isNaN(+height) ? height : "".concat(height, "px");
return {
height: harmony() ? Number(height) : calcHeight
};
}, [
height
]);
useEffect(function() {
if (listview.current) {
nextTick(function() {
setListGroup();
});
}
}, [
listview
]);
var _obj;
return /*#__PURE__*/ React.createElement("div", _object_spread({
className: "".concat(classPrefix, " ").concat(className, " ").concat(classPrefix, "-").concat(uuid),
style: style
}, rest), /*#__PURE__*/ React.createElement(View, {
className: "".concat(classPrefix, "-list"),
style: getWrapStyle
}, /*#__PURE__*/ React.createElement(ScrollView, {
scrollTop: scrollTop,
scrollY: true,
scrollWithAnimation: true,
scrollAnchoring: true,
className: "".concat(classPrefix, "-list-inner"),
type: "list",
ref: listview,
onScroll: listViewScroll,
onTouchStart: function(e) {
scrolling.current = false;
}
}, list.map(function(item, idx) {
return /*#__PURE__*/ React.createElement(View, {
className: "".concat(classPrefix, "-list-item nut-elevator-item-").concat(idx),
key: idx
}, /*#__PURE__*/ React.createElement(View, {
className: "".concat(classPrefix, "-list-item-code")
}, item[floorKey]), /*#__PURE__*/ React.createElement(React.Fragment, null, item.list.map(function(subitem) {
var _obj;
return /*#__PURE__*/ React.createElement(View, {
className: classNames((_obj = {}, _define_property(_obj, "".concat(classPrefix, "-list-item-name"), true), _define_property(_obj, "".concat(classPrefix, "-list-item-name-highcolor"), currentData.id === subitem.id && currentKey === item[floorKey]), _obj)),
key: subitem.id,
onClick: function() {
return handleClickItem(item[floorKey], subitem);
}
}, children ? /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(elevatorContext.Provider, {
value: subitem
}, children)) : subitem.name);
})));
}))), showKeys ? /*#__PURE__*/ React.createElement(React.Fragment, null, list.length && scrollStart ? /*#__PURE__*/ React.createElement(View, {
className: classNames((_obj = {}, _define_property(_obj, "".concat(classPrefix, "-code-current"), true), _define_property(_obj, "".concat(classPrefix, "-code-current-current"), true), _obj))
}, list[codeIndex][floorKey]) : null, /*#__PURE__*/ React.createElement(View, {
className: "".concat(classPrefix, "-bars")
}, /*#__PURE__*/ React.createElement(View, {
className: "".concat(classPrefix, "-bars-inner")
}, list.map(function(item, index) {
var _obj;
return /*#__PURE__*/ React.createElement(View, {
className: classNames((_obj = {}, _define_property(_obj, "".concat(classPrefix, "-bars-inner-item"), true), _define_property(_obj, "".concat(classPrefix, "-bars-inner-item-active"), item[floorKey] === list[codeIndex][floorKey]), _obj)),
"data-index": index,
key: index,
onClick: function() {
return handleClickIndex(item[floorKey]);
},
onTouchStart: function(event) {
return touchStart(event);
},
onTouchMove: function(event) {
return touchMove(event);
},
onTouchEnd: touchEnd,
style: {
touchAction: 'pan-y'
}
}, item[floorKey]);
})))) : null, sticky && scrollY > 0 ? /*#__PURE__*/ React.createElement(View, {
className: "".concat(classPrefix, "-list-fixed")
}, /*#__PURE__*/ React.createElement(Text, {
className: "".concat(classPrefix, "-list-fixed-title")
}, list[codeIndex][floorKey])) : null);
};
Elevator.displayName = 'NutElevator';
Elevator.Context = elevatorContext;