import React, { ReactNode, isValidElement, forwardRef, ForwardRefRenderFunction, useImperativeHandle, Children, } from 'react'; import { View, StyleSheet, StyleProp, ViewStyle, Text, TextStyle, } from 'react-native'; import { isNil, isString, isBoolean } from 'lodash'; import { mergeProps } from '../../core/helpers'; import ShouldRender from '../ShouldRender'; import createStyle from './styles/list.style'; import { useTheme } from '../Theme/Theme'; export interface ListProps { /** * 头部插槽 */ header?: ReactNode; /** * 底部插槽 */ footer?: ReactNode; /** * 分割线 * @default true */ separator?: boolean | ReactNode; children?: ReactNode; /** * 容器样式 */ style?: StyleProp; /** * 头部文字样式,header为string类型时生效 */ headerTextStyle?: StyleProp; /** * 底部文字样式,footer为string类型时生效 */ footerTextStyle?: StyleProp; /** * body样式 */ bodyStyle?: StyleProp; } const defaultProps = { separator: true, }; export interface ListRef {} const List: ForwardRefRenderFunction = (p, ref) => { const props = mergeProps(defaultProps, p); const token = useTheme(); const styles = createStyle(token); // header const header = ( <> {/* 渲染header */} {/* header为string类型 */} {props.header} {/* header为元素类型 */} {props.header} ); // footer const footer = ( <> {/* 渲染footer */} {/* footer为string类型 */} {props.footer} {/* footer为元素类型 */} {props.footer} ); // 分割线 const separator = (index: number) => { return ( <> {/* 最后一个child没有底部边框 */} {/* 布尔类型且为true */} {/* 自定义分割线 */} {props.separator} ); }; // body const body = ( <> {/* 渲染list的children */} {Children.map(props.children, (child, index) => { return ( {child} {child} {separator(index)} ); })} ); // 预留向外界暴露的对象 useImperativeHandle(ref, () => ({})); return ( {header} {body} {footer} ); }; export default forwardRef(List);