Skip to content

Commit

Permalink
Add focus method of <Mention />
Browse files Browse the repository at this point in the history
  • Loading branch information
RaoHai committed May 15, 2017
1 parent 61d6cf1 commit 29bc4a6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions components/mention/demo/controllder-simple.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class App extends React.Component {
state = {
value: toContentState('@afc163'),
}
componentDidMount() {
this.mention.focus();
}
handleChange = (editorState) => {
this.setState({
value: editorState,
Expand All @@ -29,6 +32,7 @@ class App extends React.Component {
render() {
return (
<Mention
ref={ele => this.mention = ele}
suggestions={['afc163', 'benjycui', 'yiminghe', 'RaoHai', '中文', 'にほんご']}
value={this.state.value}
onChange={this.handleChange}
Expand Down
7 changes: 7 additions & 0 deletions components/mention/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ When need to mention someone or something.
| readOnly | Tell if the input is readonly. | boolean | false |
| disabled | Tell if the input is disabled. | boolean | false |


### Mention methods

| Property | Description | Type | Default |
|----------|---------------|----------|--------------|
| focus | Force focus back onto the editor node. | - | - |

### Nav props

| Property | Description | Type | Default |
Expand Down
13 changes: 13 additions & 0 deletions components/mention/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface MentionProps {
onBlur?: Function;
readOnly?: boolean;
disabled?: boolean;
ref?: Function;
}

export interface MentionState {
Expand All @@ -46,6 +47,7 @@ export default class Mention extends React.Component<MentionProps, MentionState>
console.warn('Mention.toEditorState is deprecated. Use toContentState instead.');
return toEditorState(text);
}
private mentionEle: any;
constructor(props) {
super(props);
this.state = {
Expand Down Expand Up @@ -101,6 +103,16 @@ export default class Mention extends React.Component<MentionProps, MentionState>
this.props.onBlur(ev);
}
}
focus = () => {
this.mentionEle._editor.focus();
}
mentionRef = ele => {
const { ref } = this.props;
if (ref) {
ref(ele);
}
this.mentionEle = ele;
}
render() {
const { className = '', prefixCls, loading } = this.props;
const { suggestions, focus } = this.state;
Expand All @@ -116,6 +128,7 @@ export default class Mention extends React.Component<MentionProps, MentionState>
<RcMention
{...this.props}
className={cls}
ref={this.mentionRef}
onSearchChange={this.onSearchChange}
onChange={this.onChange}
onFocus={this.onFocus}
Expand Down
7 changes: 7 additions & 0 deletions components/mention/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ title: Mention
| readOnly | 是否只读. | boolean | false |
| disabled | 是否禁用状态. | boolean | false |


### Mention methods

| Property | Description | Type | Default |
|----------|---------------|----------|--------------|
| focus | 强制获得焦点. | - | - |

### Nav props

| 参数 | 说明 | 类型 | 默认值 |
Expand Down

0 comments on commit 29bc4a6

Please sign in to comment.