UNPKG

@td-design/react-native

Version:

react-native UI组件库

126 lines 3.91 kB
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } import React, { memo, useRef } from 'react'; import { ScrollView } from 'react-native'; import { useTheme } from '@shopify/restyle'; import { useMap, useSafeState } from '@td-design/rn-hooks'; import Box from '../box'; import Flex from '../flex'; import Text from '../text'; const VerticalTimeline = _ref => { let { data, customIcon, lineStyle } = _ref; const titleMeasured = useRef(false); const [titleHeight, setTitleHeight] = useSafeState(0); const handleTitleLayout = e => { if (!titleMeasured.current) { titleMeasured.current = true; setTitleHeight(Math.floor(e.nativeEvent.layout.height)); } }; const [_, { set, get }] = useMap(); const handleEventLayout = (e, index) => { set(index, Math.floor(e.nativeEvent.layout.height)); }; const renderItem = (item, index) => { return /*#__PURE__*/React.createElement(Flex, { key: String(index), alignItems: "flex-start" }, /*#__PURE__*/React.createElement(DateAndTime, item), /*#__PURE__*/React.createElement(CircleAndLine, { height: get(index) || 0, isLast: index === data.length - 1, titleHeight: titleHeight, customIcon: customIcon, lineStyle: lineStyle }), /*#__PURE__*/React.createElement(Event, _extends({}, item, { onTitleLayout: handleTitleLayout, onLayout: e => handleEventLayout(e, index) }))); }; return /*#__PURE__*/React.createElement(ScrollView, { style: { flex: 1 }, horizontal: false, showsVerticalScrollIndicator: false }, data.map((item, index) => renderItem(item, index))); }; VerticalTimeline.displayName = 'VerticalTimeline'; export default VerticalTimeline; const DateAndTime = /*#__PURE__*/memo(_ref2 => { let { date, time } = _ref2; return /*#__PURE__*/React.createElement(Box, { paddingLeft: 'x1', alignItems: 'flex-end' }, /*#__PURE__*/React.createElement(Text, { variant: "p0", color: "text", numberOfLines: 1 }, date), /*#__PURE__*/React.createElement(Text, { variant: 'p2', color: "text" }, time)); }); const Event = /*#__PURE__*/memo(_ref3 => { let { title, description, onLayout, onTitleLayout } = _ref3; return /*#__PURE__*/React.createElement(Box, { onLayout: onLayout, flex: 1 }, /*#__PURE__*/React.createElement(Text, { variant: "p0", color: "text", onLayout: onTitleLayout, numberOfLines: 1 }, title), /*#__PURE__*/React.createElement(Text, { variant: "p2", color: "text" }, description)); }); const CircleAndLine = /*#__PURE__*/memo(_ref4 => { let { height, isLast, titleHeight, customIcon, lineStyle } = _ref4; const theme = useTheme(); const [iconHeight, setIconHeight] = useSafeState(theme.borderRadii.x2); const handleLayout = e => { setIconHeight(Math.floor(e.nativeEvent.layout.height)); }; return /*#__PURE__*/React.createElement(Box, { alignItems: 'center', style: { paddingHorizontal: theme.spacing.x2, top: titleHeight / 2 - iconHeight / 2 } }, customIcon ? /*#__PURE__*/React.createElement(Box, { backgroundColor: 'white', onLayout: handleLayout }, customIcon) : /*#__PURE__*/React.createElement(Box, { width: theme.borderRadii.x2, height: theme.borderRadii.x2, backgroundColor: "primary200", borderRadius: "x2" }), !isLast && /*#__PURE__*/React.createElement(Box, { width: 1, height: height, backgroundColor: 'border', style: lineStyle })); }); //# sourceMappingURL=VerticalTimeline.js.map