Skip to content
This repository was archived by the owner on Nov 20, 2021. It is now read-only.

Commit 4874ed1

Browse files
author
Ola Holmström
committed
control input box
1 parent 180e8c7 commit 4874ed1

File tree

16 files changed

+20916
-19198
lines changed

16 files changed

+20916
-19198
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
.DS_Store
2-
components
32
node_modules
43
bower_components
54
*.log

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 3.14.0 (2016-12-07)
2+
3+
* Add `inputValue` and `onChangeInput` which allows control of input.
4+
15
### 3.13.6 (2016-10-11)
26

37
* Fix input focusing error.

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ Highly customizable [React](http://facebook.github.io/react/index.html) componen
1919
* [FAQ](#faq)
2020
* [How do I make the input dynamically grow in size?](#how-do-i-make-the-input-dynamically-grow-in-size)
2121
* [How do I add auto suggestion?](#how-do-i-add-auto-suggestion)
22+
* [How do I control the value of the input box?](#how-do-i-control-the-value-of-the-input-box)
2223
* [Component Interface](#component-interface)
2324
* [Props](#props)
2425
* [value (required)](#value-required)
2526
* [onChange (required)](#onchange-required)
27+
* [onChangeInput](#onchangeinput)
2628
* [addKeys](#addkeys)
2729
* [currentValue](#currentvalue)
30+
* [inputValue](#inputvalue)
2831
* [onlyUnique](#onlyunique)
2932
* [validationRegex](#validationregex)
3033
* [disabled](#disabled)
@@ -42,6 +45,8 @@ Highly customizable [React](http://facebook.github.io/react/index.html) componen
4245
* [focus()](#focus)
4346
* [blur()](#blur)
4447
* [accept()](#accept)
48+
* [addTag()](#addTag)
49+
* [clearInput()](#clearInput)
4550
* [Styling](#styling)
4651
* [Contributors](#contributors)
4752
* [Changelog](#changelog)
@@ -123,7 +128,39 @@ function autosuggestRenderInput (props) {
123128
}
124129
```
125130

126-
A working example can be found in [`example/index.js`](https://github.com/olahol/react-tagsinput/blob/master/example/index.js#L137).
131+
A working example can be found in [`example/components/autocomplete.js`](https://github.com/olahol/react-tagsinput/blob/master/example/components/autocomplete.js).
132+
133+
##### How do I control the value of the input box?
134+
135+
Use `inputValue` and `onChangeInput`:
136+
137+
```js
138+
class Example extends React.Component {
139+
constructor() {
140+
super()
141+
this.state = {tags: [], tag: ''}
142+
}
143+
144+
handleChange(tags) {
145+
this.setState({tags})
146+
}
147+
148+
handleChangeInput(tag) {
149+
this.setState({tag})
150+
}
151+
152+
render() {
153+
return (
154+
<TagsInput
155+
value={this.state.tags}
156+
onChange={::this.handleChange}
157+
inputValue={this.state.tag}
158+
onChangeInput={::this.handleChangeInput}
159+
/>
160+
)
161+
}
162+
}
163+
```
127164

128165
## Component Interface
129166

@@ -139,6 +176,10 @@ Callback when tags change, gets three arguments `tags` which is the new
139176
tag array, `changed` which is an array of the tags that have changed and
140177
`changedIndexes` which is an array of the indexes that have changed.
141178

179+
##### onChangeInput
180+
181+
Callback from the input box, gets one argument `value` which is the content of the input box.
182+
142183
##### addKeys
143184

144185
An array of key codes that add a tag, default is `[9, 13]` (Tab and Enter).
@@ -147,6 +188,10 @@ An array of key codes that add a tag, default is `[9, 13]` (Tab and Enter).
147188

148189
A string to set a value on the input.
149190

191+
##### inputValue
192+
193+
Similar to `currentValue` but needed for controlling the input box.
194+
150195
##### onlyUnique
151196

152197
Allow only unique tags, default is `false`.

bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"react-tagsinput.js",
55
"react-tagsinput.css"
66
],
7-
"version": "3.13.6",
7+
"version": "3.14.0",
88
"homepage": "https://github.com/olahol/react-tagsinput",
9-
"description": "Simple react.js component for inputing tags",
9+
"description": "Highly customizable React component for inputing tags",
1010
"keywords": [
1111
"react",
1212
"tags",

example/bundle.js

Lines changed: 20395 additions & 18917 deletions
Large diffs are not rendered by default.

example/components/autoadd.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React from 'react'
2+
3+
import TagsInput from '../../src'
4+
5+
class Example extends React.Component {
6+
constructor () {
7+
super()
8+
this.state = {tags: [], tag: ''}
9+
}
10+
11+
handleChange (tags) {
12+
this.setState({tags})
13+
}
14+
15+
handleInputChange (value) {
16+
if (value.length > 2) {
17+
return this.refs.tagsinput.accept()
18+
}
19+
20+
this.setState({tag: value})
21+
}
22+
23+
render () {
24+
return (
25+
<TagsInput
26+
ref='tagsinput'
27+
value={this.state.tags}
28+
onChange={::this.handleChange}
29+
inputValue={this.state.tag}
30+
onChangeInput={::this.handleInputChange}
31+
/>
32+
)
33+
}
34+
}
35+
36+
export default Example

example/components/autocomplete.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import React from 'react'
2+
3+
import TagsInput from '../../src'
4+
5+
import Autosuggest from 'react-autosuggest'
6+
7+
function states () {
8+
return [
9+
{abbr: 'AL', name: 'Alabama'},
10+
{abbr: 'AK', name: 'Alaska'},
11+
{abbr: 'AZ', name: 'Arizona'},
12+
{abbr: 'AR', name: 'Arkansas'},
13+
{abbr: 'CA', name: 'California'},
14+
{abbr: 'CO', name: 'Colorado'},
15+
{abbr: 'CT', name: 'Connecticut'},
16+
{abbr: 'DE', name: 'Delaware'},
17+
{abbr: 'FL', name: 'Florida'},
18+
{abbr: 'GA', name: 'Georgia'},
19+
{abbr: 'HI', name: 'Hawaii'},
20+
{abbr: 'ID', name: 'Idaho'},
21+
{abbr: 'IL', name: 'Illinois'},
22+
{abbr: 'IN', name: 'Indiana'},
23+
{abbr: 'IA', name: 'Iowa'},
24+
{abbr: 'KS', name: 'Kansas'},
25+
{abbr: 'KY', name: 'Kentucky'},
26+
{abbr: 'LA', name: 'Louisiana'},
27+
{abbr: 'ME', name: 'Maine'},
28+
{abbr: 'MD', name: 'Maryland'},
29+
{abbr: 'MA', name: 'Massachusetts'},
30+
{abbr: 'MI', name: 'Michigan'},
31+
{abbr: 'MN', name: 'Minnesota'},
32+
{abbr: 'MS', name: 'Mississippi'},
33+
{abbr: 'MO', name: 'Missouri'},
34+
{abbr: 'MT', name: 'Montana'},
35+
{abbr: 'NE', name: 'Nebraska'},
36+
{abbr: 'NV', name: 'Nevada'},
37+
{abbr: 'NH', name: 'New Hampshire'},
38+
{abbr: 'NJ', name: 'New Jersey'},
39+
{abbr: 'NM', name: 'New Mexico'},
40+
{abbr: 'NY', name: 'New York'},
41+
{abbr: 'NC', name: 'North Carolina'},
42+
{abbr: 'ND', name: 'North Dakota'},
43+
{abbr: 'OH', name: 'Ohio'},
44+
{abbr: 'OK', name: 'Oklahoma'},
45+
{abbr: 'OR', name: 'Oregon'},
46+
{abbr: 'PA', name: 'Pennsylvania'},
47+
{abbr: 'RI', name: 'Rhode Island'},
48+
{abbr: 'SC', name: 'South Carolina'},
49+
{abbr: 'SD', name: 'South Dakota'},
50+
{abbr: 'TN', name: 'Tennessee'},
51+
{abbr: 'TX', name: 'Texas'},
52+
{abbr: 'UT', name: 'Utah'},
53+
{abbr: 'VT', name: 'Vermont'},
54+
{abbr: 'VA', name: 'Virginia'},
55+
{abbr: 'WA', name: 'Washington'},
56+
{abbr: 'WV', name: 'West Virginia'},
57+
{abbr: 'WI', name: 'Wisconsin'},
58+
{abbr: 'WY', name: 'Wyoming'}
59+
]
60+
}
61+
62+
class AutocompleteExample extends React.Component {
63+
constructor () {
64+
super()
65+
this.state = {tags: []}
66+
}
67+
68+
handleChange (tags) {
69+
this.setState({tags})
70+
}
71+
72+
render () {
73+
const autocompleteRenderInput = (props) => {
74+
const {addTag, ...other} = props
75+
76+
const handleOnChange = (e, {newValue, method}) => {
77+
if (method === 'enter') {
78+
e.preventDefault()
79+
} else {
80+
props.onChange(e)
81+
}
82+
}
83+
84+
const inputValue = (props.value && props.value.trim().toLowerCase()) || ''
85+
const inputLength = inputValue.length
86+
87+
let {tags} = this.state
88+
let suggestions = states().filter((state) => {
89+
return state.name.toLowerCase().slice(0, inputLength) === inputValue
90+
})
91+
92+
return (
93+
<Autosuggest
94+
ref={props.ref}
95+
suggestions={suggestions}
96+
shouldRenderSuggestions={(value) => value && value.trim().length > 0}
97+
getSuggestionValue={(suggestion) => suggestion.name}
98+
renderSuggestion={(suggestion) => <span>{suggestion.name}</span>}
99+
inputProps={{...other, onChange: handleOnChange}}
100+
onSuggestionSelected={(e, {suggestion}) => {
101+
props.addTag(suggestion.name)
102+
}}
103+
/>
104+
)
105+
}
106+
107+
return <TagsInput renderInput={autocompleteRenderInput} value={this.state.tags} onChange={::this.handleChange} />
108+
}
109+
}
110+
111+
112+
export default AutocompleteExample

example/components/autosize.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react'
2+
3+
import TagsInput from '../../src'
4+
5+
import AutosizeInput from 'react-input-autosize'
6+
7+
class AutosizeExample extends React.Component {
8+
constructor () {
9+
super()
10+
this.state = {tags: []}
11+
}
12+
13+
handleChange (tags) {
14+
this.setState({tags})
15+
}
16+
17+
render () {
18+
function autosizingRenderInput (props) {
19+
let {onChange, value, addTag, ...other} = props
20+
return (
21+
<AutosizeInput type='text' onChange={onChange} value={value} {...other} />
22+
)
23+
}
24+
25+
return <TagsInput renderInput={autosizingRenderInput} value={this.state.tags} onChange={::this.handleChange} />
26+
}
27+
}
28+
29+
export default AutosizeExample

example/components/email.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react'
2+
3+
import TagsInput from '../../src'
4+
5+
class EmailExample extends React.Component {
6+
constructor () {
7+
super()
8+
this.state = {tags: []}
9+
}
10+
11+
handleChange (tags) {
12+
this.setState({tags})
13+
}
14+
15+
render () {
16+
var EMAIL_VALIDATION_REGEX = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i
17+
18+
return (
19+
<TagsInput
20+
value={this.state.tags}
21+
addKeys={[9, 13, 32, 186, 188]} // tab, enter, space, semicolon, comma
22+
onlyUnique={true}
23+
addOnPaste={true}
24+
validationRegex={EMAIL_VALIDATION_REGEX}
25+
pasteSplit={data => {
26+
return data.replace(/[\r\n,;]/g, ' ').split(' ').map(d => d.trim())
27+
}}
28+
onChange={::this.handleChange}
29+
/>
30+
)
31+
}
32+
}
33+
34+
export default EmailExample;

example/components/form.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react'
2+
3+
import TagsInput from '../../src'
4+
5+
class FormExample extends React.Component {
6+
constructor () {
7+
super()
8+
this.state = {tags: []}
9+
}
10+
11+
handleChange (tags) {
12+
this.setState({tags})
13+
}
14+
15+
render () {
16+
return (
17+
<form>
18+
<TagsInput name='form' value={this.state.tags} onChange={::this.handleChange} onlyUnique />
19+
</form>
20+
)
21+
}
22+
}
23+
24+
export default FormExample

0 commit comments

Comments
 (0)