Skip to content

Commit

Permalink
Prefix all event handlers with "on" rather than "handle", fixes #91
Browse files Browse the repository at this point in the history
  • Loading branch information
i-like-robots committed Nov 30, 2017
1 parent 3077bd9 commit a4ce278
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 72 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ class App extends React.Component {
}
}

handleDelete (i) {
onDelete (i) {
const tags = this.state.tags.slice(0)
tags.splice(i, 1)
this.setState({ tags })
}

handleAddition (tag) {
onAddition (tag) {
const tags = [].concat(this.state.tags, tag)
this.setState({ tags })
}
Expand All @@ -56,8 +56,8 @@ class App extends React.Component {
<ReactTags
tags={this.state.tags}
suggestions={this.state.suggestions}
handleDelete={this.handleDelete.bind(this)}
handleAddition={this.handleAddition.bind(this)} />
onDelete={this.onDelete.bind(this)}
onAddition={this.onAddition.bind(this)} />
)
}
}
Expand All @@ -75,11 +75,11 @@ React.render(<App />, document.getElementById('app'))
- [`minQueryLength`](#minquerylength-optional)
- [`maxSuggestionsLength`](#maxsuggestionslength-optional)
- [`classNames`](#classnames-optional)
- [`handleAddition`](#handleaddition-optional)
- [`handleDelete`](#handledelete-optional)
- [`handleInput`](#handleinput-optional)
- [`handleFocus`](#handlefocus-optional)
- [`handleBlur`](#handleblur-optional)
- [`onAddition`](#onaddition-optional)
- [`onDelete`](#ondelete-optional)
- [`onInput`](#oninput-optional)
- [`onFocus`](#onfocus-optional)
- [`onBlur`](#onblur-optional)
- [`allowNew`](#allownew-optional)
- [`tagComponent`](#tagcomponent-optional)

Expand Down Expand Up @@ -146,34 +146,34 @@ Override the default class names. Defaults:
}
```

#### handleAddition (required)
#### onAddition (required)

Function called when the user wants to add a tag. Receives the tag.

```js
function (tag) {
function onAddition (tag) {
// Add the tag { id, name } to the tag list
tags.push(tag)
}
```

#### handleDelete (required)
#### onDelete (required)

Function called when the user wants to delete a tag. Receives the tag index.

```js
function (i) {
function onDelete (i) {
// Delete the tag at index i
tags.splice(i, 1)
}
```

#### handleInput (optional)
#### onInput (optional)

Optional event handler when the input changes. Receives the current input value.

```js
function (input) {
function onInput (input) {
if (!this.state.busy) {
this.setState({ busy: true })

Expand All @@ -184,11 +184,11 @@ function (input) {
}
```

#### handleFocus (optional)
#### onFocus (optional)

Optional event handler when the input receives focus. Receives no arguments.

#### handleBlur (optional)
#### onBlur (optional)

Optional event handler when focus on the input is lost. Receives no arguments.

Expand Down
8 changes: 4 additions & 4 deletions example/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class App extends React.Component {
}
}

handleDelete (i) {
onDelete (i) {
const tags = this.state.tags.slice(0)
tags.splice(i, 1)
this.setState({ tags })
}

handleAddition (tag) {
onAddition (tag) {
const tags = [].concat(this.state.tags, tag)
this.setState({ tags })
}
Expand All @@ -35,8 +35,8 @@ class App extends React.Component {
<Tags
tags={this.state.tags}
suggestions={this.state.suggestions}
handleDelete={this.handleDelete.bind(this)}
handleAddition={this.handleAddition.bind(this)} />
onDelete={this.onDelete.bind(this)}
onAddition={this.onAddition.bind(this)} />
<hr />
<pre><code>{JSON.stringify(this.state.tags, null, 2)}</code></pre>
</div>
Expand Down
46 changes: 23 additions & 23 deletions lib/ReactTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ class ReactTags extends React.Component {
})
}

handleInput (e) {
onInput (e) {
const query = e.target.value

if (this.props.handleInput) {
this.props.handleInput(query)
if (this.props.onInput) {
this.props.onInput(query)
}

this.setState({ query })
}

handleKeyDown (e) {
onKeyDown (e) {
const { query, selectedIndex } = this.state
const { delimiters } = this.props

Expand Down Expand Up @@ -105,25 +105,25 @@ class ReactTags extends React.Component {
}
}

handleClick (e) {
onClick (e) {
if (document.activeElement !== e.target) {
this.input.input.focus()
}
}

handleBlur () {
onBlur () {
this.setState({ focused: false, selectedIndex: -1 })

if (this.props.handleBlur) {
this.props.handleBlur()
if (this.props.onBlur) {
this.props.onBlur()
}
}

handleFocus () {
onFocus () {
this.setState({ focused: true })

if (this.props.handleFocus) {
this.props.handleFocus()
if (this.props.onFocus) {
this.props.onFocus()
}
}

Expand All @@ -132,7 +132,7 @@ class ReactTags extends React.Component {
return
}

this.props.handleAddition(tag)
this.props.onAddition(tag)

// reset the state
this.setState({
Expand All @@ -142,7 +142,7 @@ class ReactTags extends React.Component {
}

deleteTag (i) {
this.props.handleDelete(i)
this.props.onDelete(i)
this.setState({ query: '' })
}

Expand All @@ -165,16 +165,16 @@ class ReactTags extends React.Component {
this.state.focused && classNames.push(this.state.classNames.rootFocused)

return (
<div className={classNames.join(' ')} onClick={this.handleClick.bind(this)}>
<div className={classNames.join(' ')} onClick={this.onClick.bind(this)}>
<div className={this.state.classNames.selected} aria-live='polite' aria-relevant='additions removals'>
{tags}
</div>
<div
className={this.state.classNames.search}
onBlurCapture={this.handleBlur.bind(this)}
onFocusCapture={this.handleFocus.bind(this)}
onInput={this.handleInput.bind(this)}
onKeyDown={this.handleKeyDown.bind(this)}>
onBlurCapture={this.onBlur.bind(this)}
onFocusCapture={this.onFocus.bind(this)}
onInput={this.onInput.bind(this)}
onKeyDown={this.onKeyDown.bind(this)}>
<Input {...this.state}
ref={(c) => { this.input = c }}
listboxId={listboxId}
Expand Down Expand Up @@ -213,11 +213,11 @@ ReactTags.propTypes = {
suggestions: PropTypes.arrayOf(PropTypes.object),
autoresize: PropTypes.bool,
delimiters: PropTypes.arrayOf(PropTypes.string),
handleDelete: PropTypes.func.isRequired,
handleAddition: PropTypes.func.isRequired,
handleInput: PropTypes.func,
handleFocus: PropTypes.func,
handleBlur: PropTypes.func,
onDelete: PropTypes.func.isRequired,
onAddition: PropTypes.func.isRequired,
onInput: PropTypes.func,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
minQueryLength: PropTypes.number,
maxSuggestionsLength: PropTypes.number,
classNames: PropTypes.object,
Expand Down
4 changes: 2 additions & 2 deletions lib/Suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Suggestions extends React.Component {
})
}

handleMouseDown (item, e) {
onMouseDown (item, e) {
// focus is shifted on mouse down but calling preventDefault prevents this
e.preventDefault()
this.props.addTag(item)
Expand Down Expand Up @@ -64,7 +64,7 @@ class Suggestions extends React.Component {
role='option'
className={classNames.join(' ')}
aria-disabled={item.disabled === true}
onMouseDown={this.handleMouseDown.bind(this, item)}>
onMouseDown={this.onMouseDown.bind(this, item)}>
<span dangerouslySetInnerHTML={markIt(item.name, this.props.query)} />
</li>
)
Expand Down
52 changes: 26 additions & 26 deletions spec/ReactTags.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ function createInstance (data = {}) {
const defaults = {
tags: [],
suggestions: [],
handleBlur: sinon.stub(),
handleFocus: sinon.stub(),
handleDelete: sinon.stub(),
handleAddition: sinon.stub(),
handleInput: sinon.stub()
onBlur: sinon.stub(),
onFocus: sinon.stub(),
onDelete: sinon.stub(),
onAddition: sinon.stub(),
onInput: sinon.stub()
}

props = Object.assign(defaults, data)
Expand Down Expand Up @@ -125,10 +125,10 @@ describe('React Tags', () => {
createInstance()

TestUtils.Simulate.focus($('input'))
sinon.assert.calledOnce(props.handleFocus)
sinon.assert.calledOnce(props.onFocus)

TestUtils.Simulate.blur($('input'))
sinon.assert.calledOnce(props.handleBlur)
sinon.assert.calledOnce(props.onBlur)
})
})

Expand All @@ -147,31 +147,31 @@ describe('React Tags', () => {
it('triggers the change callback', () => {
type(query)

sinon.assert.called(props.handleInput)
sinon.assert.calledWith(props.handleInput, query)
sinon.assert.called(props.onInput)
sinon.assert.calledWith(props.onInput, query)
})

it('can allow new, non-suggested tags to be added', () => {
createInstance({ allowNew: false })

type(query); key('Enter')

sinon.assert.notCalled(props.handleAddition)
sinon.assert.notCalled(props.onAddition)

createInstance({ allowNew: true })

type(query); key('Enter')

sinon.assert.calledOnce(props.handleAddition)
sinon.assert.calledWith(props.handleAddition, { name: query })
sinon.assert.calledOnce(props.onAddition)
sinon.assert.calledWith(props.onAddition, { name: query })
})

it('can add new tags when a delimiter character is entered', () => {
createInstance({ allowNew: true, delimiters: ['Enter', ',', ';'] })

type('foo,bar;baz'); key('Enter')

sinon.assert.calledThrice(props.handleAddition)
sinon.assert.calledThrice(props.onAddition)
})
})

Expand Down Expand Up @@ -291,30 +291,30 @@ describe('React Tags', () => {

key('ArrowDown', 'Enter')

sinon.assert.notCalled(props.handleAddition)
sinon.assert.notCalled(props.onAddition)
})

it('triggers addition when a suggestion is clicked', () => {
type(query); click($('li[role="option"]:nth-child(2)'))

sinon.assert.calledOnce(props.handleAddition)
sinon.assert.calledWith(props.handleAddition, { id: 196, name: 'United Kingdom' })
sinon.assert.calledOnce(props.onAddition)
sinon.assert.calledWith(props.onAddition, { id: 196, name: 'United Kingdom' })
})

it('triggers addition for the selected suggestion when a delimiter is pressed', () => {
key('Enter')

sinon.assert.notCalled(props.handleAddition)
sinon.assert.notCalled(props.onAddition)

type(query); key('ArrowDown', 'ArrowDown', 'Enter')

sinon.assert.calledOnce(props.handleAddition)
sinon.assert.calledWith(props.handleAddition, { id: 196, name: 'United Kingdom' })
sinon.assert.calledOnce(props.onAddition)
sinon.assert.calledWith(props.onAddition, { id: 196, name: 'United Kingdom' })
})

it('triggers addition for an unselected but matching suggestion when a delimiter is pressed', () => {
type('united kingdom'); key('Enter')
sinon.assert.calledWith(props.handleAddition, { id: 196, name: 'United Kingdom' })
sinon.assert.calledWith(props.onAddition, { id: 196, name: 'United Kingdom' })
})

it('clears the input when an addition is triggered', () => {
Expand All @@ -339,20 +339,20 @@ describe('React Tags', () => {
it('triggers removal when a tag is clicked', () => {
click($('.react-tags__selected-tag'))

sinon.assert.calledOnce(props.handleDelete)
sinon.assert.calledWith(props.handleDelete, sinon.match(0))
sinon.assert.calledOnce(props.onDelete)
sinon.assert.calledWith(props.onDelete, sinon.match(0))
})

it('deletes the last selected tag when backspace is pressed and query is empty', () => {
type(''); key('Backspace')

sinon.assert.calledOnce(props.handleDelete)
sinon.assert.calledWith(props.handleDelete, sinon.match(instance.props.tags.length - 1))
sinon.assert.calledOnce(props.onDelete)
sinon.assert.calledWith(props.onDelete, sinon.match(instance.props.tags.length - 1))
})

it('does not delete the last selected tag when backspace is pressed and query is not empty', () => {
type('uni'); key('Backspace')
sinon.assert.notCalled(props.handleDelete)
sinon.assert.notCalled(props.onDelete)
})

it('does not delete the last selected tag when allowBackspace option is false', () => {
Expand All @@ -363,7 +363,7 @@ describe('React Tags', () => {

type(''); key('Backspace')

sinon.assert.notCalled(props.handleDelete)
sinon.assert.notCalled(props.onDelete)
})

it('can render a custom tag component when provided', () => {
Expand Down

0 comments on commit a4ce278

Please sign in to comment.