diff --git a/markdown/sections/usage/custom-component.md b/markdown/sections/usage/custom-component.md index bd1c697..2c78831 100644 --- a/markdown/sections/usage/custom-component.md +++ b/markdown/sections/usage/custom-component.md @@ -32,31 +32,31 @@ export const markdownComponents: MarkdownToJSX.Overrides = { ### Tab - - ```tsx filename="app/server-action/layout.tsx" - import type { PropsWithChildren } from 'react' - - export default async ({ children }: PropsWithChildren) => { - return ( - - Layout Render At: {Date.now()} - {children} - - ) - } - ``` - - - ```tsx filename="app/server-action/action.tsx" - 'use server' - - import { revalidatePath } from 'next/cache' - - export const actionRevalidate = async () => { - revalidatePath('/server-action') - } - ``` - + + ```tsx filename="app/server-action/layout.tsx" + import type { PropsWithChildren } from 'react' + + export default async ({ children }: PropsWithChildren) => { + return ( + + Layout Render At: {Date.now()} + {children} + + ) + } + ``` + + + ```tsx filename="app/server-action/action.tsx" + 'use server' + + import { revalidatePath } from 'next/cache' + + export const actionRevalidate = async () => { + revalidatePath('/server-action') + } + ``` + 代码为: diff --git a/src/components/tabs/Tab.tsx b/src/components/tabs/Tab.tsx index 903b6b4..c0aa391 100644 --- a/src/components/tabs/Tab.tsx +++ b/src/components/tabs/Tab.tsx @@ -1,68 +1,62 @@ 'use client' import * as RadixTabs from '@radix-ui/react-tabs' -import { - createContext, - useCallback, - useContext, - useEffect, - useId, - useState, -} from 'react' +import React, { useId, useMemo, useState } from 'react' import { m } from 'framer-motion' import type { FC, PropsWithChildren } from 'react' import { clsxm } from '~/lib/helper' -const TabActionContext = createContext<{ - addTab: (label: string) => void -}>(null!) export const Tabs: FC = ({ children }) => { - const [tabs, setTabs] = useState([]) - const [activeTab, setActiveTab] = useState(null) const id = useId() - return ( - { - setTabs((tabs) => [...tabs, label]) - if (!activeTab) setActiveTab(label) - return () => { - setTabs((tabs) => tabs.filter((tab) => tab !== label)) - } - }, - [activeTab], - ), - }} - > - - - {tabs.map((tab) => { - return ( - - {tab} + const tabs = useMemo(() => { + const labels = [] as string[] + for (const child of React.Children.toArray(children)) { + if (!child) { + continue + } + if (typeof child !== 'object') continue + if (!('props' in child)) continue + if (!('type' in child)) continue + + // if (child.type !== Tab) continue + const label = child.props.label + labels.push(label) + } + return labels + }, [children]) - {activeTab === tab && ( - - )} - - ) - })} - + const [activeTab, setActiveTab] = useState(tabs[0]) + return ( + + + {tabs.map((tab) => { + return ( + + {tab} - {children} - - + {activeTab === tab && ( + + )} + + ) + })} + + + {children} + ) } @@ -70,10 +64,5 @@ export const Tab: FC<{ label: string children: React.ReactNode }> = ({ label, children }) => { - const { addTab } = useContext(TabActionContext) - useEffect(() => { - return addTab(label) - }, []) - return {children} }