Skip to content

Commit

Permalink
feat: support Mention[disabled|readOnly] and add some warning(#6004)
Browse files Browse the repository at this point in the history
#### FEATURES

 * use `ContentState` instead of `EditorState`, selection events won't trigger
   onChange anymore.
 * deprecated `Mention.toEditorState`, instead of `Mention.toContentState`,
 * `Mention.toEditorState` are compatibled, but have a warnning in console.

 #### ISSUES

 * `disabled` and `readOnly` props supported.
 * fixed controlled mode bug.

 * close #5788
 * close #5175
 * close #react-component/editor-mention#7
  • Loading branch information
RaoHai authored and benjycui committed May 5, 2017
1 parent 0d672d5 commit 4518b02
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 27 deletions.
8 changes: 4 additions & 4 deletions components/mention/demo/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ Basic usage.

````jsx
import { Mention } from 'antd';
const { toString, toEditorState } = Mention;
const { toString, toContentState } = Mention;

function onChange(editorState) {
console.log(toString(editorState));
function onChange(contentState) {
console.log(toString(contentState));
}

function onSelect(suggestion) {
Expand All @@ -29,7 +29,7 @@ ReactDOM.render(
<Mention
style={{ width: '100%', height: 100 }}
onChange={onChange}
defaultValue={toEditorState('@afc163')}
defaultValue={toContentState('@afc163')}
suggestions={['afc163', 'benjycui', 'yiminghe', 'RaoHai', '中文', 'にほんご']}
onSelect={onSelect}
/>
Expand Down
4 changes: 2 additions & 2 deletions components/mention/demo/controllder-simple.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ Controlled mode.

````jsx
import { Mention } from 'antd';
const { toEditorState } = Mention;
const { toContentState } = Mention;

class App extends React.Component {
state = {
value: toEditorState('@afc163'),
value: toContentState('@afc163'),
}
handleChange = (editorState) => {
this.setState({
Expand Down
4 changes: 2 additions & 2 deletions components/mention/demo/controlled.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ Controlled mode, for example, to work with `Form`.

````jsx
import { Mention, Form, Button } from 'antd';
const { toEditorState, getMentions } = Mention;
const { toContentState, getMentions } = Mention;
const FormItem = Form.Item;

class App extends React.Component {
state = {
initValue: toEditorState('@afc163'),
initValue: toContentState('@afc163'),
}
handleReset = (e) => {
e.preventDefault();
Expand Down
4 changes: 2 additions & 2 deletions components/mention/demo/popupContainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ To set the container of the suggestion.

````jsx
import { Mention, Popover, Button } from 'antd';
const { toString, toEditorState } = Mention;
const { toString, toContentState } = Mention;

function onChange(editorState) {
console.log(toString(editorState));
Expand All @@ -34,7 +34,7 @@ class PopoverContainer extends React.Component {
<Mention
style={{ width: '100%', height: 100 }}
onChange={onChange}
defaultValue={toEditorState('@afc163')}
defaultValue={toContentState('@afc163')}
suggestions={['afc163', 'benjycui', 'yiminghe', 'RaoHai', '中文', 'にほんご']}
onSelect={onSelect}
getSuggestionContainer={this.getSuggestionContainer}
Expand Down
53 changes: 53 additions & 0 deletions components/mention/demo/readonly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
order: 7
title:
zh-CN: 无效或只读
en-US: disabled or readOnly
---

## zh-CN

通过 `disabled` 属性设置是否生效。通过 `readOnly` 属性设置是否只读。

## en-US

Configurate `disabled` and `readOnly`.

````jsx
import { Mention } from 'antd';
const { toString } = Mention;

function onChange(editorState) {
console.log(toString(editorState));
}

const users = ['afc163', 'benjycui', 'yiminghe', 'jljsj33', 'dqaria', 'RaoHai'];

function App() {
return (
<div>
<div style={{ marginBottom: 10 }}>
<Mention
style={{ width: '100%' }}
onChange={onChange}
placeholder="this is disabled Mention"
suggestions={users}
disabled
/>
</div>
<Mention
style={{ width: '100%' }}
onChange={onChange}
placeholder="this is readOnly Mention"
suggestions={users}
readOnly
/>
</div>
);
}


ReactDOM.render(
<App />
, mountNode);
````
15 changes: 9 additions & 6 deletions components/mention/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ When need to mention someone or something.

| API | Description | Type |
|----------|---------------|----------|--------------|
| toString | convert EditorState to string | Function(editorState: EditorState): string |
| toEditorState | convert string to EditorState | Function(value: string): EditorState |
| getMentions | get mentioned people in current editorState | Function(editorState: EditorState): string[] |
| toString | convert ContentState to string | Function(contentState: ContentState): string |
| toContentState (recommended) | convert string to ContentState | Function(value: string): ContentState |
| toEditorState (Backward compatible) | convert string to ContentState | Function(value: string): ContentState |
| getMentions | get mentioned people in current contentState | Function(ContentState: contentState): string[] |


### Mention props
Expand All @@ -35,18 +36,20 @@ When need to mention someone or something.
| suggestions | suggestion content | Array<string\|Mention.Nav> | [] |
| suggestionStyle | style of suggestion container | object | {} |
| onSearchChange | Callback function called when search content changes | function(value:string, trigger: string) | [] |
| onChange | Callback function called when content of input changes | function(editorState: EditorState) | null |
| onChange | Callback function called when content of input changes | function(contentState: ContentState) | null |
| onSelect | Callback function called when select from suggestions | function(suggestion: string, data?: any) | null |
| notFoundContent| suggestion when suggestions empty | string | '无匹配结果,轻敲空格完成输入' |
| loading | loading mode | boolean | false |
| multiLines | multilines mode | boolean | false |
| prefix | character which will trigger Mention to show mention list | string or Array<string> | '@' |
| defaultValue | default value | EditorState, you can use `Mention.toEditorState` to convert text to `EditorState` | null |
| value | core state of mention | EditorState | null |
| defaultValue | default value | ContentState, you can use `Mention.toContentState` or `Mention.toEditorState` to convert text to `ContentState` | null |
| value | core state of mention | ContentState | null |
| placeholder | placeholder of input | string | null |
| getSuggestionContainer | rendered to the root of the menu. Default rendered to the body dom. If gets any problem of the menu while scrolling. Try to make the root the dom scrolled, and make it position relative. | function | () => document.body |
| onFocus | Callback function called when mention component get focus | functione) | null |
| onBlur | Callback function called when mention component blur | function(e) | null |
| readOnly | Tell if the input is readonly. | boolean | false |
| disabled | Tell if the input is disabled. | boolean | false |

### Nav props

Expand Down
12 changes: 9 additions & 3 deletions components/mention/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface MentionProps {
getSuggestionContainer?: (triggerNode: Element) => HTMLElement;
onFocus?: Function;
onBlur?: Function;
readOnly?: boolean;
disabled?: boolean;
}

export interface MentionState {
Expand All @@ -30,16 +32,20 @@ export interface MentionState {
}

export default class Mention extends React.Component<MentionProps, MentionState> {
static Nav = Nav;
static toString = toString;
static toEditorState = toEditorState;
static getMentions = getMentions;
static defaultProps = {
prefixCls: 'ant-mention',
notFoundContent: '无匹配结果,轻敲空格完成输入',
loading: false,
multiLines: false,
};
static Nav = Nav;
static toString = toString;
static toContentState = toEditorState;
static toEditorState = text => {
console.warn('Mention.toEditorState is deprecated. Use toContentState instead.');
return toEditorState(text);
}
constructor(props) {
super(props);
this.state = {
Expand Down
15 changes: 9 additions & 6 deletions components/mention/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ title: Mention

| API | 说明 | 类型 |
|----------|---------------|----------|
| toString | 把 EditorState 转成字符串 | Function(editorState: EditorState): string |
| toEditorState | 把字符串转成 EditorState | Function(value: string): EditorState |
| getMentions | 获取当前 editorState 中提到的人的列表 | Function(editorState: EditorState): string[] |
| toString | 把 ContentState 转成字符串 | Function(contentState: ContentState): string |
| toContentState(推荐) | 把字符串转成 ContentState | Function(value: string): ContentState |
| toEditorState(兼容) | 把字符串转成 ContentState | Function(value: string): ContentState |
| getMentions | 获取当前 contentState 中提到的人的列表 | Function(contentState: ContentState): string[] |

### Mention props

Expand All @@ -35,18 +36,20 @@ title: Mention
| suggestions | 建议内容 | Array<string\|Mention.Nav> | [] |
| suggestionStyle | 弹出下拉框样式 | object | {} |
| onSearchChange | 输入框中 @ 变化时回调 | function(value:string, trigger: string) | [] |
| onChange | 输入框内容变化时回调 | function(editorState: EditorState) | null |
| onChange | 输入框内容变化时回调 | function(contentState: ContentState) | null |
| onSelect | 下拉框选择建议时回调 | function(suggestion: string, data?: any) | null |
| notFoundContent| 未找到时的内容 | string | '无匹配结果,轻敲空格完成输入' |
| loading | 加载中 | boolean | false |
| multiLines | 多行模式 | boolean | false |
| prefix | 触发弹出下拉框的字符 | string or Array<string> | '@' |
| placeholder | 输入框默认文字 | string | null |
| defaultValue | 默认值 | EditorState, 可以用 Mention.toEditorState(text) 把文字转换成 EditorState | null |
| value || EditorState | null |
| defaultValue | 默认值 | ContentState, 可以用 `Mention.toContentState(text)``Mention.toEditorState(text)` 把文字转换成 ContentState | null |
| value || ContentState | null |
| getSuggestionContainer | 菜单渲染父节点。默认渲染到 body 上,如果你遇到菜单滚动定位问题,试试修改为滚动的区域,并相对其定位| function() | () => document.body |
| onFocus | 获得焦点时回调 | function(e) | null |
| onBlur | 失去焦点时回调 | function(e) | null |
| readOnly | 是否只读. | boolean | false |
| disabled | 是否禁用状态. | boolean | false |

### Nav props

Expand Down
5 changes: 4 additions & 1 deletion components/mention/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
height: auto;
}
}
&.@{mention-prefix-cls}-active .@{mention-prefix-cls}-editor {
&.@{mention-prefix-cls}-active:not(.disabled) .@{mention-prefix-cls}-editor {
.active;
}
&.disabled .@{mention-prefix-cls}-editor {
.disabled();
}
.public-DraftEditorPlaceholder-root {
position: absolute;
.public-DraftEditorPlaceholder-inner {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"rc-collapse": "~1.7.0",
"rc-dialog": "~6.5.0",
"rc-dropdown": "~1.4.8",
"rc-editor-mention": "~0.5.2",
"rc-editor-mention": "~0.6.11",
"rc-form": "~1.3.0",
"rc-input-number": "~3.4.4",
"rc-menu": "~5.0.9",
Expand Down

0 comments on commit 4518b02

Please sign in to comment.