@td-design/react-native
Version:
react-native UI组件库
111 lines • 3.5 kB
JavaScript
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 } 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 HorizontalTimeline = _ref => {
let {
data,
customIcon,
lineStyle
} = _ref;
const [_, {
set,
get
}] = useMap();
const handleDateLayout = (e, index) => {
const textWidth = e.nativeEvent.lines[0].width;
set(index, Math.floor(textWidth));
};
const renderItem = (item, index) => {
return /*#__PURE__*/React.createElement(Box, {
key: String(index),
maxWidth: 120,
marginRight: 'x2'
}, /*#__PURE__*/React.createElement(DateAndTime, _extends({}, item, {
onLayout: e => handleDateLayout(e, index)
})), /*#__PURE__*/React.createElement(CircleAndLine, {
width: get(index) || 0,
isLast: index === data.length - 1,
customIcon: customIcon,
lineStyle: lineStyle
}), /*#__PURE__*/React.createElement(TitleAndDescription, item));
};
return /*#__PURE__*/React.createElement(ScrollView, {
style: {
flex: 1
},
horizontal: true,
showsHorizontalScrollIndicator: false
}, data.map((item, index) => renderItem(item, index)));
};
HorizontalTimeline.displayName = 'HorizontalTimeline';
export default HorizontalTimeline;
const DateAndTime = /*#__PURE__*/memo(_ref2 => {
let {
date,
time,
onLayout
} = _ref2;
return /*#__PURE__*/React.createElement(Box, {
paddingLeft: 'x1'
}, /*#__PURE__*/React.createElement(Text, {
variant: "p0",
color: "text",
numberOfLines: 1,
onTextLayout: onLayout
}, date), /*#__PURE__*/React.createElement(Text, {
variant: 'p2',
color: "text"
}, time));
});
const TitleAndDescription = /*#__PURE__*/memo(_ref3 => {
let {
title,
description
} = _ref3;
return /*#__PURE__*/React.createElement(Box, {
paddingLeft: 'x1'
}, /*#__PURE__*/React.createElement(Text, {
variant: 'p0',
color: 'text'
}, title), /*#__PURE__*/React.createElement(Text, {
variant: 'p2',
color: 'text'
}, description));
});
const CircleAndLine = /*#__PURE__*/memo(_ref4 => {
let {
isLast,
width,
customIcon,
lineStyle
} = _ref4;
const theme = useTheme();
const [iconWidth, setIconWidth] = useSafeState(theme.borderRadii.x2);
const handleLayout = e => {
setIconWidth(Math.floor(e.nativeEvent.layout.width));
};
return /*#__PURE__*/React.createElement(Flex, {
left: width / 2 - iconWidth / 2,
paddingVertical: 'x1',
alignItems: 'center'
}, 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: 120 + theme.spacing.x2,
height: 1,
backgroundColor: 'border',
style: lineStyle
}));
});
//# sourceMappingURL=HorizontalTimeline.js.map