Mention 提及

提及组件。

何时使用

用于在输入中提及某人或某事,常用于发布、聊天或评论功能。

代码演示

基本使用

基本使用

Mention提及 - 图1

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Mention } from 'choerodon-ui';
  4. const { toString, toContentState } = Mention;
  5. function onChange(contentState) {
  6. console.log(toString(contentState));
  7. }
  8. function onSelect(suggestion) {
  9. console.log('onSelect', suggestion);
  10. }
  11. ReactDOM.render(
  12. <Mention
  13. style={{ width: '100%' }}
  14. onChange={onChange}
  15. defaultValue={toContentState('@afc163')}
  16. suggestions={['afc163', 'benjycui', 'yiminghe', 'RaoHai', '中文', 'にほんご']}
  17. onSelect={onSelect}
  18. />,
  19. document.getElementById('container'),
  20. );

向上展开

向上展开建议。

Mention提及 - 图2

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Mention } from 'choerodon-ui';
  4. const { toString } = Mention;
  5. function onChange(contentState) {
  6. console.log(toString(contentState));
  7. }
  8. function onSelect(suggestion) {
  9. console.log('onSelect', suggestion);
  10. }
  11. ReactDOM.render(
  12. <Mention
  13. style={{ width: '100%' }}
  14. onChange={onChange}
  15. suggestions={['afc163', 'benjycui', 'yiminghe', 'RaoHai', '中文', 'にほんご']}
  16. onSelect={onSelect}
  17. placement="top"
  18. />,
  19. document.getElementById('container'),
  20. );

异步加载

匹配内容列表为异步返回时。

