@nutui/nutui-react-taro
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
295 lines (294 loc) • 13.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: Object.getOwnPropertyDescriptor(all, name).get
});
}
_export(exports, {
get Elevator () {
return Elevator;
},
get elevatorContext () {
return elevatorContext;
}
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _async_to_generator = require("@swc/helpers/_/_async_to_generator");
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 _ts_generator = require("@swc/helpers/_/_ts_generator");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _taro = /*#__PURE__*/ _interop_require_wildcard._(require("@tarojs/taro"));
var _components = require("@tarojs/components");
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _typings = require("../../utils/typings");
var _platform = require("../../utils/taro/platform");
var _useuuid = require("../../hooks/use-uuid");
var _raf = /*#__PURE__*/ _interop_require_default._(require("../../utils/raf"));
var _hooks = require("../../hooks");
var elevatorContext = /*#__PURE__*/ (0, _react.createContext)({});
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
mode: 'horizontal',
height: '200px',
floorKey: 'title',
list: [],
sticky: false,
spaceHeight: 18,
showKeys: true,
defaultValue: undefined,
value: undefined
});
var Elevator = function Elevator(props) {
var _ref = (0, _object_spread._)({}, defaultProps, props), value = _ref.value, defaultValue = _ref.defaultValue, mode = _ref.mode, height = _ref.height, floorKey = _ref.floorKey, list = _ref.list, sticky = _ref.sticky, spaceHeight = _ref.spaceHeight, showKeys = _ref.showKeys, className = _ref.className, style = _ref.style, onItemClick = _ref.onItemClick, onIndexClick = _ref.onIndexClick, children = _ref.children, rest = (0, _object_without_properties._)(_ref, [
"value",
"defaultValue",
"mode",
"height",
"floorKey",
"list",
"sticky",
"spaceHeight",
"showKeys",
"className",
"style",
"onItemClick",
"onIndexClick",
"children"
]);
var uuid = (0, _useuuid.useUuid)();
var classPrefix = 'nut-elevator';
var initData = {
anchorIndex: 0,
listHeight: [],
scrollY: 0
};
var touchState = (0, _react.useRef)({
y1: 0,
y2: 0
});
var _usePropsValue = (0, _sliced_to_array._)((0, _hooks.usePropsValue)({
value: value,
defaultValue: defaultValue || {}
}), 2), currentData = _usePropsValue[0], setCurrentData = _usePropsValue[1];
var _useState = (0, _sliced_to_array._)((0, _react.useState)(0), 2), codeIndex = _useState[0], setCodeIndex = _useState[1];
var _useState1 = (0, _sliced_to_array._)((0, _react.useState)(false), 2), scrollStart = _useState1[0], setScrollStart = _useState1[1];
var state = (0, _react.useRef)(initData);
var scrolling = (0, _react.useRef)(false);
var _useState2 = (0, _sliced_to_array._)((0, _react.useState)(0), 2), scrollTop = _useState2[0], setScrollTop = _useState2[1];
var _useState3 = (0, _sliced_to_array._)((0, _react.useState)(0), 2), scrollY = _useState3[0], setScrollY = _useState3[1];
var resetScrollState = function resetScrollState() {
setScrollStart(false);
};
var getData = function getData(el) {
if (!el.dataset.index) {
return '0';
}
return el.dataset.index;
};
var calculateHeight = function calculateHeight() {
state.current.listHeight = [
0
];
var query = (0, _taro.createSelectorQuery)();
query.selectAll("#".concat(classPrefix, "-").concat(uuid, " .").concat(classPrefix, "-list-item")).boundingClientRect().exec(function() {
var rect = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [];
if (rect[0] && rect[0].length) {
// rect[0] = rect[0].reverse()
state.current.listHeight = rect[0].reduce(function(acc, item, index) {
// 当前项的高度等于前面所有项的高度之和
var height = acc[index] + item.height;
acc.push(height);
return acc;
}, [
0
]);
}
});
};
var scrollTo = function scrollTo(index) {
return (0, _async_to_generator._)(function() {
var cacheIndex, scrollTop;
return (0, _ts_generator._)(this, function(_state) {
switch(_state.label){
case 0:
if (!index && index !== 0) {
return [
2
];
}
if (!!state.current.listHeight.length) return [
3,
2
];
return [
4,
calculateHeight()
];
case 1:
_state.sent();
_state.label = 2;
case 2:
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);
scrollTop = state.current.listHeight[cacheIndex];
setScrollTop(scrollTop);
if (mode === 'vertical' && sticky) {
setScrollY(Math.floor(scrollTop) > 0 ? 1 : 0);
}
return [
2
];
}
});
})();
};
var touchMove = function touchMove(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 touchEnd() {
resetScrollState();
};
var touchStart = function touchStart(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 handleClickItem(key, item) {
onItemClick && onItemClick(key, item);
setCurrentData(item);
};
var handleClickIndex = function handleClickIndex(key) {
onIndexClick && onIndexClick(key);
};
var listViewScroll = function listViewScroll(e) {
(0, _raf.default)(function() {
var listHeight = state.current.listHeight;
if (!listHeight.length) {
calculateHeight();
}
var target = e.target;
var scrollTop = target.scrollTop;
state.current.scrollY = Math.floor(scrollTop);
_taro.default.getEnv() === 'WEB' && setScrollTop(scrollTop);
if (mode === 'vertical' && sticky) {
setScrollY(Math.floor(scrollTop) > 0 ? 1 : 0);
}
if (scrolling.current) return;
var index = listHeight.findIndex(function(height, i) {
return state.current.scrollY >= height && state.current.scrollY < (listHeight[i + 1] || Infinity);
});
if (index !== -1 && index !== codeIndex) {
setCodeIndex(index);
}
});
};
var getWrapStyle = (0, _react.useMemo)(function() {
var calcHeight = Number.isNaN(+height) ? height : "".concat(height, "px");
return {
height: (0, _platform.harmony)() ? Number(height) : calcHeight
};
}, [
height
]);
(0, _react.useEffect)(function() {
(0, _taro.nextTick)(function() {
calculateHeight();
});
}, []);
var _obj;
return /*#__PURE__*/ _react.default.createElement("div", (0, _object_spread._)({
className: "".concat(classPrefix, " ").concat(className, " ").concat(classPrefix, "-").concat(mode, " "),
id: "".concat(classPrefix, "-").concat(uuid),
style: style
}, rest), /*#__PURE__*/ _react.default.createElement(_components.View, {
className: "".concat(classPrefix, "-list"),
style: getWrapStyle
}, /*#__PURE__*/ _react.default.createElement(_components.ScrollView, {
scrollTop: scrollTop,
scrollY: true,
enhanced: true,
scrollWithAnimation: false,
scrollAnchoring: true,
className: "".concat(classPrefix, "-list-inner"),
onScroll: listViewScroll,
onTouchStart: function onTouchStart(e) {
scrolling.current = false;
}
}, list.map(function(item, idx) {
return /*#__PURE__*/ _react.default.createElement(_components.View, {
className: "".concat(classPrefix, "-list-item nut-elevator-item-").concat(idx),
key: idx
}, /*#__PURE__*/ _react.default.createElement(_components.View, {
className: "".concat(classPrefix, "-list-item-code")
}, item[floorKey]), /*#__PURE__*/ _react.default.createElement(_components.View, {
className: "".concat(classPrefix, "-list-item-sublist")
}, item.list.map(function(subitem) {
var _obj;
return /*#__PURE__*/ _react.default.createElement(_components.View, {
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), _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(_components.View, {
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] && String(list[codeIndex][floorKey])) : null, /*#__PURE__*/ _react.default.createElement(_components.View, {
className: "".concat(classPrefix, "-bars")
}, /*#__PURE__*/ _react.default.createElement(_components.View, {
className: "".concat(classPrefix, "-bars-inner")
}, list.map(function(item, index) {
var _obj;
return /*#__PURE__*/ _react.default.createElement(_components.View, {
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[codeIndex][floorKey]), _obj)),
"data-index": index,
key: index,
onClick: function onClick() {
return handleClickIndex(item[floorKey]);
},
onTouchStart: function onTouchStart(event) {
return touchStart(event);
},
onTouchMove: function onTouchMove(event) {
return touchMove(event);
},
onTouchEnd: touchEnd,
style: {
touchAction: 'pan-y'
}
}, String(item[floorKey]));
})))) : null, mode === 'vertical' && sticky && scrollY > 0 ? /*#__PURE__*/ _react.default.createElement(_components.View, {
className: "".concat(classPrefix, "-list-fixed")
}, /*#__PURE__*/ _react.default.createElement(_components.Text, {
className: "".concat(classPrefix, "-list-fixed-title")
}, list[codeIndex] && String(list[codeIndex][floorKey]))) : null);
};
Elevator.displayName = 'NutElevator';
Elevator.Context = elevatorContext;