Skip to content

Commit

Permalink
Merge pull request #4 from adammcarth/feature/list-item-key-support
Browse files Browse the repository at this point in the history
Option to specify the react "key" prop for list items
  • Loading branch information
adammcarth authored Jan 20, 2020
2 parents a5903db + e2213b7 commit 2806832
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Example extends Component {
{ label: 'Option 2' },
],
column2: [
{ label: 'Option 3' },
{ label: 'Option 3', key: 'option_3' },
],
}}
/>
Expand All @@ -73,7 +73,7 @@ Further examples can be found in [./examples/src](https://github.com/adammcarth/

| Prop | Description | Default |
|------------------------------|--------------------------------------------------------------------------------|-------------|
| `options` | Data to be populated into the picklists. `{columnId: [{label: ''}, ...], ...}` | |
| `options` | Data to be populated into the picklists. `{columnId: [{label: '', key: ''}, ...], ...}` | |
| `visible` | Not used by default. Set to `true` or `false` to manually handle visibility. | `null` |
| `defaultSelections` | Eg: `{columnId: 'label string to auto-select', ...}` | `{}` |
| `confirmText` | Text displayed in the top right hand corner. | `'Done'` |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-segmented-picker",
"version": "1.0.0",
"version": "1.0.1",
"description": "A segmentable dropdown picker wheel with no native dependencies.",
"main": "index.js",
"scripts": {
Expand Down
8 changes: 6 additions & 2 deletions src/components/SegmentedPicker/SegmentedPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,13 @@ class SegmentedPicker extends Component {

<UI.PickerList>
<FlatList
data={options[column].map(({ label }) => ({ label, column }))}
data={options[column].map(({ label, key }) => ({
label,
key,
column,
}))}
renderItem={this._renderPickerItem}
keyExtractor={item => `${column}_${item.label}`}
keyExtractor={item => `${column}_${item.key || item.label}`}
initialNumToRender={40}
getItemLayout={(data, index) => (
{
Expand Down
27 changes: 26 additions & 1 deletion src/components/SegmentedPicker/SegmentedPicker.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import 'react-native';
import { FlatList } from 'react-native';
import renderer from 'react-test-renderer';
import SegmentedPicker from './SegmentedPicker';

Expand Down Expand Up @@ -83,6 +83,31 @@ describe('SegmentedPicker', () => {
);
});

it('Sets the react "key" of list items correctly', () => {
const column1 = 'column1';
const listData = {
[column1]: [
{ label: 'Adam' },
{ label: 'Francesca', key: 'fran' },
],
};
const testRenderer = renderer.create(
<SegmentedPicker
visible
options={listData}
/>,
);
const testInstance = testRenderer.root;
jest.advanceTimersByTime(1000);
const {
props: { data, keyExtractor: listItemKeyExtractor },
} = testInstance.findByType(FlatList);
expect(listItemKeyExtractor(data[0]))
.toBe(`${column1}_${listData[column1][0].label}`);
expect(listItemKeyExtractor(data[1]))
.toBe(`${column1}_${listData[column1][1].key}`);
});

it('Can be shown and hidden programmatically.', () => {
const ref = React.createRef();
renderer.create(
Expand Down

0 comments on commit 2806832

Please sign in to comment.