Mention提及 - 图3

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Mention } from 'choerodon-ui';
  4. const users = ['afc163', 'benjycui', 'yiminghe', 'jljsj33', 'dqaria', 'RaoHai'];
  5. class AsyncMention extends React.Component {
  6. state = {
  7. suggestions: [],
  8. loading: false,
  9. }
  10. fetchSuggestions = (value, callback) => {
  11. setTimeout(() => {
  12. callback(users.filter(item => item.indexOf(value) !== -1));
  13. }, 500);
  14. }
  15. onSearchChange = (value) => {
  16. this.fetchSuggestions(value, (suggestions) => {
  17. this.setState({
  18. suggestions,
  19. loading: false,
  20. });
  21. });
  22. this.setState({
  23. loading: true,
  24. });
  25. }

自定义建议

自定义建议

注意,自定义建议时,onSearchChange 必须不能为空。

Mention提及 - 图4

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Mention } from 'choerodon-ui';
  4. const Nav = Mention.Nav;
  5. const webFrameworks = [
  6. { name: 'React', type: 'JavaScript' },
  7. { name: 'Angular', type: 'JavaScript' },
  8. { name: 'Laravel', type: 'PHP', disabled: true },
  9. { name: 'Flask', type: 'Python' },
  10. { name: 'Django', type: 'Python' },
  11. ];
  12. function onSelect(suggestion, data) {
  13. console.log('onSelect', suggestion, data);
  14. }
  15. class CustomNavMention extends React.Component {
  16. state = {
  17. suggestions: [],
  18. };
  19. onSearchChange = value => {
  20. const searchValue = value.toLowerCase();
  21. const filtered = webFrameworks.filter(
  22. item => item.name.toLowerCase().indexOf(searchValue) !== -1,
  23. );
  24. const suggestions = filtered.map(suggestion => (
  25. <Nav key={suggestion.name} value={suggestion.name} data={suggestion}>

头像

自定义建议(含头像)

注意,自定义建议时,onSearchChange 必须不能为空。

Mention提及 - 图5

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Mention, Avatar } from 'choerodon-ui';
  4. const Nav = Mention.Nav;
  5. const webFrameworks = [
  6. {
  7. name: 'React',
  8. type: 'JavaScript',
  9. icon: 'https://zos.alipayobjects.com/rmsportal/LFIeMPzdLcLnEUe.svg',
  10. },
  11. {
  12. name: 'Angular',
  13. type: 'JavaScript',
  14. icon: 'https://zos.alipayobjects.com/rmsportal/PJTbxSvzYWjDZnJ.png',
  15. },
  16. {
  17. name: 'Dva',
  18. type: 'Javascript',
  19. icon: 'https://zos.alipayobjects.com/rmsportal/EYPwSeEJKxDtVxI.png',
  20. },
  21. {
  22. name: 'Flask',
  23. type: 'Python',
  24. icon: 'https://zos.alipayobjects.com/rmsportal/xaypBUijfnpAlXE.png',
  25. },
  26. ];

受控模式

受控模式.

Mention提及 - 图6

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Mention } from 'choerodon-ui';
  4. const { toContentState } = Mention;
  5. class App extends React.Component {
  6. state = {
  7. value: toContentState('@afc163'),
  8. };
  9. componentDidMount() {
  10. this.mention.focus();
  11. }
  12. handleChange = editorState => {
  13. this.setState({
  14. value: editorState,
  15. });
  16. };
  17. render() {
  18. return (
  19. <Mention
  20. ref={ele => (this.mention = ele)}
  21. suggestions={['afc163', 'benjycui', 'yiminghe', 'RaoHai', '中文', 'にほんご']}
  22. value={this.state.value}
  23. onChange={this.handleChange}

配合 Form 使用

受控模式,例如配合 Form 使用。

Mention提及 - 图7

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Mention, Form, Button } from 'choerodon-ui';
  4. const { toContentState, getMentions } = Mention;
  5. const FormItem = Form.Item;
  6. class App extends React.Component {
  7. state = {
  8. initValue: toContentState('@afc163'),
  9. };
  10. handleReset = e => {
  11. e.preventDefault();
  12. this.props.form.resetFields();
  13. };
  14. handleSubmit = e => {
  15. e.preventDefault();
  16. this.props.form.validateFields((errors, values) => {
  17. if (errors) {
  18. console.log('Errors in form!!!');
  19. return;
  20. }
  21. console.log('Submit!!!');
  22. console.log(values);
  23. });
  24. };
  25. checkMention = (rule, value, callback) => {
  26. const {
  27. form: { getFieldValue },
  28. } = this.props;

多行

多行模式,多行模式必须指定高度。

Mention提及 - 图8

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Mention } from 'choerodon-ui';
  4. const { toString } = Mention;
  5. function onChange(editorState) {
  6. console.log(toString(editorState));
  7. }
  8. ReactDOM.render(
  9. <Mention
  10. style={{ width: '100%', height: 100 }}
  11. onChange={onChange}
  12. suggestions={['afc163', 'benjycui', 'yiminghe', 'jljsj33', 'dqaria', 'RaoHai']}
  13. multiLines
  14. />,
  15. document.getElementById('container'),
  16. );

建议渲染父节点

指定提示渲染的父节点。

Mention提及 - 图9

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Mention, Popover, Button } from 'choerodon-ui';
  4. const { toString, toContentState } = Mention;
  5. function onChange(editorState) {
  6. console.log(toString(editorState));
  7. }
  8. function onSelect(suggestion) {
  9. console.log('onSelect', suggestion);
  10. }
  11. class PopoverContainer extends React.Component {
  12. getSuggestionContainer = () => {
  13. return this.popover.getPopupDomNode();
  14. };
  15. visibleChange = visible => {
  16. if (visible && this.mention) {
  17. this.mention.focus();
  18. }
  19. };
  20. render() {
  21. const mention = (
  22. <Mention

无效或只读

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

Mention提及 - 图10

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Mention } from 'choerodon-ui';
  4. const { toString } = Mention;
  5. function onChange(editorState) {
  6. console.log(toString(editorState));
  7. }
  8. const users = ['afc163', 'benjycui', 'yiminghe', 'jljsj33', 'dqaria', 'RaoHai'];
  9. function App() {
  10. return (
  11. <div>
  12. <div style={{ marginBottom: 10 }}>
  13. <Mention
  14. style={{ width: '100%' }}
  15. onChange={onChange}
  16. placeholder="this is disabled Mention"
  17. suggestions={users}
  18. disabled
  19. />
  20. </div>
  21. <Mention
  22. style={{ width: '100%' }}
  23. onChange={onChange}
  24. placeholder="this is readOnly Mention"
  25. suggestions={users}
  26. readOnly
  27. />
  28. </div>

自定义触发字符

通过 prefix 属性自定义触发字符。默认为 @, 可以定义为数组。

Mention提及 - 图11

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Mention } from 'choerodon-ui';
  4. const { toString } = Mention;
  5. function onChange(editorState) {
  6. console.log(toString(editorState));
  7. }
  8. function onSelect(suggestion) {
  9. console.log('onSelect', suggestion);
  10. }
  11. const users = ['afc163', 'benjycui', 'yiminghe', 'jljsj33', 'dqaria', 'RaoHai'];
  12. const tags = ['1.0', '2.0', '3.0'];
  13. class App extends React.Component {
  14. constructor() {
  15. super();
  16. this.state = {
  17. suggestions: [],
  18. };
  19. }
  20. onSearchChange = (value, trigger) => {
  21. console.log('onSearchChange', value, trigger);
  22. const dataSource = trigger === '@' ? users : tags;
  23. this.setState({
  24. suggestions: dataSource.filter(item => item.indexOf(value) !== -1),

API

  1. <Mention
  2. onChange={onChange}
  3. suggestions={['afc163', 'benjycui', 'yiminghe', 'jljsj33', 'dqaria', 'RaoHai']}
  4. />

Mention API

API说明类型
getMentions获取当前 contentState 中提到的人的列表Function(contentState: ContentState): string[]
toContentState把字符串转成 ContentStateFunction(value: string): ContentState
toString把 ContentState 转成字符串Function(contentState: ContentState): string

Mention

参数说明类型默认值
autoFocus自动获取焦点booleanfalse
defaultValue默认值ContentState, 可以用 Mention.toContentState(text) 把文字转换成 ContentStatenull
disabled是否禁用状态.booleanfalse
getSuggestionContainer菜单渲染父节点。默认渲染到 body 上,如果你遇到菜单滚动定位问题,试试修改为滚动的区域,并相对其定位function()() => document.body
loading加载中booleanfalse
multiLines多行模式booleanfalse
notFoundContent未找到时的内容string‘无匹配结果,轻敲空格完成输入’
placeholder输入框默认文字stringnull
placement建议框位置,可选 top bottomstring‘bottom’
prefix触发弹出下拉框的字符string or Array‘@’
readOnly是否只读.booleanfalse
suggestions建议内容Array<string|Mention.Nav>[]
suggestionStyle弹出下拉框样式object{}
valueContentStatenull
onBlur失去焦点时回调function(e)null
onChange输入框内容变化时回调function(contentState: ContentState)null
onFocus获得焦点时回调function(e)null
onSearchChange输入框中 @ 变化时回调function(value:string, trigger: string)[]
onSelect下拉框选择建议时回调function(suggestion: string, data?: any)null

Mention 方法

名称描述
blur()移除焦点
focus()获取焦点

Nav

参数说明类型默认值
children建议内容object{}
value建议值,选择建议时,用此值插入到输入框中string“”