Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

fix: support radix-ui/tabs root props #104

Merged
merged 2 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/modern-seals-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@greenlabs/formula-components": patch
"@greenlabs/rescript-formula-components": patch
---

fix: support radix-ui/tabs defaultValue and etcs

- fixes #103
2 changes: 1 addition & 1 deletion packages/components-rescript/__tests__/Formula_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ let testTextField = () => {

let textTextTab = () => {
<TextTab.List
rootProps={{"defaultValue": "a"}}
defaultValue="a"
fullWidth={true}
onValueChange={_ => ()}
contents={<>
Expand Down
3 changes: 3 additions & 0 deletions packages/components-rescript/src/Formula__TextTab.res
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module List = {
external make: (
~props: {..}=?,
~rootProps: {..}=?, // TODO: RadixUI props
~dir: [#ltr | #rtl]=?,
~value: string=?,
~defaultValue: string=?,
~contents: React.element=?,
~fullWidth: bool=?,
~onValueChange: string => unit=?,
Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/Tab/TextTab/TextTab.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ const Template = (args) => {
},
}}
>
defaultValue: b
<Tab.List
rootProps={{ defaultValue: "a" }}
defaultValue="b"
fullWidth
onValueChange={(_) => {}}
contents={
Expand Down
27 changes: 22 additions & 5 deletions packages/components/src/Tab/TextTab/TextTab.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PropsWithChildren, ReactNode } from "react"
import {
forwardRef,
useLayoutEffect,
useEffect,
useRef,
useState,
useImperativeHandle,
Expand All @@ -27,22 +27,36 @@ const extractIndicatorState = (tabEl: TriggerHTMLType) => {
interface ListProps {
contents?: ReactNode
fullWidth?: boolean
rootProps?: TabsProps
defaultValue?: string
value?: string
dir?: "ltr" | "rtl"
rootProps?: Omit<TabsProps, "defaultValue" | "value" | "dir">
onValueChange?: (value: string) => void
}
// todo: on resize
export const List = forwardRef<HTMLDivElement, PropsWithChildren<ListProps>>(
(
{ children, contents = null, onValueChange, fullWidth, rootProps },
{
children,
contents = null,
onValueChange,
fullWidth,
rootProps,
defaultValue,
value,
dir,
},
forwardedRef
) => {
const ref = useRef<HTMLDivElement>(null)
const [state, setState] = useState({ left: 0, width: 0 })

useImperativeHandle(forwardedRef, () => ref.current as HTMLDivElement)

useLayoutEffect(() => {
const tabEl = ref.current?.querySelector("button")
useEffect(() => {
const tabEl = ref.current?.querySelector(
'[data-state="active"]'
) as HTMLButtonElement | null
if (tabEl) {
setState(extractIndicatorState(tabEl))
}
Expand All @@ -64,6 +78,9 @@ export const List = forwardRef<HTMLDivElement, PropsWithChildren<ListProps>>(
<Root
className={rootStyle}
onValueChange={onValueChangeWrapped}
defaultValue={defaultValue}
value={value}
dir={dir}
{...rootProps}
>
<div className={listContainerStyle}>
Expand Down