Skip to content

Commit 1760166

Browse files
authored
fix(List): calling onLoadMore function while using keyboard (#1284)
1 parent 7366c9b commit 1760166

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

src/components/List/List.tsx

+7-5
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class List<T = unknown> extends React.Component<ListProps<T>, ListState<T
9393
refFilter = React.createRef<HTMLInputElement>();
9494
refContainer = React.createRef<any>();
9595
blurTimer: ReturnType<typeof setTimeout> | null = null;
96-
loadingItem = {value: '__LIST_ITEM_LOADING__', disabled: true} as unknown as ListItemData<
96+
loadingItem = {value: '__LIST_ITEM_LOADING__', disabled: false} as unknown as ListItemData<
9797
T & {value: string}
9898
>;
9999
uniqId = getUniqId();
@@ -518,11 +518,13 @@ export class List<T = unknown> extends React.Component<ListProps<T>, ListState<T
518518
};
519519

520520
private handleKeyMove(event: React.KeyboardEvent, step: number, defaultItemIndex = 0) {
521-
event.preventDefault();
522521
const {activeItem = defaultItemIndex} = this.state;
523-
this.activateItem(
524-
List.findNextIndex<T>(this.state.items, activeItem + step, Math.sign(step)),
525-
);
522+
523+
event.preventDefault();
524+
525+
const items = this.getItemsWithLoading();
526+
527+
this.activateItem(List.findNextIndex<T>(items, activeItem + step, Math.sign(step)));
526528
}
527529

528530
private handleFocus = () => {

src/components/List/__stories__/List.stories.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {List, listDefaultProps} from '..';
66
import type {ListProps} from '..';
77

88
import {ListShowcase} from './ListShowcase';
9+
import {ListWithLoader} from './ListWithLoader';
910

1011
type ComponentType = React.JSXElementConstructor<ListProps<string>>;
1112

@@ -44,7 +45,17 @@ export const RenderItem = RenderItemTemplate.bind({});
4445
RenderItem.args = {
4546
items,
4647
renderItem: (item) => `🔥🔥🔥 ${item} 🔥🔥🔥`,
48+
};
49+
50+
const TemplateWithState: ComponentStory<ComponentType> = (args) => <ListWithLoader {...args} />;
51+
52+
export const WithLoadingMoreItems = TemplateWithState.bind({});
53+
WithLoadingMoreItems.args = {
54+
items: ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'],
4755
itemsHeight: 150,
56+
itemHeight: 28,
57+
loading: true,
58+
virtualized: false,
4859
};
4960

5061
const ShowcaseTemplate: ComponentStory<ComponentType> = () => <ListShowcase />;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@use '../../variables';
2+
@use '../../../../styles/mixins';
3+
4+
.list-with-loader {
5+
overflow: auto;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
3+
import {cn} from '../../utils/cn';
4+
import {List} from '../List';
5+
import type {ListProps} from '../types';
6+
7+
import './ListWithLoader.scss';
8+
9+
const b = cn('list-with-loader');
10+
11+
export const ListWithLoader = (args: ListProps<string>) => {
12+
const [items, setItems] = React.useState(args.items);
13+
14+
const onLoadMore = () => {
15+
// delay for fetching new real items
16+
setTimeout(() => {
17+
setItems([...items, ...args.items]);
18+
}, 300);
19+
};
20+
21+
return (
22+
<div className={b()}>
23+
<List {...args} items={items} onLoadMore={onLoadMore} />
24+
</div>
25+
);
26+
};

0 commit comments

Comments
 (0)