Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,48 @@ describe('Rendering', () => {
expect(getComboboxInput()).not.toBe(null)
})

it('should preserve refs on immediate children', () => {
let childRef = React.createRef<HTMLDivElement>()

render(
<Combobox value="a" onChange={NOOP}>
<Combobox.Input onChange={NOOP} />
<Combobox.Button>Open</Combobox.Button>
<Combobox.Options static modal={false}>
<div ref={childRef} data-example-child>
<Combobox.Option value="a">A</Combobox.Option>
</div>
</Combobox.Options>
</Combobox>
)

let child = document.querySelector('[data-example-child]')
expect(child).not.toBeNull()
expect(childRef.current).toBe(child)
})

it('should merge the Options ref with refs on immediate children', () => {
let optionsRef = React.createRef<HTMLElement>()
let childRef = React.createRef<HTMLDivElement>()

render(
<Combobox value="a" onChange={NOOP}>
<Combobox.Input onChange={NOOP} />
<Combobox.Button>Open</Combobox.Button>
<Combobox.Options static modal={false} as={Fragment} ref={optionsRef}>
<div ref={childRef} data-example-child>
<Combobox.Option value="a">A</Combobox.Option>
</div>
</Combobox.Options>
</Combobox>
)

let child = document.querySelector('[data-example-child]')
expect(child).not.toBeNull()
expect(childRef.current).toBe(child)
expect(optionsRef.current).toBe(childRef.current)
})

it('should be possible to use a different render strategy for the Combobox.Options', async () => {
render(
<Combobox value="test" onChange={(x) => console.log(x)}>
Expand Down
8 changes: 6 additions & 2 deletions packages/@headlessui-react/src/internal/frozen.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React, { cloneElement, isValidElement, useState } from 'react'
import { useSyncRefs } from '../hooks/use-sync-refs'
import { getElementRef } from '../utils/render'

function FrozenFn(
{ children, freeze }: { children: React.ReactNode; freeze: boolean },
ref: React.ForwardedRef<HTMLElement>
) {
let contents = useFrozenData(freeze, children)
let element = isValidElement(contents) ? (contents as React.ReactElement) : null
let mergedRef = useSyncRefs(element ? getElementRef(element) : null, ref)

if (isValidElement(contents)) {
return cloneElement(contents as React.ReactElement, { ref })
if (element) {
return mergedRef ? cloneElement(element, { ref: mergedRef }) : element
}

return <>{contents}</>
Expand Down
2 changes: 1 addition & 1 deletion packages/@headlessui-react/src/utils/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ function omit<T extends Record<any, any>>(object: T, keysToOmit: string[] = [])
return clone
}

function getElementRef(element: React.ReactElement) {
export function getElementRef(element: React.ReactElement) {
// @ts-expect-error
return React.version.split('.')[0] >= '19' ? element.props.ref : element.ref
}
Expand Down