import * as React from 'react'; import { useWindowDimensions, View } from 'react-native'; import { getElement, useColors } from '../../hook'; import { usePaletteContext } from '../../theme'; import { Icon } from '../../ui/Image'; import type { TabPageHeaderProps } from '../../ui/TabPage'; import { calculateLeft } from '../../ui/TabPage/TabPageHeader.hooks'; import { SingleLineText } from '../../ui/Text'; import { gTabBarHeaderHeight } from './TabPage.const'; export type TabPageTabBarHeaderProps = TabPageHeaderProps & { StateViews?: ( | React.ComponentType | React.ReactElement | null | undefined )[]; }; /** * tab component. */ export const TabPageTabBarHeader: React.FunctionComponent< TabPageTabBarHeaderProps > = (props: TabPageTabBarHeaderProps) => { const { propRef, onClicked, titles: items, width: initWidth, containerStyle, content, initIndex, StateViews, } = props; const { width: winWidth } = useWindowDimensions(); const { colors } = usePaletteContext(); const { getColor } = useColors({ selected: { light: colors.primary[5], dark: colors.primary[6], }, no_selected: { light: colors.neutral[5], dark: colors.neutral[6], }, }); const [currentIndex, setCurrentIndex] = React.useState(initIndex ?? 0); const count = items.length; const width = initWidth ?? winWidth; const { unitWidth } = calculateLeft({ width, count, index: initIndex ?? 0, indicatorWidth: 0, }); if (propRef.current) { propRef.current.toLeft = (movedCount: number) => { if (movedCount === 0) return; const cur = currentIndex - movedCount; setCurrentIndex(cur); }; propRef.current.toRight = (movedCount: number) => { if (movedCount === 0) return; const cur = currentIndex + movedCount; setCurrentIndex(cur); }; } return ( {items.map((v, i) => { const StateView = StateViews?.[i]; return ( { onClicked?.(i); }} > {v.title ? ( {v.title} ) : null} {StateView ? ( {getElement(StateView)} ) : null} ); })} ); };