@nutui/nutui-react
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
191 lines (190 loc) • 8.55 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 React, { useEffect, useRef } from "react";
import classNames from "classnames";
import { JoySmile } from "@nutui/icons-react";
import { ComponentDefaults } from "../../utils/typings";
import TabPane from "../tabpane";
import raf from "../../utils/raf";
import { usePropsValue } from "../../hooks/use-props-value";
import { useForceUpdate } from "../../hooks/use-force-update";
import { useRtl } from "../configprovider";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
tabStyle: {},
activeColor: '',
direction: 'horizontal',
activeType: 'line',
duration: 300,
autoHeight: false
});
var classPrefix = 'nut-tabs';
export var Tabs = function(props) {
var rtl = useRtl();
var _ref = _object_spread({}, defaultProps, props), activeColor = _ref.activeColor, tabStyle = _ref.tabStyle, direction = _ref.direction, activeType = _ref.activeType, duration = _ref.duration, align = _ref.align, title = _ref.title, children = _ref.children, onClick = _ref.onClick, onChange = _ref.onChange, className = _ref.className, autoHeight = _ref.autoHeight, outerValue = _ref.value, outerDefaultValue = _ref.defaultValue, rest = _object_without_properties(_ref, [
"activeColor",
"tabStyle",
"direction",
"activeType",
"duration",
"align",
"title",
"children",
"onClick",
"onChange",
"className",
"autoHeight",
"value",
"defaultValue"
]);
var _usePropsValue = _sliced_to_array(usePropsValue({
value: outerValue,
defaultValue: outerDefaultValue,
onChange: onChange
}), 2), value = _usePropsValue[0], setValue = _usePropsValue[1];
var titleItemsRef = useRef([]);
var navRef = useRef(null);
var scrollDirection = function(nav, to, duration) {
var from = direction === 'horizontal' ? nav.scrollLeft : nav.scrollTop;
var frames = duration === 0 ? 1 : Math.round(duration * 1000 / 16);
var count = 0;
var animate = function() {
if (direction === 'horizontal') nav.scrollLeft += (to - from) / frames;
else nav.scrollTop += (to - from) / frames;
if (++count < frames) raf(animate);
};
animate();
};
var scrollIntoView = function(index, immediate) {
var nav = navRef.current;
var titleItem = titleItemsRef.current;
var titlesLength = titles.current.length;
var itemLength = titleItem.length;
if (!nav || !titleItem || !titleItem[itemLength - titlesLength + index]) {
return;
}
var title = titleItem[itemLength - titlesLength + index];
var to = 0;
if (direction === 'vertical') {
var runTop = title.offsetTop - nav.offsetTop + 10;
to = runTop - (nav.offsetHeight - title.offsetHeight) / 2;
} else {
to = title.offsetLeft - (nav.offsetWidth - title.offsetWidth) / 2;
}
scrollDirection(nav, to, immediate ? 0 : 0.3);
};
var getTitles = function() {
var titles = [];
React.Children.forEach(children, function(child, idx) {
if (/*#__PURE__*/ React.isValidElement(child)) {
var _$props = child.props;
if ((_$props === null || _$props === void 0 ? void 0 : _$props.title) || (_$props === null || _$props === void 0 ? void 0 : _$props.value)) {
titles.push({
title: _$props.title,
value: _$props.value || idx,
disabled: _$props.disabled
});
}
}
});
return titles;
};
var titles = useRef(getTitles());
var forceUpdate = useForceUpdate();
useEffect(function() {
titles.current = getTitles();
var current = '';
titles.current.forEach(function(title) {
if (title.value === value) {
current = value;
}
});
if (current !== '' && current !== value) {
setValue(current);
} else {
forceUpdate();
}
}, [
children
]);
var classes = classNames(classPrefix, "".concat(classPrefix, "-").concat(direction), className);
var _obj;
var classesTitle = classNames("".concat(classPrefix, "-titles"), (_obj = {}, _define_property(_obj, "".concat(classPrefix, "-titles-").concat(activeType), activeType), _define_property(_obj, "".concat(classPrefix, "-titles-scrollable"), true), _define_property(_obj, "".concat(classPrefix, "-titles-").concat(align), align), _obj));
var getContentStyle = function() {
var index = titles.current.findIndex(function(t) {
return t.value === value;
});
index = index < 0 ? 0 : index;
return {
transform: direction === 'horizontal' ? "translate3d(".concat(rtl ? '' : '-').concat(index * 100, "%, 0, 0)") : "translate3d( 0, -".concat(index * 100, "%, 0)"),
transitionDuration: "".concat(duration, "ms")
};
};
useEffect(function() {
var index = titles.current.findIndex(function(t) {
return t.value === value;
});
index = index < 0 ? 0 : index;
setTimeout(function() {
scrollIntoView(index);
});
}, [
value
]);
var tabChange = function(item) {
onClick && onClick(item.value);
if (!item.disabled) {
setValue(item.value);
}
};
return /*#__PURE__*/ React.createElement("div", _object_spread({
className: classes
}, rest), /*#__PURE__*/ React.createElement("div", {
className: classesTitle,
style: tabStyle,
ref: navRef
}, !!title && typeof title === 'function' ? title() : titles.current.map(function(item) {
var _obj;
return /*#__PURE__*/ React.createElement("div", {
key: item.value,
ref: function(ref) {
return titleItemsRef.current.push(ref);
},
onClick: function() {
return tabChange(item);
},
className: classNames("".concat(classPrefix, "-titles-item"), (_obj = {}, _define_property(_obj, "nut-tabs-titles-item-active", !item.disabled && String(item.value) === String(value)), _define_property(_obj, "nut-tabs-titles-item-disabled", item.disabled), _define_property(_obj, "nut-tabs-titles-item-".concat(align), align), _obj))
}, activeType === 'line' && /*#__PURE__*/ React.createElement("div", {
className: classNames("".concat(classPrefix, "-titles-item-line"), "".concat(classPrefix, "-titles-item-line-").concat(direction)),
style: {
background: activeColor
}
}), activeType === 'smile' && /*#__PURE__*/ React.createElement("div", {
className: "".concat(classPrefix, "-titles-item-smile")
}, /*#__PURE__*/ React.createElement(JoySmile, {
color: activeColor,
width: 40,
height: 20
})), /*#__PURE__*/ React.createElement("div", {
className: classNames(_define_property({}, "".concat(classPrefix, "-ellipsis"), direction === 'vertical'), "".concat(classPrefix, "-titles-item-text")),
style: {
color: activeColor
}
}, item.title));
})), /*#__PURE__*/ React.createElement("div", {
className: "".concat(classPrefix, "-content-wrap")
}, /*#__PURE__*/ React.createElement("div", {
className: "".concat(classPrefix, "-content"),
style: getContentStyle()
}, React.Children.map(children, function(child, idx) {
if (!/*#__PURE__*/ React.isValidElement(child)) return null;
return /*#__PURE__*/ React.cloneElement(child, _object_spread_props(_object_spread({}, child.props), {
active: value === child.props.value,
autoHeightClassName: autoHeight && String(value) !== String(child.props.value || idx) ? 'inactive' : ''
}));
}))));
};
Tabs.displayName = 'NutTabs';
Tabs.TabPane = TabPane;