UNPKG

ldx-widgets

Version:

widgets

147 lines (112 loc) 3.25 kB
React = require 'react' ReactDOM = require 'react-dom' Input = React.createFactory(require './input_placeholder') Spinner = React.createFactory(require './spinner') {div, button} = React.DOM ### Search Input Props @props.wrapClass - String - default: search-input-wrap className to apply to the div wrapper @props.placeholder - String - default: search placeholder text for searh field @props.height - Int - default: 18 height of the inout @props.width - Int - default: 260 width of the input @props.changeDelay - Int - default: 0 ms delay before calling change handler @props.handleChange - function method to call when the search term changes @props.focusOnMount - Boolean - default: false whether or not the search input should focus immediately after mounting @props.term - String - default: '' the value of the search field when first rendered ### SearchInput = React.createClass displayName: 'SearchInput' getDefaultProps: -> { wrapClass: 'search-input-wrap' height: 28 width: 260 changeDelay: 0 placeholder: 'search' focusOnMount: no term: '' loading: false inputClass: '' defaultTerm: '' handleChange: -> } render: -> {handleChange, height, width, wrapClass, placeholder, loading, inputClass, autoComplete, disabled} = @props {term} = @state # Autocomplete autoComplete = switch typeof autoComplete when 'string' then autoComplete when 'boolean' if autoComplete then 'on' else 'off' div { className: wrapClass style: {height, width} }, [ Input { className: "search-input loading-spinner #{inputClass}" ref: 'searchInput' type: 'text' placeholder: placeholder key: 'searchField' value: term onChange: @handleChange onKeyDown: @handleKeyDown autoComplete: autoComplete disabled: disabled } div({ key: 'input-spinner' className: 'input-spinner' }, Spinner { length: 3 } ) if loading button { className: 'search-clear' title: 'Clear Search' key: 'searchClearBtn' onClick: @clearSearch tabIndex: -1 }, [] if term?.length and not disabled ] getInitialState: -> { term: @props.defaultTerm } componentDidMount: -> if @props.focusOnMount then @focus() handleChange: (e) -> newTerm = e.target.value {term} = @state # IE fires change events when the field is focused # this just ensures that state onlt changes when the data actually change return unless newTerm isnt term @setState { term: newTerm } if @changeTimer? clearInterval @changeTimer @changeTimer = setTimeout => @props.handleChange?(newTerm) , @props.changeDelay handleKeyDown: (e) -> e.preventDefault() if e.key is 'Enter' setTerm: (term) -> @setState {term} clearSearch: -> @props.handleChange('') @setState term: '' focus: -> ReactDOM.findDOMNode(@refs.searchInput).focus() blur: -> ReactDOM.findDOMNode(@refs.searchInput).blur() module.exports = SearchInput