-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add TabList component * Use TabList component in test panel
- Loading branch information
1 parent
acdf7ba
commit 0635b9e
Showing
2 changed files
with
75 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
CSSProperties, | ||
Children, | ||
PropsWithChildren, | ||
ReactNode, | ||
cloneElement, | ||
useId, | ||
useState, | ||
} from "react"; | ||
|
||
export const Tab = ( | ||
props: PropsWithChildren<{ | ||
title: ReactNode; | ||
style?: CSSProperties; | ||
parent?: string; | ||
checked?: boolean; | ||
onSelect?: () => void; | ||
}> | ||
) => { | ||
const id = useId(); | ||
const tab = `tab-${id}`; | ||
const panel = `panel-${id}`; | ||
return ( | ||
<> | ||
<div role="tab" id={tab} aria-controls={panel} style={props.style}> | ||
<label> | ||
{props.title} | ||
<input | ||
type="radio" | ||
name={props.parent} | ||
aria-controls={panel} | ||
value={tab} | ||
checked={props.checked} | ||
onChange={(e) => e.target.checked == true && props.onSelect?.()} | ||
/> | ||
</label> | ||
</div> | ||
<div role="tabpanel" id={panel} aria-labelledby={tab}> | ||
{props.children} | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export const TabList = (props: { children: ReturnType<typeof Tab>[] }) => { | ||
const id = useId(); | ||
const [selectedIndex, setSelectedIndex] = useState(0); | ||
return ( | ||
<section | ||
role="tablist" | ||
style={{ "--tab-count": props.children.length } as React.CSSProperties} | ||
> | ||
{Children.map(props.children, (child, index) => | ||
cloneElement(child, { | ||
checked: index === selectedIndex, | ||
parent: id, | ||
idx: index, | ||
onSelect: () => { | ||
setSelectedIndex(index); | ||
child.props?.onSelect?.(); | ||
}, | ||
}) | ||
)} | ||
</section> | ||
); | ||
}; |
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