import { CSSProperties, ReactNode, ReactElement, createContext, Children, cloneElement, } from 'react' import classNames from 'classnames' import { CommonComponentProps } from '../../utils/types' import './Tabbar.scss' import { TabbarItem, TabbarItemProps } from './TabbarItem' export * from './TabbarItem' export interface TabbarContext { color?: string activeColor?: string activeIndex?: number } export const TabbarContext = createContext({} as TabbarContext) export interface TabbarProps extends CommonComponentProps { className?: string style?: CSSProperties children?: ReactNode color?: string activeColor?: string activeIndex?: number fixed?: boolean onChange?: (index: number) => void } export function Tabbar(props: TabbarProps) { const { className, style, children, color, activeColor, activeIndex, fixed = true, onChange, ...restProps } = props const handleItemClick = (index: number, element: ReactNode) => { onChange?.(index) ;(element as ReactElement).props?.onClick?.(index) } const tabbarClass = classNames( 's-tabbar', { 's-tabbar-fixed': fixed, }, className ) return (
{Children.map(children, (element, index) => cloneElement(element as React.ReactElement, { color, activeColor, index, activeIndex, onClick: (index: number) => handleItemClick(index, element), }) )}
) } Tabbar.Item = TabbarItem export default Tabbar