ldx-widgets
Version:
widgets
144 lines (125 loc) • 4.22 kB
JavaScript
(function() {
var React, ReactDOM, SearchInput, Spinner, button, div, input, ref;
React = require('react');
ReactDOM = require('react-dom');
Spinner = React.createFactory(require('./spinner'));
ref = React.DOM, div = ref.div, button = ref.button, input = ref.input;
/*
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.tabIndex - int - default: -1
tab order of the text input
@props.term - String - default: ''
the value of the search field when first rendered
*/
SearchInput = React.createClass({
displayName: 'SearchInput',
getDefaultProps: function() {
return {
wrapClass: 'search-input-wrap',
height: 28,
width: 260,
changeDelay: 0,
placeholder: 'search',
focusOnMount: false,
term: '',
loading: false,
inputClass: '',
tabIndex: -1,
handleChange: function() {}
};
},
componentDidMount: function() {
if (this.props.focusOnMount) {
return this.focus();
}
},
render: function() {
var autoComplete, disabled, handleChange, height, inputClass, loading, placeholder, ref1, tabIndex, term, width, wrapClass;
ref1 = this.props, term = ref1.term, handleChange = ref1.handleChange, height = ref1.height, width = ref1.width, wrapClass = ref1.wrapClass, placeholder = ref1.placeholder, loading = ref1.loading, inputClass = ref1.inputClass, autoComplete = ref1.autoComplete, disabled = ref1.disabled, tabIndex = ref1.tabIndex;
autoComplete = (function() {
switch (typeof autoComplete) {
case 'string':
return autoComplete;
case 'boolean':
if (autoComplete) {
return 'on';
} else {
return 'off';
}
}
})();
return div({
className: wrapClass,
style: {
height: height,
width: width
}
}, [
input({
className: "search-input loading-spinner " + inputClass,
ref: 'searchInput',
type: 'text',
placeholder: placeholder,
key: 'searchField',
value: term || '',
onChange: this.handleChange,
onKeyDown: this.handleKeyDown,
autoComplete: autoComplete,
disabled: disabled,
tabIndex: tabIndex
}), loading ? div({
key: 'input-spinner',
className: 'input-spinner'
}, Spinner({
length: 3
})) : void 0, (term != null ? term.length : void 0) && !disabled ? button({
className: 'search-clear',
title: 'Clear Search',
key: 'searchClearBtn',
onClick: this.clearSearch,
tabIndex: -1
}, []) : void 0
]);
},
handleChange: function(e) {
var base, newTerm, term;
newTerm = e.target.value;
term = this.props.term;
if (newTerm === term) {
return;
}
return typeof (base = this.props).handleChange === "function" ? base.handleChange(newTerm) : void 0;
},
handleKeyDown: function(e) {
var base;
if (e.key === 'Enter') {
e.preventDefault();
}
return typeof (base = this.props).handleKeyDown === "function" ? base.handleKeyDown(e) : void 0;
},
clearSearch: function() {
return this.props.handleChange('');
},
focus: function() {
return ReactDOM.findDOMNode(this.refs.searchInput).focus();
},
blur: function() {
return ReactDOM.findDOMNode(this.refs.searchInput).blur();
}
});
module.exports = SearchInput;
}).call(this);