Skip to content

Commit ecfde32

Browse files
committed
add customize filter demo
1 parent 9d64515 commit ecfde32

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

examples/filter.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* eslint no-console: 0 */
2+
3+
import React from 'react';
4+
import Mentions from '../src';
5+
import '../assets/index.less';
6+
7+
const { Option } = Mentions;
8+
9+
function filterOption(input, { id }) {
10+
return id.indexOf(input) !== -1;
11+
}
12+
13+
class Demo extends React.Component {
14+
state = {};
15+
16+
render() {
17+
return (
18+
<div>
19+
<h1>Customize Filter</h1>
20+
<p>Option has `id` and filter only hit by `id`</p>
21+
<Mentions style={{ width: '100%', fontSize: 30 }} filterOption={filterOption} autoFocus>
22+
<Option value="light" id="1128">
23+
Light (ID: 1128)
24+
</Option>
25+
<Option value="bamboo" id="903">
26+
Bamboo (ID: 903)
27+
</Option>
28+
<Option value="cat" id="1706">
29+
Cat (ID: 1706)
30+
</Option>
31+
</Mentions>
32+
</div>
33+
);
34+
}
35+
}
36+
37+
export default Demo;

src/Mentions.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import KeywordTrigger from './KeywordTrigger';
88
import { MentionsContextProvider } from './MentionsContext';
99
import Option, { OptionProps } from './Option';
1010
import {
11+
filterOption as defaultFilterOption,
1112
getBeforeSelectionText,
1213
getLastMeasureIndex,
1314
replaceWithMeasure,
@@ -27,6 +28,7 @@ export interface MentionsProps {
2728
autoFocus?: boolean;
2829
split?: string;
2930
validateSearch?: typeof defaultValidateSearch;
31+
filterOption?: false | typeof defaultFilterOption;
3032
}
3133
interface MentionsState {
3234
value: string;
@@ -44,6 +46,7 @@ class Mentions extends React.Component<MentionsProps, MentionsState> {
4446
prefix: '@',
4547
split: ' ',
4648
validateSearch: defaultValidateSearch,
49+
filterOption: defaultFilterOption,
4750
};
4851

4952
public static getDerivedStateFromProps(props: MentionsProps, prevState: MentionsState) {
@@ -217,12 +220,16 @@ class Mentions extends React.Component<MentionsProps, MentionsState> {
217220
};
218221

219222
public getOptions = (measureText?: string): OptionProps[] => {
220-
const targetMeasureText = (measureText || this.state.measureText || '').toLocaleLowerCase();
221-
const { children } = this.props;
223+
const targetMeasureText = measureText || this.state.measureText || '';
224+
const { children, filterOption } = this.props;
222225
const list = toArray(children)
223226
.map(({ props }: { props: OptionProps }) => props)
224-
.filter(({ value = '' }: OptionProps) => {
225-
return value.toLowerCase().indexOf(targetMeasureText) !== -1;
227+
.filter((option: OptionProps) => {
228+
/** Return all result if `filterOption` is false. */
229+
if (filterOption === false) {
230+
return true;
231+
}
232+
return filterOption!(targetMeasureText, option);
226233
});
227234
return list;
228235
};

src/util.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { MentionsProps } from './Mentions';
2+
import { OptionProps } from './Option';
23

34
/**
45
* Cut input selection into 2 part and return text before selection start
@@ -116,3 +117,8 @@ export function validateSearch(text: string, props: MentionsProps) {
116117
const { split } = props;
117118
return !split || text.indexOf(split) === -1;
118119
}
120+
121+
export function filterOption(input: string, { value = '' }: OptionProps): boolean {
122+
const lowerCase = input.toLowerCase();
123+
return value.toLowerCase().indexOf(lowerCase) !== -1;
124+
}

storybook/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import { withViewport } from '@storybook/addon-viewport';
88
import { withInfo } from '@storybook/addon-info';
99
import BasicSource from 'rc-source-loader!../examples/basic';
1010
import DynamicSource from 'rc-source-loader!../examples/dynamic';
11+
import FilterSource from 'rc-source-loader!../examples/filter';
1112
import MultiplePrefixSource from 'rc-source-loader!../examples/multiple-prefix';
1213
import SplitSource from 'rc-source-loader!../examples/split';
1314
import Basic from '../examples/basic';
1415
import Dynamic from '../examples/dynamic';
16+
import Filter from '../examples/filter';
1517
import MultiplePrefix from '../examples/multiple-prefix';
1618
import Split from '../examples/split';
1719
import READMECode from '../README.md';
@@ -49,6 +51,11 @@ storiesOf('rc-mentions', module)
4951
code: DynamicSource,
5052
},
5153
})
54+
.add('filter', () => <Filter />, {
55+
source: {
56+
code: FilterSource,
57+
},
58+
})
5259
.add('multiple-prefix', () => <MultiplePrefix />, {
5360
source: {
5461
code: MultiplePrefixSource,

0 commit comments

Comments
 (0)