Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(Tabs): support v4 * remove comment
- Loading branch information
Showing
7 changed files
with
288 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React, { forwardRef, ReactNode } from 'react' | ||
import clsx from 'clsx' | ||
import { twMerge } from 'tailwind-merge' | ||
|
||
export type RadioTabProps = Omit< | ||
React.InputHTMLAttributes<HTMLInputElement>, | ||
'type' | ||
> & { | ||
active?: boolean | ||
disabled?: boolean | ||
label: string | ||
name: string | ||
contentClassName?: string | ||
} | ||
|
||
const RadioTab = forwardRef<HTMLInputElement, RadioTabProps>( | ||
( | ||
{ | ||
children, | ||
className, | ||
active, | ||
label, | ||
disabled, | ||
name, | ||
contentClassName, | ||
...props | ||
}, | ||
ref | ||
): JSX.Element => { | ||
const classes = twMerge( | ||
'tab', | ||
className, | ||
clsx({ | ||
'tab-active': active, | ||
'tab-disabled': disabled, | ||
}) | ||
) | ||
const contentClasses = twMerge('tab-content', contentClassName) | ||
|
||
return ( | ||
<> | ||
<input | ||
className={classes} | ||
role="tab" | ||
type="radio" | ||
name={name} | ||
disabled={disabled} | ||
aria-label={label} | ||
{...props} | ||
ref={ref} | ||
/> | ||
<div className={contentClasses}>{children}</div> | ||
</> | ||
) | ||
} | ||
) | ||
|
||
export default RadioTab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,28 @@ | ||
import React from 'react' | ||
import React, { forwardRef } from 'react' | ||
import clsx from 'clsx' | ||
import { twMerge } from 'tailwind-merge' | ||
|
||
import { ComponentSize } from '../types' | ||
|
||
export type TabProps<T> = Omit< | ||
React.AnchorHTMLAttributes<HTMLAnchorElement>, | ||
'onClick' | ||
> & { | ||
value: T | ||
activeValue?: T | ||
onClick?: (value: T) => void | ||
size?: ComponentSize | ||
variant?: 'boxed' | 'bordered' | 'lifted' | ||
export type TabProps = React.AnchorHTMLAttributes<HTMLAnchorElement> & { | ||
active?: boolean | ||
disabled?: boolean | ||
} | ||
|
||
const TabInner = <T extends string | number | undefined>( | ||
{ | ||
children, | ||
value, | ||
activeValue, | ||
onClick, | ||
size, | ||
variant, | ||
disabled, | ||
className, | ||
style, | ||
...props | ||
}: TabProps<T>, | ||
ref?: React.ForwardedRef<T> | ||
): JSX.Element => { | ||
const classes = twMerge( | ||
'tab', | ||
className, | ||
clsx({ | ||
'tab-active': value != null && value === activeValue, | ||
'tab-disabled': disabled, | ||
'tab-lg': size === 'lg', | ||
'tab-md': size === 'md', | ||
'tab-sm': size === 'sm', | ||
'tab-xs': size === 'xs', | ||
'tab-bordered': variant === 'bordered', | ||
'tab-lifted': variant === 'lifted', | ||
}) | ||
) | ||
|
||
return ( | ||
<a | ||
role="tab" | ||
{...props} | ||
className={classes} | ||
style={style} | ||
onClick={() => onClick && onClick(value)} | ||
> | ||
{children} | ||
</a> | ||
) | ||
} | ||
|
||
// Make forwardRef work with generic component | ||
const Tab = React.forwardRef(TabInner) as <T>( | ||
props: TabProps<T> & { ref?: React.ForwardedRef<HTMLAnchorElement> } | ||
) => ReturnType<typeof TabInner> | ||
const Tab = forwardRef<HTMLAnchorElement, TabProps>( | ||
({ children, className, active, disabled, ...props }, ref): JSX.Element => { | ||
const classes = twMerge( | ||
'tab', | ||
className, | ||
clsx({ | ||
'tab-active': active, | ||
'tab-disabled': disabled, | ||
}) | ||
) | ||
return ( | ||
<a role="tab" {...props} ref={ref} className={classes}> | ||
{children} | ||
</a> | ||
) | ||
} | ||
) | ||
|
||
export default Tab |
Oops, something went wrong.