Skip to content

Commit c182d2c

Browse files
committed
support placement
1 parent cb759fc commit c182d2c

File tree

5 files changed

+53
-16
lines changed

5 files changed

+53
-16
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ React.render(<Demo />, container);
6969
| defaultValue | Default value | string | - |
7070
| filterOption | Customize filter option logic | false \| (input: string, option: OptionProps) => boolean | - |
7171
| notFoundContent | Set mentions content when not match | ReactNode | 'Not Found' |
72+
| placement | Set popup placement | 'top' \| 'bottom' | 'bottom' |
7273
| prefix | Set trigger prefix keyword | string \| string[] | '@' |
7374
| rows | Set row count | number | 1 |
7475
| split | Set split string before and after selected mention | string | ' ' |

examples/textarea.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import React from 'react';
44
import Mentions from '../src';
55
import '../assets/index.less';
6+
import './textarea.less';
67

78
const { Option } = Mentions;
89

@@ -19,5 +20,13 @@ export default () => (
1920
<Option value="bamboo">Bamboo</Option>
2021
<Option value="cat">Cat</Option>
2122
</Mentions>
23+
24+
<div style={{ paddingTop: 200 }}>
25+
<Mentions placeholder="placement: top" placement="top" transitionName="motion">
26+
<Option value="light">Light</Option>
27+
<Option value="bamboo">Bamboo</Option>
28+
<Option value="cat">Cat</Option>
29+
</Mentions>
30+
</div>
2231
</div>
2332
);

examples/textarea.less

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.motion {
2+
transform: scale(.3);
3+
}

src/KeywordTrigger.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import * as React from 'react';
33
import DropdownMenu from './DropdownMenu';
44
import { OptionProps } from './Option';
55

6+
import { Placement } from './Mentions';
7+
68
const BUILT_IN_PLACEMENTS = {
79
bottomRight: {
810
points: ['tl', 'br'],
@@ -23,10 +25,12 @@ const BUILT_IN_PLACEMENTS = {
2325
};
2426

2527
interface KeywordTriggerProps {
26-
visible?: boolean;
2728
loading?: boolean;
28-
prefixCls?: string;
2929
options: OptionProps[];
30+
prefixCls?: string;
31+
placement?: Placement;
32+
visible?: boolean;
33+
transitionName?: string;
3034
}
3135

3236
class KeywordTrigger extends React.Component<KeywordTriggerProps, {}> {
@@ -38,7 +42,7 @@ class KeywordTrigger extends React.Component<KeywordTriggerProps, {}> {
3842
};
3943

4044
public render() {
41-
const { children, visible } = this.props;
45+
const { children, visible, placement, transitionName } = this.props;
4246

4347
const popupElement = this.getDropdownElement();
4448

@@ -47,7 +51,8 @@ class KeywordTrigger extends React.Component<KeywordTriggerProps, {}> {
4751
prefixCls={this.getDropdownPrefix()}
4852
popupVisible={visible}
4953
popup={popupElement}
50-
popupPlacement="bottomRight"
54+
popupPlacement={placement === 'top' ? 'topRight' : 'bottomRight'}
55+
popupTransitionName={transitionName}
5156
builtinPlacements={BUILT_IN_PLACEMENTS}
5257
>
5358
{children}

src/Mentions.tsx

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,27 @@ type BaseTextareaAttrs = Omit<
2222
'prefix' | 'onChange' | 'onSelect'
2323
>;
2424

25+
export type Placement = 'top' | 'bottom';
26+
2527
export interface MentionsProps extends BaseTextareaAttrs {
28+
autoFocus?: boolean;
29+
className?: string;
2630
defaultValue?: string;
31+
notFoundContent?: React.ReactNode;
32+
split?: string;
33+
style?: React.CSSProperties;
34+
transitionName?: string;
35+
placement?: Placement;
36+
prefix?: string | string[];
37+
prefixCls?: string;
2738
value?: string;
39+
filterOption?: false | typeof defaultFilterOption;
40+
validateSearch?: typeof defaultValidateSearch;
2841
onChange?: (text: string) => void;
2942
onSelect?: (option: OptionProps, prefix: string) => void;
3043
onSearch?: (text: string, prefix: string) => void;
3144
onFocus?: React.FocusEventHandler<HTMLTextAreaElement>;
3245
onBlur?: React.FocusEventHandler<HTMLTextAreaElement>;
33-
prefixCls?: string;
34-
prefix?: string | string[];
35-
className?: string;
36-
style?: React.CSSProperties;
37-
autoFocus?: boolean;
38-
split?: string;
39-
validateSearch?: typeof defaultValidateSearch;
40-
filterOption?: false | typeof defaultFilterOption;
41-
notFoundContent?: React.ReactNode;
4246
}
4347
interface MentionsState {
4448
value: string;
@@ -206,7 +210,7 @@ class Mentions extends React.Component<MentionsProps, MentionsState> {
206210
};
207211

208212
public onInputBlur: React.FocusEventHandler<HTMLTextAreaElement> = event => {
209-
this.onBlur(event);
213+
// this.onBlur(event);
210214
};
211215

212216
public onDropdownFocus = () => {
@@ -317,7 +321,16 @@ class Mentions extends React.Component<MentionsProps, MentionsState> {
317321

318322
public render() {
319323
const { value, measureLocation, measurePrefix, measuring, activeIndex } = this.state;
320-
const { prefixCls, className, style, autoFocus, notFoundContent, ...restProps } = this.props;
324+
const {
325+
prefixCls,
326+
placement,
327+
transitionName,
328+
className,
329+
style,
330+
autoFocus,
331+
notFoundContent,
332+
...restProps
333+
} = this.props;
321334

322335
const inputProps = omit(
323336
restProps,
@@ -359,7 +372,13 @@ class Mentions extends React.Component<MentionsProps, MentionsState> {
359372
onFocus: this.onDropdownFocus,
360373
}}
361374
>
362-
<KeywordTrigger prefixCls={prefixCls} options={options} visible={true}>
375+
<KeywordTrigger
376+
prefixCls={prefixCls}
377+
transitionName={transitionName}
378+
placement={placement}
379+
options={options}
380+
visible={true}
381+
>
363382
<span>{measurePrefix}</span>
364383
</KeywordTrigger>
365384
</MentionsContextProvider>

0 commit comments

Comments
 (0)