Skip to content

Commit

Permalink
correctly handle TypeScript render abstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Feb 11, 2021
1 parent d557d50 commit bb68793
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ function Label<TTag extends ElementType = typeof DEFAULT_LABEL_TAG>(
state.buttonRef,
])

let propsBag = useMemo<OptionsRenderPropArg>(
let propsBag = useMemo<LabelRenderPropArg>(
() => ({ open: state.listboxState === ListboxStates.Open, disabled: state.disabled }),
[state]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import React, {
ElementType,
MutableRefObject,
} from 'react'
import { Props, Expand } from '../../types'
import { Props } from '../../types'

import { useId } from '../../hooks/use-id'
import { useIsInitialRender } from '../../hooks/use-is-initial-render'
Expand Down Expand Up @@ -201,8 +201,10 @@ function TransitionChild<TTag extends ElementType = typeof DEFAULT_TRANSITION_CH
leave,
leaveFrom,
leaveTo,

// @ts-expect-error
...rest
} = props as Expand<typeof props>
} = props as typeof props
let container = useRef<HTMLElement | null>(null)
let [state, setState] = useState(TreeStates.Visible)
let strategy = rest.unmount ? RenderStrategy.Unmount : RenderStrategy.Hidden
Expand Down Expand Up @@ -331,7 +333,8 @@ function TransitionChild<TTag extends ElementType = typeof DEFAULT_TRANSITION_CH
export function Transition<TTag extends ElementType = typeof DEFAULT_TRANSITION_CHILD_TAG>(
props: TransitionChildProps<TTag> & { show: boolean; appear?: boolean }
) {
let { show, appear = false, unmount, ...passthroughProps } = props as Expand<typeof props>
// @ts-expect-error
let { show, appear = false, unmount, ...passthroughProps } = props as typeof props

if (![true, false].includes(show)) {
throw new Error('A <Transition /> is used but it is missing a `show={true | false}` prop.')
Expand Down
13 changes: 10 additions & 3 deletions packages/@headlessui-react/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ReactNode, ReactElement } from 'react'
// A unique placeholder we can use as some defaults. This is nice because we can use this instead of
// defaulting to null / never / ... and possibly collide with actual data.
const __: unique symbol = Symbol('__placeholder__')
Expand All @@ -9,11 +10,17 @@ export type PropsOf<TTag = any> = TTag extends React.ElementType
? React.ComponentProps<TTag>
: never

export type Props<TTag, TSlot = {}, TOmitableProps extends keyof any = __> = {
export type Props<
TTag,
TSlot = any,
TOmitableProps extends keyof any = __
> = (TOmitableProps extends __
? Omit<PropsOf<TTag>, 'as' | 'children' | 'refName'>
: Omit<PropsOf<TTag>, TOmitableProps | 'as' | 'children' | 'refName'>) & {
as?: TTag
children?: React.ReactNode | ((bag: TSlot) => React.ReactElement)
children?: ReactNode | ((bag: TSlot) => ReactElement)
refName?: string
} & (TOmitableProps extends __ ? PropsOf<TTag> : Omit<PropsOf<TTag>, TOmitableProps>)
}

type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never }
export type XOR<T, U> = T | U extends __
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 @@ -88,7 +88,7 @@ export function render<TFeature extends Features, TTag extends ElementType, TBag
}

function _render<TTag extends ElementType, TBag>(
props: Expand<Props<TTag, TBag> & { ref?: unknown }>,
props: Props<TTag, TBag> & { ref?: unknown },
bag: TBag,
tag: ElementType
) {
Expand Down

0 comments on commit bb68793

Please sign in to comment.