From 7b8b8182969335976db769d2279a4fae9d3db3cf Mon Sep 17 00:00:00 2001 From: Stian Didriksen Date: Sun, 7 Aug 2016 16:12:59 +0200 Subject: [PATCH] eslint fixes --- .eslintrc.yml | 1 + src/Select/index.js | 272 ++++++++++++++++++++++++-------------------- 2 files changed, 148 insertions(+), 125 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index 6389a899..24561e40 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -38,3 +38,4 @@ rules: react/jsx-filename-extension: off import/no-extraneous-dependencies: off global-require: off + no-underscore-dangle: off diff --git a/src/Select/index.js b/src/Select/index.js index 48f3f80d..c11d8e53 100644 --- a/src/Select/index.js +++ b/src/Select/index.js @@ -225,12 +225,12 @@ export default class Select extends Component { this.refs.input.blur() } - handleTouchMove = (event) => { + handleTouchMove = () => { // Set a flag that the view is being dragged this.dragging = true } - handleTouchStart = (event) => { + handleTouchStart = () => { // Set a flag that the view is not being dragged this.dragging = false } @@ -271,14 +271,15 @@ export default class Select extends Component { // for the non-searchable select, toggle the menu if (!this.props.searchable) { this.focus() - return this.setState({ + this.setState({ isOpen: !this.state.isOpen, }) + return } if (this.state.isFocused) { // On iOS, we can get into a state where we think the input is focused but it isn't really, - // since iOS ignores programmatic calls to input.focus() that weren't triggered by a click event. + // since iOS ignores programmatic calls to input.focus() that weren't triggered by a click // Call focus() again here to be safe. this.focus() @@ -429,7 +430,11 @@ export default class Select extends Component { case 36: // home key this.focusStartOption() default: - if (this.props.allowCreate && this.props.multi && event.keyCode === this.props.addItemOnKeyCode) { + if ( + this.props.allowCreate && + this.props.multi && + (event.keyCode === this.props.addItemOnKeyCode) + ) { event.preventDefault() event.stopPropagation() this.selectFocusedOption() @@ -459,10 +464,6 @@ export default class Select extends Component { return (multi ? value.length === 0 : Object.keys(value).length === 0) } - getOptionLabel = (op) => { - return op[this.props.labelKey] - } - getValueArray = (value) => { if (this.props.multi) { if (typeof value === 'string') value = value.split(this.props.delimiter) @@ -478,8 +479,8 @@ export default class Select extends Component { expandValue = (value) => { if (typeof value !== 'string' && typeof value !== 'number') return value - let { options, valueKey } = this.props - if (!options) return + const { options, valueKey } = this.props + if (!options) return undefined for (let i = 0; i < options.length; i++) { if (options[i][valueKey] === value) { return options[i] @@ -489,6 +490,8 @@ export default class Select extends Component { if (this.props.allowCreate && value !== '') { return this.createNewOption(value) } + + return undefined } setValue = (value) => { @@ -541,9 +544,8 @@ export default class Select extends Component { this.setValue(valueArray.filter(i => { if (i.create) { return i[this.props.valueKey] !== value[this.props.valueKey] && i[this.props.labelKey] !== value[this.props.labelKey] - } else { - return i !== value } + return i !== value })) this.focus() } @@ -676,15 +678,54 @@ export default class Select extends Component { return newOption } - renderLoading() { - if (!this.props.isLoading) { - return false + getOptionLabel = op => op[this.props.labelKey] + + filterOptions = (excludeOptions) => { + let filterValue = this.state.inputValue + const originalFilterValue = filterValue + const options = this.props.options || [] + let filteredOptions = [] + if (typeof this.props.filterOptions === 'function') { + filteredOptions = this.props.filterOptions.call(this, options, filterValue, excludeOptions) + } else if (this.props.filterOptions) { + if (this.props.ignoreCase) { + filterValue = filterValue.toLowerCase() + } + if (excludeOptions) excludeOptions = excludeOptions.map(i => i[this.props.valueKey]) + filteredOptions = options.filter(option => { + if (excludeOptions && excludeOptions.indexOf(option[this.props.valueKey]) > -1) return false + if (this.props.filterOption) return this.props.filterOption.call(this, option, filterValue) + if (!filterValue) return true + let valueTest = String(option[this.props.valueKey]) + let labelTest = String(option[this.props.labelKey]) + if (this.props.ignoreCase) { + if (this.props.matchProp !== 'label') valueTest = valueTest.toLowerCase() + if (this.props.matchProp !== 'value') labelTest = labelTest.toLowerCase() + } + return this.props.matchPos === 'start' ? ( + (this.props.matchProp !== 'label' && valueTest.substr(0, filterValue.length) === filterValue) || + (this.props.matchProp !== 'value' && labelTest.substr(0, filterValue.length) === filterValue) + ) : ( + (this.props.matchProp !== 'label' && valueTest.indexOf(filterValue) >= 0) || + (this.props.matchProp !== 'value' && labelTest.indexOf(filterValue) >= 0) + ) + }) + } else { + filteredOptions = options } - return ( -