Skip to content

Commit

Permalink
feat(List): support item click handler with event in arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mournfulCoroner committed Jun 25, 2024
1 parent 39cd8ad commit b4beb5a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
23 changes: 21 additions & 2 deletions src/components/List/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ import {getUniqId} from '../utils/common';
import {ListLoadingIndicator} from './ListLoadingIndicator';
import {ListItem, SimpleContainer, defaultRenderItem} from './components';
import {listNavigationIgnoredKeys} from './constants';
import type {ListItemData, ListItemProps, ListProps} from './types';
import type {
ItemClickWithEvent,
ListItemData,
ListItemProps,
ListProps,
SimpleItemClick,
} from './types';

import './List.scss';

Expand Down Expand Up @@ -248,7 +254,20 @@ export class List<T = unknown> extends React.Component<ListProps<T>, ListState<T
}
case 'Enter': {
if (typeof activeItem === 'number' && this.props.onItemClick) {
this.props.onItemClick(this.state.items[activeItem], activeItem, true);
if (this.props.onItemClick.length === 1) {
(this.props.onItemClick as ItemClickWithEvent<T>)({
item: this.state.items[activeItem],
index: activeItem,
event,
fromKeyboard: true,
});
} else {
(this.props.onItemClick as SimpleItemClick<T>)(
this.state.items[activeItem],
activeItem,
true,
);
}
}
break;
}
Expand Down
4 changes: 4 additions & 0 deletions src/components/List/__stories__/List.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ export const RenderItem = RenderItemTemplate.bind({});
RenderItem.args = {
items,
renderItem: (item) => `🔥🔥🔥 ${item} 🔥🔥🔥`,
virtualized: false,
onItemClick: (item: string, index: number) => {
alert(`Click on item ${item} with index ${index}`);
},
};

const TemplateWithState: StoryFn<ComponentType> = (args) => <ListWithLoader {...args} />;
Expand Down
17 changes: 15 additions & 2 deletions src/components/List/components/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {Icon} from '../../Icon';
import {block} from '../../utils/cn';
import {eventBroker} from '../../utils/event-broker';
import {ListQa} from '../constants';
import type {ListItemProps} from '../types';
import type {ItemClickWithEvent, ListItemProps, SimpleItemClick} from '../types';

const b = block('list');

Expand Down Expand Up @@ -108,7 +108,20 @@ export class ListItem<T = unknown> extends React.Component<ListItemProps<T>> {
return <div className={b('item-content')}>{renderItem(item, active, itemIndex)}</div>;
}

private onClick = () => this.props.onClick?.(this.props.item, this.props.itemIndex);
private onClick: React.MouseEventHandler<HTMLDivElement> = (event) => {
if (!this.props.onClick) {
return;
}
if (this.props.onClick.length === 1) {
(this.props.onClick as ItemClickWithEvent<T>)({
item: this.props.item,
index: this.props.itemIndex,
event,
});
return;
}
(this.props.onClick as SimpleItemClick<T>)(this.props.item, this.props.itemIndex);
};

private onClickCapture: React.MouseEventHandler<HTMLDivElement> = (event) => {
ListItem.publishEvent({
Expand Down
22 changes: 21 additions & 1 deletion src/components/List/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ export type ListSortParams = {oldIndex: number; newIndex: number};

export type ListItemData<T> = T & {disabled?: boolean};

export type SimpleItemClick<T> = (
item: ListItemData<T>,
index: number,
fromKeyboard?: boolean,
) => void;

export type ItemClickWithEvent<T> = ({
item,
index,
fromKeyboard,
event,
}: {
item: ListItemData<T>;
index: number;
fromKeyboard?: boolean;
event: React.MouseEvent<HTMLDivElement, MouseEvent> | React.KeyboardEvent<HTMLElement>;
}) => void;

type ItemClickHandler<T> = SimpleItemClick<T> | ItemClickWithEvent<T>;

export type ListProps<T = unknown> = QAProps & {
items: ListItemData<T>[];
className?: string;
Expand All @@ -36,7 +56,7 @@ export type ListProps<T = unknown> = QAProps & {
itemIndex: number,
) => React.ReactNode;
filterItem?: (filter: string) => (item: ListItemData<T>) => boolean;
onItemClick?: (item: ListItemData<T>, index: number, fromKeyboard?: boolean) => void;
onItemClick?: ItemClickHandler<T>;
onFilterUpdate?: (filter: string) => void;
onFilterEnd?: ({items}: {items: ListItemData<T>[]}) => void;
onSortEnd?: (params: ListSortParams) => void;
Expand Down

0 comments on commit b4beb5a

Please sign in to comment.