Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(List): calling onLoadMore function while using keyboard #1284

Merged
merged 2 commits into from
Jan 30, 2024
Merged
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
12 changes: 7 additions & 5 deletions src/components/List/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@
};

refFilter = React.createRef<HTMLInputElement>();
refContainer = React.createRef<any>();

Check warning on line 94 in src/components/List/List.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
blurTimer: ReturnType<typeof setTimeout> | null = null;
loadingItem = {value: '__LIST_ITEM_LOADING__', disabled: true} as unknown as ListItemData<
loadingItem = {value: '__LIST_ITEM_LOADING__', disabled: false} as unknown as ListItemData<
T & {value: string}
>;
uniqId = getUniqId();
Expand Down Expand Up @@ -207,11 +207,11 @@
break;
}
case 'PageDown': {
this.handleKeyMove(event, pageSize!);

Check warning on line 210 in src/components/List/List.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Forbidden non-null assertion
break;
}
case 'PageUp': {
this.handleKeyMove(event, -pageSize!);

Check warning on line 214 in src/components/List/List.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Forbidden non-null assertion
break;
}
case 'Home': {
Expand Down Expand Up @@ -518,11 +518,13 @@
};

private handleKeyMove(event: React.KeyboardEvent, step: number, defaultItemIndex = 0) {
event.preventDefault();
const {activeItem = defaultItemIndex} = this.state;
this.activateItem(
List.findNextIndex<T>(this.state.items, activeItem + step, Math.sign(step)),
);

event.preventDefault();

const items = this.getItemsWithLoading();

this.activateItem(List.findNextIndex<T>(items, activeItem + step, Math.sign(step)));
}

private handleFocus = () => {
Expand Down
11 changes: 11 additions & 0 deletions src/components/List/__stories__/List.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {List, listDefaultProps} from '..';
import type {ListProps} from '..';

import {ListShowcase} from './ListShowcase';
import {ListWithLoader} from './ListWithLoader';

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

Expand Down Expand Up @@ -44,7 +45,17 @@ export const RenderItem = RenderItemTemplate.bind({});
RenderItem.args = {
items,
renderItem: (item) => `🔥🔥🔥 ${item} 🔥🔥🔥`,
};

const TemplateWithState: ComponentStory<ComponentType> = (args) => <ListWithLoader {...args} />;

export const WithLoadingMoreItems = TemplateWithState.bind({});
WithLoadingMoreItems.args = {
items: ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'],
itemsHeight: 150,
itemHeight: 28,
loading: true,
virtualized: false,
};

const ShowcaseTemplate: ComponentStory<ComponentType> = () => <ListShowcase />;
Expand Down
6 changes: 6 additions & 0 deletions src/components/List/__stories__/ListWithLoader.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@use '../../variables';
@use '../../../../styles/mixins';

.list-with-loader {
overflow: auto;
}
26 changes: 26 additions & 0 deletions src/components/List/__stories__/ListWithLoader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';

import {cn} from '../../utils/cn';
import {List} from '../List';
import type {ListProps} from '../types';

import './ListWithLoader.scss';

const b = cn('list-with-loader');

export const ListWithLoader = (args: ListProps<string>) => {
const [items, setItems] = React.useState(args.items);

const onLoadMore = () => {
// delay for fetching new real items
setTimeout(() => {
setItems([...items, ...args.items]);
}, 300);
};

return (
<div className={b()}>
<List {...args} items={items} onLoadMore={onLoadMore} />
</div>
);
};
Loading