Skip to content

Commit

Permalink
Pass event and name to onAddItem
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffcarbs committed Oct 2, 2016
1 parent 3d05b4a commit 1ad8abd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
8 changes: 4 additions & 4 deletions docs/app/Examples/modules/Dropdown/Types/AllowAdditions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ const options = [
export default class AllowAdditionsExample extends Component {
state = { optionsSingle: options, optionsMultiple: options }

handleAdditionSingle = value => {
handleAdditionSingle = (e, { value }) => {
// validate, save to server, show a message to the user ...
this.setState({
optionsSingle: [{ text: value, value }, ...this.state.optionsSingle],
})
}

handleAdditionMultiple = value => {
handleAdditionMultiple = (e, { value }) => {
// validate, save to server, show a message to the user ...
this.setState({
optionsMultiple: [{ text: value, value }, ...this.state.optionsMultiple],
})
}

handleChangeSingle = (e, value) => this.setState({ currentValue: value })
handleChangeSingle = (e, { value }) => this.setState({ currentValue: value })

handleChangeMultiple = (e, values) => this.setState({ currentValues: values })
handleChangeMultiple = (e, { value }) => this.setState({ currentValues: value })

render() {
const { currentValue, currentValues } = this.state
Expand Down
18 changes: 11 additions & 7 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ export default class Dropdown extends Component {
PropTypes.bool,
]),

/** Called with the new value added by the user. Use this to update the options list. */
onAddItem: PropTypes.func,

/** Position of the `Add: ...` option in the dropdown list ('top' or 'bottom'). */
additionPosition: PropTypes.oneOf(_meta.props.additionPosition),

Expand All @@ -141,6 +138,9 @@ export default class Dropdown extends Component {
// Callbacks
// ------------------------------------

/** Called with the name and new value added by the user. Use this to update the options list. */
onAddItem: PropTypes.func,

/** Called with the React Synthetic Event on Dropdown blur. */
onBlur: PropTypes.func,

Expand Down Expand Up @@ -405,15 +405,17 @@ export default class Dropdown extends Component {

selectHighlightedItem = (e) => {
const { open } = this.state
const { multiple, onAddItem, options } = this.props
const { multiple, name, onAddItem, options } = this.props
const value = _.get(this.getSelectedItem(), 'value')

// prevent selecting null if there was no selected item value
// prevent selecting duplicate items when the dropdown is closed
if (!value || !open) return

// notify the onAddItem prop if this is a new value
if (onAddItem && !_.some(options, { text: value })) onAddItem(value)
if (onAddItem && !_.some(options, { text: value })) {
onAddItem(e, { name, value })
}

// notify the onChange prop that the user is trying to change value
if (multiple) {
Expand Down Expand Up @@ -492,7 +494,7 @@ export default class Dropdown extends Component {
handleItemClick = (e, value) => {
debug('handleItemClick()')
debug(value)
const { multiple, onAddItem, options } = this.props
const { multiple, name, onAddItem, options } = this.props
const item = this.getItemByValue(value) || {}

// prevent toggle() in handleClick()
Expand All @@ -505,7 +507,9 @@ export default class Dropdown extends Component {
if (item.disabled) return

// notify the onAddItem prop if this is a new value
if (onAddItem && !_.some(options, { value })) onAddItem(value)
if (onAddItem && !_.some(options, { text: value })) {
onAddItem(e, { name, value })
}

// notify the onChange prop that the user is trying to change value
if (multiple) {
Expand Down
10 changes: 5 additions & 5 deletions src/modules/Search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ export default class Search extends Component {

// onChange needs to receive a value
// can't rely on props.value if we are controlled
onChange = (e, result) => {
debug('onChange()')
handleChange = (e, result) => {
debug('handleChange()')
debug(result)
const { onChange } = this.props
if (onChange) onChange(e, result)
Expand Down Expand Up @@ -286,7 +286,7 @@ export default class Search extends Component {

// notify the onChange prop that the user is trying to change value
this.setValue(result.title)
this.onChange(e, result)
this.handleChange(e, result)
this.close()
}

Expand Down Expand Up @@ -333,7 +333,7 @@ export default class Search extends Component {

// notify the onChange prop that the user is trying to change value
this.setValue(result.title)
this.onChange(e, result)
this.handleChange(e, result)
this.close()
}

Expand Down Expand Up @@ -616,7 +616,7 @@ export default class Search extends Component {
className={classes}
onBlur={this.handleBlur}
onFocus={this.handleFocus}
onChange={this.onChange}
onChange={this.handleChange}
onMouseDown={this.handleMouseDown}
>
{this.renderSearchInput()}
Expand Down
10 changes: 6 additions & 4 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1596,8 +1596,9 @@ describe('Dropdown Component', () => {

it('calls onAddItem prop when clicking new value', () => {
const spy = sandbox.spy()
const name = 'my-dropdown'
const search = wrapperMount(
<Dropdown options={customOptions} selection search allowAdditions onAddItem={spy} />
<Dropdown options={customOptions} selection search allowAdditions onAddItem={spy} name={name} />
)
.find('input.search')

Expand All @@ -1609,21 +1610,22 @@ describe('Dropdown Component', () => {
.simulate('click')

spy.should.have.been.calledOnce()
spy.firstCall.args[0].should.equal('boo')
spy.should.have.been.calledWith(sandbox.match.any, { name, value: 'boo' })
})

it('calls onAddItem prop when pressing enter on new value', () => {
const spy = sandbox.spy()
const name = 'my-dropdown'
const search = wrapperMount(
<Dropdown options={customOptions} selection search allowAdditions onAddItem={spy} />
<Dropdown options={customOptions} selection search allowAdditions onAddItem={spy} name={name} />
)
.find('input.search')

search.simulate('change', { target: { value: 'boo' } })
domEvent.keyDown(document, { key: 'Enter' })

spy.should.have.been.calledOnce()
spy.firstCall.args[0].should.equal('boo')
spy.should.have.been.calledWith(sandbox.match.any, { name, value: 'boo' })
})
})

Expand Down
4 changes: 2 additions & 2 deletions test/specs/modules/Search/Search-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ describe('Search Component', () => {
.simulate('click', nativeEvent)

spy.should.have.been.calledOnce()
spy.firstCall.args[1].should.deep.equal(randomResult)
spy.should.have.been.calledWith(sandbox.match.any, randomResult)
})
it('is called with event and value when pressing enter on a selected item', () => {
const firstResult = options[0]
Expand All @@ -553,7 +553,7 @@ describe('Search Component', () => {
domEvent.keyDown(document, { key: 'Enter' })

spy.should.have.been.calledOnce()
spy.firstCall.args[1].should.deep.equal(firstResult)
spy.should.have.been.calledWith(sandbox.match.any, firstResult)
})
it('is not called when updating the value prop', () => {
const value = _.sample(options).title
Expand Down

0 comments on commit 1ad8abd

Please sign in to comment.