Fix react-select definition for filterOptions (#10109)

This commit is contained in:
velveret
2016-07-19 17:16:17 -07:00
committed by Mohamed Hegazy
parent 3a5b669575
commit 2f16c8ae7e
2 changed files with 28 additions and 1 deletions
+27
View File
@@ -1,7 +1,9 @@
/// <reference path="../lodash/lodash.d.ts" />
/// <reference path="../react/react.d.ts" />
/// <reference path="../react/react-dom.d.ts" />
/// <reference path="./react-select.d.ts" />
import * as _ from "lodash";
import * as React from "react";
import * as ReactDOM from "react-dom";
@@ -61,6 +63,30 @@ const CustomValue = React.createClass({
}
});
const filterOptions = (options: Array<Option>, filter: string, values: Array<Option>) => {
// Filter already selected values
let filteredOptions = options.filter(option => {
return !(_.includes(values, option));
});
// Filter by label
if (filter !== undefined && filter != null && filter.length > 0) {
filteredOptions = filteredOptions.filter(option => {
return RegExp(filter, 'ig').test(option.label);
});
}
// Append Addition option
if (filteredOptions.length == 0) {
filteredOptions.push({
label: `Create: ${filter}`,
value: filter
});
}
return filteredOptions;
};
class SelectTest extends React.Component<React.Props<{}>, {}> {
render() {
@@ -89,6 +115,7 @@ class SelectTest extends React.Component<React.Props<{}>, {}> {
autosize: true,
clearable: true,
escapeClearsValue: true,
filterOptions: filterOptions,
ignoreAccents: true,
joinValues: false,
matchPos: "any",
+1 -1
View File
@@ -134,7 +134,7 @@ declare namespace ReactSelect {
/**
* method to filter the options array
*/
filterOptions?: (options: Array<Option>, filter: string, currentValues: (string | number)[]) => Array<Option>;
filterOptions?: (options: Array<Option>, filter: string, currentValues: Array<Option>) => Array<Option>;
/**
* whether to strip diacritics when filtering
* @default true