|
| 1 | +import React, { ReactNode, ReactElement, Children } from 'react'; |
| 2 | +import { useTabList, useTab, useTabPanel } from '@react-aria/tabs'; |
| 3 | +import { TabListState, useTabListState } from '@react-stately/tabs'; |
| 4 | +import { |
| 5 | + DOMRef, |
| 6 | + ItemProps, |
| 7 | + Node, |
| 8 | + Orientation, |
| 9 | + CollectionElement, |
| 10 | +} from '@react-types/shared'; |
| 11 | +import { Item as BaseItem } from '@react-stately/collections'; |
| 12 | +import { useHover } from '@react-aria/interactions'; |
| 13 | +import { AriaTabListProps, TabListProps } from '@react-types/tabs'; |
| 14 | +import { useDOMRef } from '@react-spectrum/utils'; |
| 15 | +import { useButton } from '@react-aria/button'; |
| 16 | +import { AriaButtonProps } from '@react-types/button'; |
| 17 | + |
| 18 | +import { mergeProps } from '../../../utils/react'; |
| 19 | +import { useFocus } from '../../../utils/react/interactions'; |
| 20 | + |
| 21 | +import { |
| 22 | + StyledTabsContainer, |
| 23 | + StyledTabPanes, |
| 24 | + StyledTabItem, |
| 25 | + StyledTabBody, |
| 26 | + // ACTION_BUTTON, |
| 27 | +} from './styled'; |
| 28 | + |
| 29 | +type CubeTabButtonProps = { |
| 30 | + icon?: ReactElement; |
| 31 | + isDisabled?: boolean; |
| 32 | + children?: ReactNode; |
| 33 | +} & AriaButtonProps<'button'>; |
| 34 | + |
| 35 | +function TabButton(props: CubeTabButtonProps) { |
| 36 | + const { isDisabled, icon } = props; |
| 37 | + const ref = React.useRef(null); |
| 38 | + const { hoverProps, isHovered } = useHover({ isDisabled }); |
| 39 | + const { focusProps, isFocused } = useFocus({ isDisabled }, true); |
| 40 | + |
| 41 | + const { buttonProps, isPressed } = useButton(props, ref); |
| 42 | + |
| 43 | + const mods = { |
| 44 | + hovered: isHovered, |
| 45 | + focused: isFocused, |
| 46 | + pressed: isPressed, |
| 47 | + }; |
| 48 | + |
| 49 | + return ( |
| 50 | + <StyledTabItem |
| 51 | + {...mergeProps(buttonProps, hoverProps, focusProps, props)} |
| 52 | + ref={ref} |
| 53 | + as="button" |
| 54 | + mods={mods} |
| 55 | + > |
| 56 | + {icon} |
| 57 | + </StyledTabItem> |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +type CubeTabProps<T> = { |
| 62 | + item: Node<T>; |
| 63 | + state: TabListState<T>; |
| 64 | + orientation?: Orientation; |
| 65 | +}; |
| 66 | + |
| 67 | +function Tab<T extends object>({ item, state, orientation }: CubeTabProps<T>) { |
| 68 | + const { key, rendered, props: itemProps } = item; |
| 69 | + const ref = React.useRef(null); |
| 70 | + const { tabProps, isSelected, isDisabled } = useTab({ key }, state, ref); |
| 71 | + const { hoverProps, isHovered } = useHover({ isDisabled }); |
| 72 | + const { focusProps, isFocused } = useFocus({ isDisabled }, true); |
| 73 | + |
| 74 | + const icon = itemProps.icon; |
| 75 | + |
| 76 | + const mods = { |
| 77 | + ...itemProps.mods, |
| 78 | + selected: isSelected, |
| 79 | + disabled: isDisabled, |
| 80 | + hovered: isHovered, |
| 81 | + focused: isFocused, |
| 82 | + horizontal: orientation === 'horizontal', |
| 83 | + vertical: orientation === 'vertical', |
| 84 | + }; |
| 85 | + |
| 86 | + return ( |
| 87 | + <StyledTabItem |
| 88 | + {...mergeProps(tabProps, hoverProps, focusProps, itemProps)} |
| 89 | + ref={ref} |
| 90 | + mods={mods} |
| 91 | + > |
| 92 | + {icon} |
| 93 | + {rendered} |
| 94 | + </StyledTabItem> |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +type CubeTabPanelProps<T> = { |
| 99 | + state: TabListState<T>; |
| 100 | +}; |
| 101 | + |
| 102 | +function TabPanel<T>({ state, ...props }: CubeTabPanelProps<T>) { |
| 103 | + const ref = React.useRef<Element>(null); |
| 104 | + const { tabPanelProps } = useTabPanel(props, state, ref); |
| 105 | + return ( |
| 106 | + <StyledTabBody {...tabPanelProps} ref={ref}> |
| 107 | + {state.selectedItem?.props.children} |
| 108 | + </StyledTabBody> |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +export type CubeTabsProps<T> = TabListProps<T> & AriaTabListProps<T>; |
| 113 | + |
| 114 | +function Tabs<T extends object>( |
| 115 | + props: CubeTabsProps<T>, |
| 116 | + ref: DOMRef<HTMLDivElement>, |
| 117 | +) { |
| 118 | + const domRef = useDOMRef(ref); |
| 119 | + const children = Children.toArray(props.children).filter( |
| 120 | + (el) => (el as ReactElement)?.props?.children, |
| 121 | + ); |
| 122 | + const tabButtons = Children.toArray(props.children).filter( |
| 123 | + (el) => !(el as ReactElement)?.props?.children, |
| 124 | + ) as ReactElement[]; |
| 125 | + const state = useTabListState({ |
| 126 | + ...props, |
| 127 | + children: children as CollectionElement<T>[], |
| 128 | + }); |
| 129 | + const { tabListProps } = useTabList( |
| 130 | + { |
| 131 | + ...props, |
| 132 | + children: children as CollectionElement<T>[], |
| 133 | + }, |
| 134 | + state, |
| 135 | + domRef, |
| 136 | + ); |
| 137 | + |
| 138 | + return ( |
| 139 | + <StyledTabsContainer> |
| 140 | + <StyledTabPanes {...tabListProps} ref={domRef}> |
| 141 | + {[...state.collection].map((item) => ( |
| 142 | + <Tab |
| 143 | + key={item.key} |
| 144 | + item={item} |
| 145 | + state={state} |
| 146 | + orientation={props.orientation} |
| 147 | + /> |
| 148 | + ))} |
| 149 | + {tabButtons.map((el) => ( |
| 150 | + <TabButton {...el.props} key={el.key}></TabButton> |
| 151 | + ))} |
| 152 | + </StyledTabPanes> |
| 153 | + <TabPanel key={state.selectedItem?.key} state={state} /> |
| 154 | + </StyledTabsContainer> |
| 155 | + ); |
| 156 | +} |
| 157 | + |
| 158 | +// forwardRef doesn't support generic parameters, so cast the result to the correct type |
| 159 | +// https://stackoverflow.com/questions/58469229/react-with-typescript-generics-while-using-react-forwardref |
| 160 | +const _Tabs = React.forwardRef(Tabs) as <T extends object>( |
| 161 | + props: CubeTabsProps<T> & { ref?: DOMRef<HTMLDivElement> }, |
| 162 | +) => ReactElement; |
| 163 | + |
| 164 | +type ItemComponent = <T>( |
| 165 | + props: Omit<ItemProps<T>, 'children'> & CubeTabButtonProps, |
| 166 | +) => JSX.Element; |
| 167 | + |
| 168 | +const Item = Object.assign(BaseItem, { |
| 169 | + displayName: 'Item', |
| 170 | +}) as ItemComponent; |
| 171 | + |
| 172 | +type __TabsComponent = typeof _Tabs & { |
| 173 | + Item: typeof Item; |
| 174 | +}; |
| 175 | + |
| 176 | +const __Tabs = Object.assign(_Tabs as __TabsComponent, { |
| 177 | + Item, |
| 178 | + displayName: 'Tabs', |
| 179 | +}); |
| 180 | + |
| 181 | +__Tabs.displayName = 'Tabs'; |
| 182 | + |
| 183 | +export { __Tabs as Tabs }; |
0 commit comments