Skip to content

Commit f248a14

Browse files
committed
Add descriptions to API documentation and examples to custom component props, fixes #182
1 parent 9a5992f commit f248a14

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

README.md

+37-5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class App extends React.Component {
4747
{ id: 6, name: "Apricots" }
4848
]
4949
}
50+
51+
this.reactTags = React.createRef()
5052
}
5153

5254
onDelete (i) {
@@ -63,6 +65,7 @@ class App extends React.Component {
6365
render () {
6466
return (
6567
<ReactTags
68+
ref={this.reactTags}
6669
tags={this.state.tags}
6770
suggestions={this.state.suggestions}
6871
onDelete={this.onDelete.bind(this)}
@@ -259,11 +262,31 @@ Enable users to delete selected tags when backspace is pressed while focussed on
259262

260263
#### tagComponent (optional)
261264

262-
Provide a custom tag component to render. Defaults to `null`.
265+
Provide a custom tag component to render. Receives the tag, button text, and delete callback as props. Defaults to `null`.
266+
267+
```jsx
268+
function TagComponent({ tag, removeButtonText, onDelete }) {
269+
return (
270+
<button type='button' title={removeButtonText} onClick={onDelete}>
271+
{tag.name}
272+
</button>
273+
)
274+
}
275+
```
263276

264277
#### suggestionComponent (optional)
265278

266-
Provide a custom suggestion component to render. Default: `null`.
279+
Provide a custom suggestion component to render. Receives the suggestion and current query as props. Defaults to `null`.
280+
281+
```jsx
282+
function SuggestionComponent({ item, query }) {
283+
return (
284+
<span id={item.id} className={item.name === query ? 'match' : 'no-match'}>
285+
{item.name}
286+
</span>
287+
)
288+
}
289+
```
267290

268291
#### inputAttributes (optional)
269292

@@ -272,11 +295,20 @@ An object containing additional attributes that will be applied to the text inpu
272295

273296
### API
274297

275-
#### addTag(tag)
298+
By adding a `ref` to any instances of this component you can access its API methods.
299+
300+
#### `addTag(tag)`
301+
302+
Adds a tag to the list of selected tags. This will trigger the validation and addition callbacks.
303+
304+
#### `deleteTag(index)`
305+
306+
Removes a tag from the list of selected tags. This will trigger the delete callback.
307+
308+
#### `clearInput()`
276309

277-
#### deleteTag(index)
310+
Clears the input and current query.
278311

279-
#### clearInput()
280312

281313
### Styling
282314

example/main.js

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class App extends React.Component {
1414
],
1515
suggestions
1616
}
17+
18+
this.reactTags = React.createRef()
1719
}
1820

1921
onDelete (i) {
@@ -32,6 +34,7 @@ class App extends React.Component {
3234
<>
3335
<p>Select the countries you have visited using React Tags below:</p>
3436
<ReactTags
37+
ref={this.reactTags}
3538
tags={this.state.tags}
3639
suggestions={this.state.suggestions}
3740
onDelete={this.onDelete.bind(this)}

0 commit comments

Comments
 (0)