UNPKG

@nutui/nutui-react-taro

Version:

京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序

135 lines (134 loc) 5.46 kB
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, { Children, useImperativeHandle, useMemo, useState, useEffect } from "react"; import { Swiper as TaroSwiper, SwiperItem as TSwiperItem, View } from "@tarojs/components"; import classNames from "classnames"; import Indicator from "../indicator/index"; import { pxTransform } from "../../utils/taro/px-transform"; var defaultProps = { direction: 'horizontal', indicator: false, autoPlay: false, autoplay: false, loop: false, defaultValue: 0, style: {} }; var classPrefix = 'nut-swiper'; export var Swiper = /*#__PURE__*/ React.forwardRef(function(props, ref) { var _ref = _object_spread({}, defaultProps, props), width = _ref.width, height = _ref.height, className = _ref.className, children = _ref.children, indicator = _ref.indicator, indicatorDots = _ref.indicatorDots, loop = _ref.loop, circular = _ref.circular, autoPlay = _ref.autoPlay, autoplay = _ref.autoplay, vertical = _ref.vertical, direction = _ref.direction, defaultValue = _ref.defaultValue, current = _ref.current, onChange = _ref.onChange, style = _ref.style, rest = _object_without_properties(_ref, [ "width", "height", "className", "children", "indicator", "indicatorDots", "loop", "circular", "autoPlay", "autoplay", "vertical", "direction", "defaultValue", "current", "onChange", "style" ]); var _useState = _sliced_to_array(useState(current || defaultValue), 2), innerValue = _useState[0], setInnerValue = _useState[1]; var childrenCount = useMemo(function() { var c = 0; React.Children.map(children, function(child, index) { c += 1; }); return c; }, [ children ]); useEffect(function() { setInnerValue(current || defaultValue); }, [ defaultValue, current ]); var renderIndicator = function() { if (/*#__PURE__*/ React.isValidElement(indicator)) return indicator; if (indicator || indicatorDots) { var _obj; return /*#__PURE__*/ React.createElement(View, { className: classNames((_obj = {}, _define_property(_obj, "".concat(classPrefix, "-indicator"), true), _define_property(_obj, "".concat(classPrefix, "-indicator-vertical"), direction === 'vertical' || vertical), _obj)) }, /*#__PURE__*/ React.createElement(Indicator, { current: innerValue, total: childrenCount, direction: vertical ? 'vertical' : direction })); } return null; }; var handleOnChange = function(value) { setInnerValue(value.detail.current); }; useImperativeHandle(ref, function() { return { to: function(value) { setInnerValue(value); }, next: function() { if (loop) { setInnerValue((innerValue + 1) % childrenCount); } else { setInnerValue(innerValue + 1 >= childrenCount ? innerValue : innerValue + 1); } }, prev: function() { if (loop) { var next = innerValue - 1; next = next < 0 ? childrenCount + next : next; setInnerValue(next % childrenCount); } else { setInnerValue(innerValue - 1 <= 0 ? 0 : innerValue - 1); } } }; }); return /*#__PURE__*/ React.createElement(View, { className: classNames(classPrefix, className), style: _object_spread_props(_object_spread({}, style), { width: !width ? '100%' : width, height: !height ? pxTransform(150) : height }) }, /*#__PURE__*/ React.createElement(View, { className: "nut-swiper-inner", style: { width: !width ? '100%' : width, height: !height ? pxTransform(150) : height } }, /*#__PURE__*/ React.createElement(TaroSwiper, _object_spread({ current: innerValue, circular: loop, autoplay: autoplay || autoPlay, vertical: direction === 'vertical' || vertical, indicatorDots: false, onChange: function(e) { var _props_onChange; handleOnChange(e); (_props_onChange = props.onChange) === null || _props_onChange === void 0 ? void 0 : _props_onChange.call(props, e); }, style: { width: !width ? '100%' : width, height: !height ? pxTransform(150) : height } }, rest), Children.toArray(children).map(function(child, index) { var className; if (/*#__PURE__*/ React.isValidElement(child)) { className = child.props.className; } return /*#__PURE__*/ React.createElement(TSwiperItem, { className: className, key: index }, child); }))), renderIndicator()); }); Swiper.displayName = 'NutSwiper';