Skip to content

Commit cd9eb9d

Browse files
authored
add test case (#6)
1 parent 7f1faf0 commit cd9eb9d

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/List.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface ListProps<T> extends React.HTMLAttributes<any> {
4343
data: T[];
4444
height?: number;
4545
itemHeight?: number;
46-
itemKey: string;
46+
itemKey: string | ((item: T) => string);
4747
component?: string | React.FC<any> | React.ComponentClass<any>;
4848
disabled?: boolean;
4949

@@ -396,7 +396,8 @@ class List<T> extends React.Component<ListProps<T>, ListState<T>> {
396396

397397
public getItemKey = (item: T, props?: Partial<ListProps<T>>) => {
398398
const { itemKey } = props || this.props;
399-
return item ? item[itemKey] : null;
399+
400+
return typeof itemKey === 'function' ? itemKey(item) : item[itemKey];
400401
};
401402

402403
/**

tests/props.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import List from '../src';
4+
5+
describe('Props', () => {
6+
it('itemKey is a function', () => {
7+
class Item extends React.Component {
8+
render() {
9+
return this.props.children;
10+
}
11+
}
12+
13+
const wrapper = mount(
14+
<List data={[{ id: 903 }, { id: 1128 }]} itemKey={item => item.id}>
15+
{({ id }) => <Item>{id}</Item>}
16+
</List>,
17+
);
18+
19+
expect(
20+
wrapper
21+
.find(Item)
22+
.at(0)
23+
.key(),
24+
).toBe('903');
25+
26+
expect(
27+
wrapper
28+
.find(Item)
29+
.at(1)
30+
.key(),
31+
).toBe('1128');
32+
});
33+
});

0 commit comments

Comments
 (0)