Repository navigation
Allow custom list wrappers to use ul elements - #1497
Conversation
Annotating a custom List or Item as Components['List'] / Components['Item'] pinned the forwarded ref to HTMLDivElement, so ul/li wrappers failed to type-check.
🦋 Changeset detectedLatest commit: d99119f The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
@cpruijsen this will, however, lose the typechecking of the ref value, right? I will be able to call non-existent methods on it. |
|
Yes, you're right, and that's a real cost. I tried the two narrower options before settling, and only one of them works:
Threading the host element through export interface Components<Data = unknown, Context = unknown, ListEl extends HTMLElement = HTMLDivElement, ItemEl extends HTMLElement = HTMLDivElement> {
Item?: React.ComponentType<ItemProps<Data, ItemEl> & ContextProp<Context>>
List?: React.ComponentType<ListProps<ListEl> & ContextProp<Context>>
}Callers then write The cost is two more type parameters on That is an API call rather than a bug fix, so I would rather you pick. Say the word and I will switch this PR to the generic version, or keep |
|
The generic approach has a backward-compatibility problem once the new host-element type parameters are threaded through This existing usage compiles with the current types but fails with the proposed generics: <Virtuoso
data={[{ id: 1 }]}
components={{
List: React.forwardRef((props, ref) => (
<div {...props} ref={ref} />
)),
}}
/>I checked this with TypeScript against the actual source declarations, applying the proposed changes in memory. Adding defaulted host-element generics changes contextual inference for the inline Changing only There is also an annotation cost for callers who specify The existing |
Components and GridComponents take ListEl/ItemEl parameters defaulting to HTMLDivElement, so a wrapper annotates its own host element and keeps the ref fully checked. Virtuoso and VirtuosoGrid accept the components object at any host type, which keeps an inline forwardRef over a div inferring as before and adds no type arguments at the call site. TableRow is typed as a table row.
|
Reproduced, same error: The problem is the parameter being inferable at the
typecheck, lint and format are clean and the 203 tests pass. The tradeoff: |
|
I think there is a backward-compatible way forward if we narrow this to the original Could you change the PR in this direction?
The const LiItem: Components<Row>['Item'] = ({
children,
context: _context,
item: _item,
...props
}) => <li {...props}>{children}</li>Please keep type coverage for:
This gives the original issue a sensible opt-in solution while preserving existing source behavior. Supporting typed refs for arbitrary |
Add a defaulted `ListEl` parameter to `Components` and `GridComponents`, constrained to `HTMLDivElement | HTMLUListElement`. `Components<Data, Context>` keeps its current behaviour; a `ul` wrapper opts in through `Components<Data, Context, HTMLUListElement>['List']` with the forwarded ref still checked. Revert the `ItemProps` change: `Item` is untouched, so an item component that spreads its props onto a non-div element compiles as before. `VirtuosoProps.components` is typed `Components<Data, Context, any>` so that `ListEl` is not inferable at the `Virtuoso` boundary. An inline `forwardRef` list has nothing to infer the host element from, and would otherwise collapse to `RefAttributes<null>`. No runtime changes.
|
Narrowed as you asked, pushed as d99119f.
One caveat you should decide on, because it is the part I am least comfortable with. The cost is narrow but real. At that one call site, an inline const BogusRefList = React.forwardRef<{ nonsense: true }, React.ComponentProps<NonNullable<Components['List']>>>(
function BogusRefList({ style, children }) {
return <div style={style}>{children}</div>
}
)
<Virtuoso data={data} components={{ List: BogusRefList }} />That compiles on this branch. Assigning I tried the obvious non- If you would rather not have
typecheck, lint and format are clean on |
Why
A custom
Listwrapper could render aulat runtime, butComponents['List']fixed its forwarded ref toHTMLDivElement. Correctly typedulwrappers therefore required a cast.What changes
ComponentsandGridComponentsnow accept an optional list-element type restricted toHTMLDivElementorHTMLUListElement. The default remainsHTMLDivElement, while aulwrapper can opt in withComponents<Data, Context, HTMLUListElement>['List']. Its forwarded ref remains typed asHTMLUListElement.Existing
ItemandItemPropstypes are unchanged. This is a type-only change with no runtime behavior change.Compatibility
Existing
divwrappers, inlineforwardRefwrappers, explicit<Virtuoso<Data, Context>>usage, and non-div item wrappers continue to compile. At theVirtuosocomponents-prop boundary, the list host type is intentionally not inferred so existing inline wrappers keep their current inference behavior. Separately typed list wrappers retain strict ref checking.Fixes #864.