_onSelect: (event) ->
@props.select(@props.item)
event.preventDefault()
_onAction: (event) ->
@props.action(@props.item)
event.preventDefault()
###
The TokenizingTextField component displays a list of options as you type
and converts them into stylable tokens.
It wraps the Menu component, which takes care of the typing and keyboard
interactions.
See documentation on the propTypes for usage info.
###
module.exports =
TokenizingTextField = React.createClass
displayName: "TokenizingTextField"
propTypes:
# An array of current tokens.
#
# A token is usually an object type like a
# `Contact` or a `SalesforceObject`. The set of tokens is stored as a
# prop instead of `state`. This means that when the set of tokens
# needs to be changed, it is the parent's responsibility to make that
# change.
tokens: React.PropTypes.arrayOf(React.PropTypes.object)
# A unique ID for each token object
#
# A function that, given an object used for tokens, returns a unique
# id (key) for that object.
#
# This is necessary for React to assign each of the subitems and
# unique key.
tokenKey: React.PropTypes.func.isRequired
# What each token looks like
#
# A function that is passed an object and should return React elements
# to display that individual token.
tokenNode: React.PropTypes.func.isRequired
# The function responsible for providing a list of possible options
# given the current input.
#
# It takes the current input as a value and should return an array of
# candidate objects. These objects must be the same type as are passed
# to the `tokens` prop.
onRequestCompletions: React.PropTypes.func.isRequired
# What each suggestion looks like.
#
# This is passed through to the Menu component's `itemContent` prop.
# See components/menu.cjsx for more info.
completionNode: React.PropTypes.func.isRequired
# If the onRequestCompletions function is asynchronous, the parent will
# have to pass in the correct completions as new props.
initialCompletions: React.PropTypes.array
# Gets called when we we're ready to add whatever it is we're
# completing
#
# It's either passed an array of objects (the same ones used to
# render tokens)
#
# OR
#
# It's passed the string currently in the input field. The string case
# happens on paste and blur.
#
# The function doesn't need to return anything, but it is generally
# responible for mutating the parent's state in a way that eventually
# updates this component's `tokens` prop.
onAdd: React.PropTypes.func.isRequired
# Gets called when we remove a token
#
# It's passed an array of objects (the same ones used to render
# tokens)
#
# The function doesn't need to return anything, but it is generally
# responible for mutating the parent's state in a way that eventually
# updates this component's `tokens` prop.
onRemove: React.PropTypes.func.isRequired
# Called when we remove and there's nothing left to remove
onEmptied: React.PropTypes.func
# Gets called when the secondary action of the token gets invoked.
onTokenAction: React.PropTypes.func
# The tabIndex of the input item
tabIndex: React.PropTypes.oneOfType([
React.PropTypes.number
React.PropTypes.string
])
# A Prompt used in the head of the menu
menuPrompt: React.PropTypes.string
# A classSet hash applied to the Menu item
menuClassSet: React.PropTypes.object
mixins: [DragDropMixin]
statics:
configureDragDrop: (registerType) ->
registerType('token', {
dropTarget:
acceptDrop: (component, token) ->
component._addToken(token)
})
getInitialState: ->
completions: @props.initialCompletions ? []
inputValue: ""
selectedTokenKey: null
componentDidMount: ->
input = React.findDOMNode(@refs.input)
check = (fn) -> (event) ->
return unless event.target is input
# Wrapper to guard against events triggering on the wrong element
fn(event)
@subscriptions = new CompositeDisposable()
@subscriptions.add atom.commands.add '.tokenizing-field',
'tokenizing-field:cancel': check => @_clearInput()
'tokenizing-field:remove': check => @_removeToken()
'tokenizing-field:add-suggestion': check => @_addToken(@refs.completions.getSelectedItem() || @state.completions[0])
'tokenizing-field:add-input-value': check => @_addInputValue()
componentWillUnmount: ->
@subscriptions?.dispose()
componentWillReceiveProps: (nextProps) ->
@setState completions: nextProps.initialCompletions ? []
componentDidUpdate: ->
# Measure the width of the text in the input and
# resize the input field to fit.
input = React.findDOMNode(@refs.input)
measure = React.findDOMNode(@refs.measure)
measure.innerText = @state.inputValue
measure.style.top = input.offsetTop + "px"
measure.style.left = input.offsetLeft + "px"
input.style.width = "calc(6px + #{measure.offsetWidth}px)"
render: ->
{Menu} = require 'ui-components'
classes = classNames _.extend (@props.menuClassSet ? {}),
"tokenizing-field": true
"focused": @state.focus
"native-key-bindings": true
"empty": (@state.inputValue ? "").trim().length is 0
"has-suggestions": @state.completions.length > 0