@nutui/nutui-react
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
240 lines (239 loc) • 10.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
Elevator: function() {
return Elevator;
},
elevatorContext: function() {
return elevatorContext;
}
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _define_property = require("@swc/helpers/_/_define_property");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _react1 = require("@use-gesture/react");
var _web = require("@react-spring/web");
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _typings = require("../../utils/typings");
var elevatorContext = /*#__PURE__*/ (0, _react.createContext)({});
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
height: '200px',
floorKey: 'title',
list: [],
sticky: false,
spaceHeight: 23,
titleHeight: 35,
showKeys: true
});
var Elevator = function Elevator(props) {
var _ref = (0, _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 = (0, _object_without_properties._)(_ref, [
"height",
"floorKey",
"list",
"sticky",
"spaceHeight",
"titleHeight",
"showKeys",
"className",
"style",
"onItemClick",
"onIndexClick",
"children"
]);
var classPrefix = 'nut-elevator';
var listview = (0, _react.useRef)(null);
var initData = {
anchorIndex: 0,
listHeight: [],
listGroup: [],
scrollY: 0
};
var touchState = (0, _react.useRef)({
y1: 0,
y2: 0
});
var _useState = (0, _sliced_to_array._)((0, _react.useState)(0), 2), scrollY = _useState[0], setScrollY = _useState[1];
var _useState1 = (0, _sliced_to_array._)((0, _react.useState)({}), 2), currentData = _useState1[0], setCurrentData = _useState1[1];
var _useState2 = (0, _sliced_to_array._)((0, _react.useState)(''), 2), currentKey = _useState2[0], setCurrentKey = _useState2[1];
var _useState3 = (0, _sliced_to_array._)((0, _react.useState)(0), 2), currentIndex = _useState3[0], setCurrentIndex = _useState3[1];
var _useState4 = (0, _sliced_to_array._)((0, _react.useState)(0), 2), codeIndex = _useState4[0], setCodeIndex = _useState4[1];
var _useState5 = (0, _sliced_to_array._)((0, _react.useState)(false), 2), scrollStart = _useState5[0], setScrollStart = _useState5[1];
var state = (0, _react.useRef)(initData);
var resetScrollState = function resetScrollState() {
setScrollStart(false);
};
var clientHeight = function clientHeight() {
return listview.current ? listview.current.clientHeight : 0;
};
var getData = function getData(el, name) {
var prefix = 'data-';
return el.getAttribute(prefix + name);
};
var calculateHeight = function calculateHeight() {
var height = 0;
state.current.listHeight.push(height);
for(var i = 0; i < state.current.listGroup.length; i++){
var item = state.current.listGroup[i];
height += item.clientHeight;
state.current.listHeight.push(height);
}
};
var scrollTo = function scrollTo(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);
if (listview.current) {
listview.current.scrollTo(0, state.current.listHeight[cacheIndex]);
}
};
var bind = (0, _react1.useGesture)({
onDragStart: function onDragStart(param) {
var target = param.target, offset = param.offset;
setScrollStart(true);
var index = Number(getData(target, 'index'));
touchState.current.y1 = offset[1];
state.current.anchorIndex = +index;
setCodeIndex(function(codeIndex) {
return codeIndex + index;
});
scrollTo(index);
},
onDragEnd: function onDragEnd(param) {
var offset = param.offset;
touchState.current.y2 = offset[1];
var delta = (touchState.current.y2 - touchState.current.y1) / spaceHeight || 0;
// delta 是一个浮点数, 需要四舍五入一下, 否则页面会找不到最终计算后的index
var cacheIndex = state.current.anchorIndex + Math.round(delta);
setCodeIndex(cacheIndex);
scrollTo(cacheIndex);
resetScrollState();
}
});
var handleClickItem = function handleClickItem(key, item) {
onItemClick && onItemClick(key, item);
setCurrentData(item);
setCurrentKey(key);
};
var handleClickIndex = function handleClickIndex(key) {
onIndexClick && onIndexClick(key);
};
var setListGroup = function setListGroup() {
if (listview.current) {
var els = listview.current.querySelectorAll('.nut-elevator-list-item');
els.forEach(function(el) {
if (el != null && !state.current.listGroup.includes(el)) {
state.current.listGroup.push(el);
}
});
}
};
var listViewScroll = function listViewScroll(e) {
var listHeight = state.current.listHeight;
if (!listHeight.length) {
calculateHeight();
}
var target = e.target;
var scrollTop = target.scrollTop;
scrollTop = Math.ceil(scrollTop);
state.current.scrollY = scrollTop;
setScrollY(scrollTop);
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) {
setCurrentIndex(i);
return;
}
}
setCurrentIndex(listHeight.length - 2);
};
(0, _react.useEffect)(function() {
if (listview.current) {
setListGroup();
listview.current.addEventListener('scroll', listViewScroll);
}
}, [
listview
]);
var _obj;
return /*#__PURE__*/ _react.default.createElement("div", (0, _object_spread._)({
className: "".concat(classPrefix, " ").concat(className),
style: style
}, rest), sticky && scrollY > 0 ? /*#__PURE__*/ _react.default.createElement("div", {
className: "".concat(classPrefix, "-list-fixed")
}, /*#__PURE__*/ _react.default.createElement("span", {
className: "".concat(classPrefix, "-list-fixed-title")
}, list[currentIndex][floorKey])) : null, /*#__PURE__*/ _react.default.createElement("div", {
className: "".concat(classPrefix, "-list"),
style: {
height: Number.isNaN(+height) ? height : "".concat(height, "px")
}
}, /*#__PURE__*/ _react.default.createElement("div", {
className: "".concat(classPrefix, "-list-inner"),
ref: listview
}, list.map(function(item, idx) {
return /*#__PURE__*/ _react.default.createElement("div", {
className: "".concat(classPrefix, "-list-item"),
key: idx
}, /*#__PURE__*/ _react.default.createElement("div", {
className: "".concat(classPrefix, "-list-item-code")
}, item[floorKey]), /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, item.list.map(function(subitem) {
var _obj;
return /*#__PURE__*/ _react.default.createElement("div", {
className: (0, _classnames.default)((_obj = {}, (0, _define_property._)(_obj, "".concat(classPrefix, "-list-item-name"), true), (0, _define_property._)(_obj, "".concat(classPrefix, "-list-item-name-highcolor"), currentData.id === subitem.id && currentKey === item[floorKey]), _obj)),
key: subitem.id,
onClick: function onClick() {
return handleClickItem(item[floorKey], subitem);
}
}, children ? /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/ _react.default.createElement(elevatorContext.Provider, {
value: subitem
}, children)) : subitem.name);
})));
}))), showKeys ? /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, list.length && scrollStart ? /*#__PURE__*/ _react.default.createElement("div", {
className: (0, _classnames.default)((_obj = {}, (0, _define_property._)(_obj, "".concat(classPrefix, "-code-current"), true), (0, _define_property._)(_obj, "".concat(classPrefix, "-code-current-current"), true), _obj))
}, list[codeIndex][floorKey]) : null, /*#__PURE__*/ _react.default.createElement("div", {
className: "".concat(classPrefix, "-bars")
}, /*#__PURE__*/ _react.default.createElement(_web.animated.div, (0, _object_spread_props._)((0, _object_spread._)({
className: "".concat(classPrefix, "-bars-inner")
}, bind()), {
style: {
touchAction: 'pan-y'
}
}), list.map(function(item, index) {
var _obj;
return /*#__PURE__*/ _react.default.createElement("div", {
className: (0, _classnames.default)((_obj = {}, (0, _define_property._)(_obj, "".concat(classPrefix, "-bars-inner-item"), true), (0, _define_property._)(_obj, "".concat(classPrefix, "-bars-inner-item-active"), item[floorKey] === list[currentIndex][floorKey]), _obj)),
"data-index": index,
key: index,
onClick: function onClick() {
return handleClickIndex(item[floorKey]);
}
}, item[floorKey]);
})))) : null);
};
Elevator.displayName = 'NutElevator';
Elevator.Context = elevatorContext;