ldx-widgets
Version:
widgets
171 lines (134 loc) • 4.62 kB
text/coffeescript
React = require 'react'
ReactDOM = require 'react-dom'
{Flux} = require 'delorean'
Input = React.createFactory(require './input_placeholder')
Spinner = React.createFactory(require './spinner')
InputMixin = require '../mixins/input_mixin'
{makeGuid, synthesizeMouseEvent} = require '../utils'
{ENTER} = require '../constants/keyboard'
{div, select, label, option} = React.DOM
###
Select Props
.value - REQUIRED - string
The value of the input
w/ out this props, the input will not work
.options - REQUIRED - array/collection
An array of objects w/ a value and label property
.onChange - REQUIRED
method that will change the value of the input prop
gets passed a single param that is the new value of the input
w/ out this method, the input will not update
.className - OPTIONAL - string - default 'select-input'
optional class to be added the select element itself
.id - OPTIONAL - string - default null
optional id to be added the input element itself
.wrapperClass - OPTIONAL - string default null
class to be added to the wrapper div
.wrapperLabel - OPTIONAL - default null
text for wrapping label element
.loading - OPTIONAL - Boolean
indicator to determine that the input is loading a value from the server
.tabIndex - OPTIONAL - int - default null
tab index for the input
.onEnterKey/onBlur/onFocus
optional handlers for various events
.disabled - OPTIONAL - Boolean - default no
disabled state of the input
.validation - OPTIONAL - method
a method that takes the value and returns an arry of validation objects
always return an empty array for a valid value
see the validation store for more documentation on validation objects
.isInPopover - OPTIONAL - default no
set this to yes if the form is inside a popover or modal, forces the
validation to display above the popover/modal layer
Inconsequential if validation is not being used
.delayedActionOnChange - OPTIONAL - Object
Takes action and interval parameters, will fire the action after the set interval everytime the data changes
.openOnMount - boolean -default no
opens the drop down when it mounts
###
SelectInput2 = React.createClass
displayName: 'SelectInput2'
mixins: [Flux.mixins.storeListener, InputMixin]
watchStores: ['validation']
propTypes:
onChange: React.PropTypes.func.isRequired
options: React.PropTypes.array.isRequired
getDefaultProps: ->
{
type: 'text'
className: 'select-menu'
id: null
wrapperClass: null
wrapperLabel: null
loading: no
tabIndex: null
onKeyDown: null
onKeyPress: null
onFocus: null
onBlur: null
onKeyUp: null
onEnterKey: null
onChange: null
disabled: no
validation: off
isInPopover: no
delayedActionOnChange: null
openOnMount: no
}
render: ->
{value, options, tabIndex, className, loading, onKeyDown, onKeyPress, onBlur, onFocus, wrapperClass, wrapperLabel, id, disabled, validation} =
{errors, forceShowAllErrors} = 'validation'
{valueHasChanged} =
isValid = not errors[]?
outerClass = 'field-wrap'
outerClass += " #{wrapperClass}" if wrapperClass?
outerClass += ' invalid shrink' if not isValid and (valueHasChanged or forceShowAllErrors)
inputClass = 'loading-spinner'
inputClass += " #{className}" if className?
optionEls = (option {
key: "#{item.value}_#{index}"
value: item.value
}, item.label for item, index in options)
input = select {
ref: 'input'
key: 'selectMenu'
onChange:
onKeyUp:
className
id
tabIndex
value
onFocus
onBlur
onKeyDown
onKeyPress
disabled
}, optionEls
# Add a wrapper label element if label prop is present
if wrapperLabel
input = label {
key: 'textInputLabel'
}, [wrapperLabel, input]
div {
className: "#{outerClass}"
}, [
input
div({
key: 'input-spinner'
className: 'input-spinner'
}, Spinner {length: 3}) if loading
div {
className: 'field-errors-show'
key: 'textInputErrorsShow'
ref: 'errorAnchor'
onMouseOver:
onMouseOut:
}
]
componentDidMount: ->
if .openOnMount
setTimeout =>
synthesizeMouseEvent .input, 'mousedown'
, 15
module.exports = SelectInput2