Skip to content
This repository was archived by the owner on Jun 13, 2023. It is now read-only.

Commit b7cd63a

Browse files
authored
Merge pull request #35 from peterp/pp-add-tag-render-prop
Add ability to render tags with a render prop.
2 parents 5261915 + 8cec09c commit b7cd63a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+13516
-2951
lines changed

.babelrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
language: node_js
22
node_js:
3-
- "6"
3+
- "10"

README.md

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ yarn add react-native-tags
2323

2424
```jsx
2525
import React from "react";
26+
import { TouchableOpacity, Text } from "react-native";
2627
import Tags from "react-native-tags";
2728

28-
const UselessComponent = () => (
29+
const MyTagInput = () => (
2930
<Tags
3031
initialText="monkey"
3132
textInputProps={{
@@ -38,26 +39,50 @@ const UselessComponent = () => (
3839
}
3940
containerStyle={{ justifyContent: "center" }}
4041
inputStyle={{ backgroundColor: "white" }}
42+
renderTag={({ tag, index, onPress, deleteTagOnPress, readonly }) => (
43+
<TouchableOpacity key={`${tag}-${index}`} onPress={onPress}>
44+
<Text>{tag}</Text>
45+
</TouchableOpacity>
46+
)}
4147
/>
4248
);
4349
```
4450

51+
## Render Props
52+
53+
### `renderTag`
54+
55+
If you would like to add new functionality or modify the way that the tags are
56+
rendered then pass in a renderTag prop.
57+
58+
| PropName | Description |
59+
| -------- | ------------------------------------------------------------ |
60+
| tag | text of the tag |
61+
| index | position in the array of tags |
62+
| onPress | Removes the tag if `deleteTagsOnPress` and readonly is false |
63+
4564
## Props
4665

47-
| PropName | Description | Default |
48-
| ------------------- | ---------------------------------------------------------------------------------------------- | --------------- |
49-
| initialText | The input element's text | |
50-
| textInputProps | [forward props to the textInput](https://facebook.github.io/react-native/docs/textinput#props) | |
51-
| initialTags | ['the', 'initial', 'tags'] | |
52-
| createTagOnString | Triggers new tag creation | [",", ".", " "] |
53-
| onChangeTags | Fires when tags are added or removed | |
54-
| maxNumberOfTags | The max number of tags that can be entered | infinity |
55-
| onTagPress | Fires when tags are pressed | |
56-
| readonly | Tags cannot be modified | false |
57-
| deleteTagOnPress | Remove the tag when pressed | true |
58-
| containerStyle | Style | |
59-
| style | Style (`containerStyle` alias) | |
60-
| inputContainerStyle | Style | |
61-
| inputStyle | Style | |
62-
| tagContainerStyle | Style | |
63-
| tagTextStyle | Style | |
66+
| PropName | Description | Default |
67+
| ----------------- | ---------------------------------------------------------------------------------------------- | --------------- |
68+
| initialText | The input element's text | |
69+
| textInputProps | [forward props to the textInput](https://facebook.github.io/react-native/docs/textinput#props) | |
70+
| initialTags | ['the', 'initial', 'tags'] | |
71+
| createTagOnString | Triggers new tag creation | [",", ".", " "] |
72+
| onChangeTags | Fires when tags are added or removed | |
73+
| maxNumberOfTags | The max number of tags that can be entered | infinity |
74+
| onTagPress | Fires when tags are pressed | |
75+
| readonly | Tags cannot be modified | false |
76+
| deleteTagOnPress | Remove the tag when pressed | true |
77+
| renderTag | Manage the rendering of your own `Tag` | |
78+
79+
## Style modification props
80+
81+
| PropName | Description | Default |
82+
| ------------------- | ------------------------------ | ------- |
83+
| style | Style (`containerStyle` alias) | |
84+
| containerStyle | Style | |
85+
| inputContainerStyle | Style | |
86+
| inputStyle | Style | |
87+
| tagContainerStyle | Style | |
88+
| tagTextStyle | Style | |

Tags/Tag.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,20 @@
11
import React from "react";
22
import PropTypes from "prop-types";
3-
import { View, Text, TextInput, TouchableOpacity } from "react-native";
3+
import { Text, TouchableOpacity } from "react-native";
44

55
import styles from "./styles";
66

7-
const Tag = ({ label, onPress, tagContainerStyle, tagTextStyle, readonly }) => {
8-
const tagText = <Text style={[styles.tagLabel, tagTextStyle]}>{label}</Text>;
9-
10-
if (readonly) {
11-
return (
12-
<View style={[styles.tag, tagContainerStyle]}>
13-
{tagText}
14-
</View>
15-
)
16-
} else {
17-
return (
18-
<TouchableOpacity style={[styles.tag, tagContainerStyle]} onPress={onPress}>
19-
{tagText}
20-
</TouchableOpacity>
21-
)
22-
}
23-
}
7+
const Tag = ({ label, onPress, tagContainerStyle, tagTextStyle }) => {
8+
return (
9+
<TouchableOpacity style={[styles.tag, tagContainerStyle]} onPress={onPress}>
10+
<Text style={[styles.tagLabel, tagTextStyle]}>{label}</Text>
11+
</TouchableOpacity>
12+
);
13+
};
2414

2515
Tag.propTypes = {
2616
label: PropTypes.string.isRequired,
27-
onPress: PropTypes.func,
28-
readonly: PropTypes.bool
17+
onPress: PropTypes.func.isRequired
2918
};
3019

3120
export default Tag;

Tags/__tests__/Tags-tests.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React from "react";
22
import renderer from "react-test-renderer";
3+
import { Text } from "react-native";
4+
35
import Tags from "../../";
46

57
describe("Tags", () => {
@@ -18,6 +20,24 @@ describe("Tags", () => {
1820
expect(tree).toMatchSnapshot();
1921
});
2022

23+
describe("renderTag prop", () => {
24+
it("allows me to render a custom tag with a function", () => {
25+
const renderTag = jest.fn(({ tag }) => <Text key={tag}>{tag}</Text>);
26+
27+
const tree = renderer
28+
.create(
29+
<Tags
30+
initialText=""
31+
initialTags={["palm", "oil", "sucks"]}
32+
onChangeTags={noop}
33+
renderTag={renderTag}
34+
/>
35+
)
36+
.toJSON();
37+
expect(tree).toMatchSnapshot();
38+
});
39+
});
40+
2141
describe("textInputProps", () => {
2242
const tree = renderer
2343
.create(

0 commit comments

Comments
 (0)