Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove forceRerender from Tab component #1846

Merged
merged 2 commits into from
Sep 12, 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
1 change: 1 addition & 0 deletions packages/@headlessui-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `<fieldset disabled>` check to radio group options in React ([#1835](https://github.com/tailwindlabs/headlessui/pull/1835))
- Ensure `Tab` order stays consistent, and the currently active `Tab` stays active ([#1837](https://github.com/tailwindlabs/headlessui/pull/1837))
- Ensure `Combobox.Label` is properly linked when rendered after `Combobox.Button` and `Combobox.Input` components ([#1838](https://github.com/tailwindlabs/headlessui/pull/1838))
- Remove `forceRerender` from `Tab` component ([#1846](https://github.com/tailwindlabs/headlessui/pull/1846))

## [1.7.0] - 2022-09-06

Expand Down
50 changes: 50 additions & 0 deletions packages/@headlessui-react/src/components/tabs/tabs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,56 @@ describe('Rendering', () => {
)

describe('`renderProps`', () => {
it(
'should be possible to render using as={Fragment}',
suppressConsoleLogs(async () => {
render(
<Tab.Group>
<Tab.List>
<Tab as={React.Fragment}>
<button>Tab 1</button>
</Tab>
<Tab>Tab 2</Tab>
<Tab>Tab 3</Tab>
</Tab.List>

<Tab.Panels>
<Tab.Panel>Content 1</Tab.Panel>
<Tab.Panel>Content 2</Tab.Panel>
<Tab.Panel>Content 3</Tab.Panel>
</Tab.Panels>
</Tab.Group>
)

assertTabs({ active: 0, tabContents: 'Tab 1', panelContents: 'Content 1' })
})
)

it(
'should be possible to render using multiple as={Fragment}',
suppressConsoleLogs(async () => {
render(
<Tab.Group>
<Tab.List>
<Tab as={React.Fragment}>
<button>Tab 1</button>
</Tab>
<Tab as={React.Fragment}>
<button>Tab 2</button>
</Tab>
</Tab.List>

<Tab.Panels>
<Tab.Panel>Content 1</Tab.Panel>
<Tab.Panel>Content 2</Tab.Panel>
</Tab.Panels>
</Tab.Group>
)

assertTabs({ active: 0, tabContents: 'Tab 1', panelContents: 'Content 1' })
})
)

it(
'should expose the `selectedIndex` on the `Tab.Group` component',
suppressConsoleLogs(async () => {
Expand Down
20 changes: 2 additions & 18 deletions packages/@headlessui-react/src/components/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ enum ActionTypes {

RegisterPanel,
UnregisterPanel,

ForceRerender,
}

type Actions =
Expand All @@ -54,7 +52,6 @@ type Actions =
| { type: ActionTypes.UnregisterTab; tab: MutableRefObject<HTMLElement | null> }
| { type: ActionTypes.RegisterPanel; panel: MutableRefObject<HTMLElement | null> }
| { type: ActionTypes.UnregisterPanel; panel: MutableRefObject<HTMLElement | null> }
| { type: ActionTypes.ForceRerender }

let reducers: {
[P in ActionTypes]: (
Expand Down Expand Up @@ -110,9 +107,6 @@ let reducers: {
[ActionTypes.UnregisterPanel](state, action) {
return { ...state, panels: state.panels.filter((panel) => panel !== action.panel) }
},
[ActionTypes.ForceRerender](state) {
return { ...state }
},
}

let TabsSSRContext = createContext<MutableRefObject<{ tabs: string[]; panels: string[] }> | null>(
Expand Down Expand Up @@ -154,7 +148,6 @@ let TabsActionsContext = createContext<{
registerTab(tab: MutableRefObject<HTMLElement | null>): () => void
registerPanel(panel: MutableRefObject<HTMLElement | null>): () => void
change(index: number): void
forceRerender(): void
} | null>(null)
TabsActionsContext.displayName = 'TabsActionsContext'

Expand Down Expand Up @@ -229,9 +222,6 @@ let Tabs = forwardRefWithAs(function Tabs<TTag extends ElementType = typeof DEFA
dispatch({ type: ActionTypes.RegisterPanel, panel })
return () => dispatch({ type: ActionTypes.UnregisterPanel, panel })
},
forceRerender() {
dispatch({ type: ActionTypes.ForceRerender })
},
change(index: number) {
if (realSelectedIndex.current !== index) {
onChangeRef.current(index)
Expand Down Expand Up @@ -335,10 +325,7 @@ let TabRoot = forwardRefWithAs(function Tab<TTag extends ElementType = typeof DE
let SSRContext = useSSRTabsCounter('Tab')

let internalTabRef = useRef<HTMLElement | null>(null)
let tabRef = useSyncRefs(internalTabRef, ref, (element) => {
if (!element) return
requestAnimationFrame(() => actions.forceRerender())
})
let tabRef = useSyncRefs(internalTabRef, ref)

useIsoMorphicEffect(() => actions.registerTab(internalTabRef), [actions, internalTabRef])

Expand Down Expand Up @@ -492,10 +479,7 @@ let Panel = forwardRefWithAs(function Panel<TTag extends ElementType = typeof DE

let id = `headlessui-tabs-panel-${useId()}`
let internalPanelRef = useRef<HTMLElement | null>(null)
let panelRef = useSyncRefs(internalPanelRef, ref, (element) => {
if (!element) return
requestAnimationFrame(() => actions.forceRerender())
})
let panelRef = useSyncRefs(internalPanelRef, ref)

useIsoMorphicEffect(() => actions.registerPanel(internalPanelRef), [actions, internalPanelRef])

Expand Down