Skip to content

Commit

Permalink
fix(SelectPicker): prevent error when listProps.itemSize is a number (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenOutman committed May 15, 2023
1 parent abc2bfe commit 151df47
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/SelectPicker/test/SelectPickerSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,19 @@ describe('SelectPicker', () => {
expect(instance.list).to.exist;
});
});

describe('Troubleshooting', () => {
it('Should not throw when `listProps.itemSize` is a number', () => {
expect(() => {
render(
<SelectPicker
data={[{ label: 'Master', value: 'Master' }]}
virtualized
listProps={{ itemSize: 66 }}
open
/>
);
}).to.not.throw();
});
});
});
12 changes: 9 additions & 3 deletions src/Windowing/List.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useImperativeHandle, useCallback } from 'react';
import React, { useRef, useImperativeHandle, useCallback, useMemo } from 'react';
import {
VariableSizeList,
Align,
Expand Down Expand Up @@ -56,7 +56,7 @@ export interface ListHandle extends Partial<VariableSizeList> {
}

const List: RsRefForwardingComponent<'div', ListProps> = React.forwardRef((props, ref) => {
const { rowHeight, as: Component = VariableSizeList, ...rest } = props;
const { rowHeight, as: Component = VariableSizeList, itemSize: itemSizeProp, ...rest } = props;
const listRef = useRef<VariableSizeList>(null);
const { rtl } = useCustom();

Expand All @@ -82,7 +82,13 @@ const List: RsRefForwardingComponent<'div', ListProps> = React.forwardRef((props
[rowHeight]
);

const compatibleProps = { ...rest } as any;
const itemSize = useMemo(() => {
if (typeof itemSizeProp === 'function') return itemSizeProp;

return () => itemSizeProp;
}, [itemSizeProp]);

const compatibleProps = { itemSize, ...rest } as any;

if (rowHeight) {
compatibleProps.itemSize = Component === VariableSizeList ? setRowHeight : rowHeight;
Expand Down
17 changes: 17 additions & 0 deletions src/Windowing/test/ListSpec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import { render } from '@testing-library/react';
import List from '../List';

describe('List', () => {
it('Should not throw when `itemSize` is a number', () => {
expect(() => {
render(
// FIXME-Doma `itemCount` and `height` props are not declared in List's props,
// but they're actually required by react-window List
<List itemSize={66} {...{ itemCount: 1, height: 400 }}>
{() => null}
</List>
);
}).to.not.throw();
});
});

1 comment on commit 151df47

@vercel
Copy link

@vercel vercel bot commented on 151df47 May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

rsuite-nextjs – ./docs

rsuite-nextjs-git-main-rsuite.vercel.app
rsuite.vercel.app
rsuite-nextjs-rsuite.vercel.app
rsuitejs.com

Please sign in to comment.