Mentions提及

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

    基本使用

    基本使用

    Mentions提及 - 图2

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

    1. const { Option, getMentions } = Mentions;
    2. class App extends React.Component {
    3. handleReset = e => {
    4. e.preventDefault();
    5. this.props.form.resetFields();
    6. };
    7. handleSubmit = e => {
    8. e.preventDefault();
    9. this.props.form.validateFields((errors, values) => {
    10. if (errors) {
    11. console.log('Errors in the form!!!');
    12. return;
    13. }
    14. console.log('Submit!!!');
    15. console.log(values);
    16. });
    17. };
    18. checkMention = (rule, value, callback) => {
    19. const mentions = getMentions(value);
    20. if (mentions.length < 2) {
    21. callback(new Error('More than one must be selected!'));
    22. } else {
    23. callback();
    24. }
    25. };
    26. render() {
    27. const {
    28. form: { getFieldDecorator },
    29. } = this.props;
    30. return (
    31. <Form layout="horizontal">
    32. <Form.Item label="Top coders" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
    33. {getFieldDecorator('mention', {
    34. rules: [{ validator: this.checkMention }],
    35. <Mentions rows="3">
    36. <Option value="afc163">afc163</Option>
    37. <Option value="zombieJ">zombieJ</Option>
    38. <Option value="yesmeck">yesmeck</Option>
    39. </Mentions>,
    40. )}
    41. <Form.Item wrapperCol={{ span: 14, offset: 6 }}>
    42. <Button type="primary" onClick={this.handleSubmit}>
    43. Submit
    44. </Button>
    45. &nbsp;&nbsp;&nbsp;
    46. <Button onClick={this.handleReset}>Reset</Button>
    47. </Form.Item>
    48. </Form>
    49. );
    50. }
    51. }
    52. const FormDemo = Form.create()(App);
    53. ReactDOM.render(<FormDemo />, mountNode);

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

    Mentions提及 - 图4

    异步加载

    1. import { Mentions } from 'antd';
    2. import debounce from 'lodash/debounce';
    3. const { Option } = Mentions;
    4. class AsyncMention extends React.Component {
    5. constructor() {
    6. super();
    7. this.loadGithubUsers = debounce(this.loadGithubUsers, 800);
    8. }
    9. state = {
    10. search: '',
    11. loading: false,
    12. users: [],
    13. };
    14. onSearch = search => {
    15. this.setState({ search, loading: !!search, users: [] });
    16. console.log('Search:', search);
    17. this.loadGithubUsers(search);
    18. };
    19. loadGithubUsers(key) {
    20. if (!key) {
    21. users: [],
    22. });
    23. return;
    24. fetch(`https://api.github.com/search/users?q=${key}`)
    25. .then(res => res.json())
    26. .then(({ items = [] }) => {
    27. const { search } = this.state;
    28. if (search !== key) return;
    29. this.setState({
    30. users: items.slice(0, 10),
    31. loading: false,
    32. });
    33. });
    34. }
    35. render() {
    36. const { users, loading } = this.state;
    37. return (
    38. <Mentions style={{ width: '100%' }} loading={loading} onSearch={this.onSearch}>
    39. {users.map(({ login, avatar_url: avatar }) => (
    40. <Option key={login} value={login} className="antd-demo-dynamic-option">
    41. <img src={avatar} alt={login} />
    42. <span>{login}</span>
    43. </Option>
    44. ))}
    45. </Mentions>
    46. );
    47. }
    48. }
    49. ReactDOM.render(<AsyncMention />, mountNode);

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

    Mentions提及 - 图6

    向上展开

    向上展开建议。

    1. import { Mentions } from 'antd';
    2. const { Option } = Mentions;
    3. ReactDOM.render(
    4. <Mentions style={{ width: '100%' }} placement="top">
    5. <Option value="afc163">afc163</Option>
    6. <Option value="zombieJ">zombieJ</Option>
    7. <Option value="yesmeck">yesmeck</Option>
    8. </Mentions>,
    9. mountNode,
    10. );
    1. <Mentions onChange={onChange}>