import { ScrollView, View } from '@tarojs/components' import type { ReactNode } from 'react' import { memo, useEffect, useState } from 'react' import classnames from 'classnames' import styles from './index.module.scss' interface ListProps { title: string value: number } interface Params { list?: ListProps[] defaultValue?: number|string children?: ReactNode tabsOnClick?: (ListProps) => void style?: Object } const Tabs = ({ list = [], defaultValue = 0, tabsOnClick, style = {} }: Params) => { const [selected, setSelected] = useState(defaultValue) const [tabId, setTabId] = useState('') useEffect(() => { const index = list?.findIndex((item) => { return item.value == defaultValue }) if (index !== -1) { const num = index > 0 ? (index - 1) : 0 setTabId(list[num].value.toString()) } }, []) const clickEvent = ({ item, index }: { item: ListProps; index: number }) => { setSelected(item.value) tabsOnClick?.(item) const num = index > 0 ? (index - 1) : 0 setTabId(list[num].value.toString()) } return ( <> { list.map((item, index) => { return ( clickEvent({ item, index })}> {item.title} ) }) } ) } export default memo(Tabs)