Skip to content

Commit

Permalink
feat(reply): Change props to generic type
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingze Xiao committed May 14, 2020
1 parent df80773 commit 11bf610
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
15 changes: 6 additions & 9 deletions src/components/Popups/ReplyField/ItemList.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
import React, { SyntheticEvent } from 'react';
import noop from 'lodash/noop';
import uniqueId from 'lodash/uniqueId';
import ItemRow from './ItemRow';
import './ItemList.scss';

export type Props = {
export type Props<T extends { id: string }> = {
activeItemIndex?: number;
itemRowAs?: JSX.Element;
items: Record<string, unknown>[];
items: T[];
onSelect: (index: number, event: React.SyntheticEvent) => void;
setActiveItemIndex?: (index: number) => void;
};

const ItemList = ({
export default function ItemList<T extends { id: string }>({
activeItemIndex = 0,
itemRowAs = <ItemRow />,
items,
onSelect,
setActiveItemIndex = noop,
...rest
}: Props): JSX.Element => {
}: Props<T>): JSX.Element {
return (
<ul data-testid="ba-ItemList" role="listbox" {...rest}>
{items.map((item, index) =>
React.cloneElement(itemRowAs, {
...item,
key: item.id ? String(item.id) : uniqueId(),
key: item.id,
className: 'ba-ItemList-row',
isActive: index === activeItemIndex,
onClick: (event: SyntheticEvent) => {
Expand All @@ -42,6 +41,4 @@ const ItemList = ({
)}
</ul>
);
};

export default ItemList;
}
35 changes: 20 additions & 15 deletions src/components/Popups/ReplyField/PopupList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import PopupBase from '../PopupBase';
import { Options, PopupReference } from '../Popper';
import './PopupList.scss';

export type Props = {
export type Props<T extends { id: string }> = {
activeItemIndex?: number;
itemRowAs?: JSX.Element;
items: Record<string, unknown>[];
items: T[];
onSelect: (index: number, event: React.SyntheticEvent) => void;
options?: Partial<Options>;
reference: PopupReference;
Expand All @@ -35,16 +35,21 @@ const defaultOptions: Partial<Options> = {
placement: 'bottom-start',
};

const PopupList = ({ items, reference, options, ...rest }: Props): JSX.Element => (
<PopupBase className="ba-PopupList" options={{ ...defaultOptions, ...options }} reference={reference}>
{isEmpty(items) ? (
<div className="ba-PopupList-prompt">
<FormattedMessage {...messages.popupListPrompt} />
</div>
) : (
<ItemList items={items} {...rest} />
)}
</PopupBase>
);

export default PopupList;
export default function PopupList<T extends { id: string }>({
items,
reference,
options,
...rest
}: Props<T>): JSX.Element {
return (
<PopupBase className="ba-PopupList" options={{ ...defaultOptions, ...options }} reference={reference}>
{isEmpty(items) ? (
<div className="ba-PopupList-prompt">
<FormattedMessage {...messages.popupListPrompt} />
</div>
) : (
<ItemList items={items} {...rest} />
)}
</PopupBase>
);
}
2 changes: 1 addition & 1 deletion src/components/Popups/ReplyField/ReplyField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export default class ReplyField extends React.Component<Props, State> {
/>

{popupReference && (
<PopupList
<PopupList<Collaborator>
activeItemIndex={activeItemIndex}
itemRowAs={itemRowAs}
items={this.getCollaborators(getActiveMentionForEditorState(editorState))}
Expand Down
3 changes: 2 additions & 1 deletion src/components/Popups/ReplyField/__tests__/ItemList-test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import { shallow, ShallowWrapper } from 'enzyme';
import ItemList, { Props } from '../ItemList';
import { Collaborator } from '../../../../@types';

describe('components/Popups/ReplyField/ItemList', () => {
const defaults: Props = {
const defaults: Props<Collaborator> = {
items: [
{ id: 'testid1', name: 'test1' },
{ id: 'testid2', name: 'test2' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import React from 'react';
import { shallow, ShallowWrapper } from 'enzyme';
import PopupBase from '../../PopupBase';
import PopupList, { Props } from '../PopupList';
import { Collaborator } from '../../../../@types';

describe('components/Popups/ReplyField/PopupList', () => {
const defaults: Props = {
const defaults: Props<Collaborator> = {
items: [],
onSelect: jest.fn(),
reference: document.createElement('div'),
Expand Down

0 comments on commit 11bf610

Please sign in to comment.