|
| 1 | +import React, { memo, forwardRef, useId, useState, useEffect } from "react"; |
| 2 | +import type { ReactNode } from "react"; |
| 3 | +import type { FrIconClassName, RiIconClassName } from "./lib/generatedFromCss/classNames"; |
| 4 | +import { symToStr } from "tsafe/symToStr"; |
| 5 | +import { fr } from "./lib"; |
| 6 | +import { cx } from "./lib/tools/cx"; |
| 7 | +import { assert } from "tsafe/assert"; |
| 8 | +import type { Equals } from "tsafe"; |
| 9 | +import { useCallbackFactory } from "./lib/tools/powerhooks/useCallbackFactory"; |
| 10 | +import "@gouvfr/dsfr/dist/component/tab/tab.css"; |
| 11 | + |
| 12 | +export type TabsProps = TabsProps.Uncontrolled | TabsProps.Controlled; |
| 13 | + |
| 14 | +export namespace TabsProps { |
| 15 | + export type Common = { |
| 16 | + className?: string; |
| 17 | + label?: string; |
| 18 | + classes?: Partial<Record<"root" | "tab" | "panel", string>>; |
| 19 | + }; |
| 20 | + |
| 21 | + export type Uncontrolled = Common & { |
| 22 | + tabs: { |
| 23 | + label: ReactNode; |
| 24 | + iconId?: FrIconClassName | RiIconClassName; |
| 25 | + content: ReactNode; |
| 26 | + }[]; |
| 27 | + selectedTabId?: undefined; |
| 28 | + onTabChange?: (params: { tabIndex: number; tab: Uncontrolled["tabs"][number] }) => void; |
| 29 | + children?: undefined; |
| 30 | + }; |
| 31 | + |
| 32 | + export type Controlled = Common & { |
| 33 | + tabs: { |
| 34 | + tabId: string; |
| 35 | + label: ReactNode; |
| 36 | + iconId?: FrIconClassName | RiIconClassName; |
| 37 | + }[]; |
| 38 | + selectedTabId: string; |
| 39 | + onTabChange: (tabId: string) => void; |
| 40 | + children?: NonNullable<ReactNode>; |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +/** @see <https://www.systeme-de-design.gouv.fr/elements-d-interface/composants/onglets> */ |
| 45 | +export const Tabs = memo( |
| 46 | + forwardRef<HTMLDivElement, TabsProps>((props, ref) => { |
| 47 | + const { |
| 48 | + className, |
| 49 | + label, |
| 50 | + classes = {}, |
| 51 | + tabs, |
| 52 | + selectedTabId, |
| 53 | + onTabChange, |
| 54 | + children, |
| 55 | + ...rest |
| 56 | + } = props; |
| 57 | + |
| 58 | + assert<Equals<keyof typeof rest, never>>(); |
| 59 | + |
| 60 | + const id = useId(); |
| 61 | + |
| 62 | + const getSelectedTabIndex = () => { |
| 63 | + assert(selectedTabId !== undefined); |
| 64 | + return tabs.findIndex(({ tabId }) => tabId === selectedTabId); |
| 65 | + }; |
| 66 | + |
| 67 | + const [selectedTabIndex, setSelectedTabIndex] = useState<number>( |
| 68 | + selectedTabId !== undefined ? getSelectedTabIndex : 0 |
| 69 | + ); |
| 70 | + |
| 71 | + useEffect(() => { |
| 72 | + if (selectedTabId === undefined) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + setSelectedTabIndex(getSelectedTabIndex()); |
| 77 | + }, [selectedTabId]); |
| 78 | + |
| 79 | + const onTabClickFactory = useCallbackFactory(([tabIndex]: [number]) => { |
| 80 | + if (selectedTabId === undefined) { |
| 81 | + onTabChange?.({ |
| 82 | + tabIndex, |
| 83 | + "tab": tabs[tabIndex] |
| 84 | + }); |
| 85 | + } else { |
| 86 | + onTabChange(tabs[tabIndex].tabId); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + const getPanelId = (tabIndex: number) => `tabpanel-${id}-${tabIndex}-panel`; |
| 91 | + const getTabId = (tabIndex: number) => `tabpanel-${id}-${tabIndex}`; |
| 92 | + |
| 93 | + return ( |
| 94 | + <div className={cx(fr.cx("fr-tabs"), className)} ref={ref} {...rest}> |
| 95 | + <ul className={fr.cx("fr-tabs__list")} role="tablist" aria-label={label}> |
| 96 | + {tabs.map(({ label, iconId }, tabIndex) => ( |
| 97 | + <li key={label + (iconId ?? "")} role="presentation"> |
| 98 | + <button |
| 99 | + id={getTabId(tabIndex)} |
| 100 | + className={cx( |
| 101 | + fr.cx("fr-tabs__tab", iconId, "fr-tabs__tab--icon-left"), |
| 102 | + classes.tab |
| 103 | + )} |
| 104 | + tabIndex={tabIndex === selectedTabIndex ? 0 : -1} |
| 105 | + role="tab" |
| 106 | + aria-selected={tabIndex === selectedTabIndex} |
| 107 | + aria-controls={getPanelId(tabIndex)} |
| 108 | + onClick={onTabClickFactory(tabIndex)} |
| 109 | + > |
| 110 | + {label} |
| 111 | + </button> |
| 112 | + </li> |
| 113 | + ))} |
| 114 | + </ul> |
| 115 | + {selectedTabId === undefined ? ( |
| 116 | + tabs.map(({ content }, tabIndex) => ( |
| 117 | + <div |
| 118 | + id={getPanelId(tabIndex)} |
| 119 | + className={cx( |
| 120 | + fr.cx( |
| 121 | + "fr-tabs__panel", |
| 122 | + `fr-tabs__panel${ |
| 123 | + tabIndex === selectedTabIndex ? "--selected" : "" |
| 124 | + }` |
| 125 | + ), |
| 126 | + classes.panel |
| 127 | + )} |
| 128 | + role="tabpanel" |
| 129 | + aria-labelledby={getTabId(tabIndex)} |
| 130 | + tabIndex={0} |
| 131 | + > |
| 132 | + {content} |
| 133 | + </div> |
| 134 | + )) |
| 135 | + ) : ( |
| 136 | + <div |
| 137 | + id={getPanelId(selectedTabIndex)} |
| 138 | + className={cx( |
| 139 | + fr.cx("fr-tabs__panel", "fr-tabs__panel--selected"), |
| 140 | + classes.panel |
| 141 | + )} |
| 142 | + role="tabpanel" |
| 143 | + aria-labelledby={getTabId(selectedTabIndex)} |
| 144 | + tabIndex={0} |
| 145 | + > |
| 146 | + {children} |
| 147 | + </div> |
| 148 | + )} |
| 149 | + </div> |
| 150 | + ); |
| 151 | + }) |
| 152 | +); |
| 153 | + |
| 154 | +Tabs.displayName = symToStr({ Tabs }); |
| 155 | + |
| 156 | +export default Tabs; |
0 commit comments