Mentions
Mention component.
When To Use#
When you need to mention someone or something.
Examples
import { Mentions } from 'antd';
const { Option } = Mentions;
function onChange(value) {
console.log('Change:', value);
}
function onSelect(option) {
console.log('select', option);
}
ReactDOM.render(
<Mentions
style={{ width: '100%' }}
onChange={onChange}
onSelect={onSelect}
defaultValue="@afc163"
>
<Option value="afc163">afc163</Option>
<Option value="zombieJ">zombieJ</Option>
<Option value="yesmeck">yesmeck</Option>
</Mentions>,
mountNode,
);
TypeScript
JavaScript
import { Mentions, Form, Button } from 'antd';
const { Option, getMentions } = Mentions;
const App = () => {
const [form] = Form.useForm();
const onReset = () => {
form.resetFields();
};
const onFinish = async () => {
try {
const values = await form.validateFields();
console.log('Submit:', values);
} catch (errInfo) {
console.log('Error:', errInfo);
}
};
const checkMention = async (_: any, value: string) => {
const mentions = getMentions(value);
if (mentions.length < 2) {
throw new Error('More than one must be selected!');
}
};
return (
<Form form={form} layout="horizontal" onFinish={onFinish}>
<Form.Item
name="coders"
label="Top coders"
labelCol={{ span: 6 }}
wrapperCol={{ span: 16 }}
rules={[{ validator: checkMention }]}
>
<Mentions rows={1}>
<Option value="afc163">afc163</Option>
<Option value="zombieJ">zombieJ</Option>
<Option value="yesmeck">yesmeck</Option>
</Mentions>
</Form.Item>
<Form.Item
name="bio"
label="Bio"
labelCol={{ span: 6 }}
wrapperCol={{ span: 16 }}
rules={[{ required: true }]}
>
<Mentions rows={3} placeholder="You can use @ to ref user here">
<Option value="afc163">afc163</Option>
<Option value="zombieJ">zombieJ</Option>
<Option value="yesmeck">yesmeck</Option>
</Mentions>
</Form.Item>
<Form.Item wrapperCol={{ span: 14, offset: 6 }}>
<Button htmlType="submit" type="primary">
Submit
</Button>
<Button htmlType="button" onClick={onReset}>
Reset
</Button>
</Form.Item>
</Form>
);
};
ReactDOM.render(<App />, mountNode);
import { Mentions } from 'antd';
const { Option } = Mentions;
function getOptions() {
return ['afc163', 'zombiej', 'yesmeck'].map(value => (
<Option key={value} value={value}>
{value}
</Option>
));
}
function App() {
return (
<>
<div style={{ marginBottom: 10 }}>
<Mentions style={{ width: '100%' }} placeholder="this is disabled Mentions" disabled>
{getOptions()}
</Mentions>
</div>
<Mentions style={{ width: '100%' }} placeholder="this is readOnly Mentions" readOnly>
{getOptions()}
</Mentions>
</>
);
}
ReactDOM.render(<App />, mountNode);
import { Mentions } from 'antd';
const { Option } = Mentions;
ReactDOM.render(
<Mentions autoSize style={{ width: '100%' }}>
<Option value="afc163">afc163</Option>
<Option value="zombieJ">zombieJ</Option>
<Option value="yesmeck">yesmeck</Option>
</Mentions>,
mountNode,
);
import { Mentions } from 'antd';
import debounce from 'lodash/debounce';
const { Option } = Mentions;
class AsyncMention extends React.Component {
constructor() {
super();
this.loadGithubUsers = debounce(this.loadGithubUsers, 800);
}
state = {
search: '',
loading: false,
users: [],
};
onSearch = search => {
this.setState({ search, loading: !!search, users: [] });
console.log('Search:', search);
this.loadGithubUsers(search);
};
loadGithubUsers(key) {
if (!key) {
this.setState({
users: [],
});
return;
}
fetch(`https://api.github.com/search/users?q=${key}`)
.then(res => res.json())
.then(({ items = [] }) => {
const { search } = this.state;
if (search !== key) return;
this.setState({
users: items.slice(0, 10),
loading: false,
});
});
}
render() {
const { users, loading } = this.state;
return (
<Mentions style={{ width: '100%' }} loading={loading} onSearch={this.onSearch}>
{users.map(({ login, avatar_url: avatar }) => (
<Option key={login} value={login} className="antd-demo-dynamic-option">
<img src={avatar} alt={login} />
<span>{login}</span>
</Option>
))}
</Mentions>
);
}
}
ReactDOM.render(<AsyncMention />, mountNode);
import { Mentions } from 'antd';
const { Option } = Mentions;
const MOCK_DATA = {
'@': ['afc163', 'zombiej', 'yesmeck'],
'#': ['1.0', '2.0', '3.0'],
};
class App extends React.Component {
state = {
prefix: '@',
};
onSearch = (_, prefix) => {
this.setState({ prefix });
};
render() {
const { prefix } = this.state;
return (
<Mentions
style={{ width: '100%' }}
placeholder="input @ to mention people, # to mention tag"
prefix={['@', '#']}
onSearch={this.onSearch}
>
{(MOCK_DATA[prefix] || []).map(value => (
<Option key={value} value={value}>
{value}
</Option>
))}
</Mentions>
);
}
}
ReactDOM.render(<App />, mountNode);
import { Mentions } from 'antd';
const { Option } = Mentions;
ReactDOM.render(
<Mentions style={{ width: '100%' }} placement="top">
<Option value="afc163">afc163</Option>
<Option value="zombieJ">zombieJ</Option>
<Option value="yesmeck">yesmeck</Option>
</Mentions>,
mountNode,
);
API#
<Mentions onChange={onChange}>
<Mentions.Option value="sample">Sample</Mentions.Option>
</Mentions>
Mention#
Property | Description | Type | Default |
---|---|---|---|
autoFocus | Auto get focus when component mounted | boolean | false |
autoSize | Textarea height autosize feature, can be set to true | false or an object { minRows: 2, maxRows: 6 } | boolean | object | false |
defaultValue | Default value | string | - |
filterOption | Customize filter option logic | false | (input: string, option: OptionProps) => boolean | - |
getPopupContainer | Set the mount HTML node for suggestions | () => HTMLElement | - |
notFoundContent | Set mentions content when not match | ReactNode | Not Found |
placement | Set popup placement | top | bottom | bottom |
prefix | Set trigger prefix keyword | string | string[] | @ |
split | Set split string before and after selected mention | string |
|
validateSearch | Customize trigger search logic | (text: string, props: MentionsProps) => void | - |
value | Set value of mentions | string | - |
onBlur | Trigger when mentions lose focus | () => void | - |
onChange | Trigger when value changed | (text: string) => void | - |
onFocus | Trigger when mentions get focus | () => void | - |
onResize | The callback function that is triggered when textarea resize | function({ width, height }) | - |
onSearch | Trigger when prefix hit | (text: string, prefix: string) => void | - |
onSelect | Trigger when user select the option | (option: OptionProps, prefix: string) => void | - |
Mention methods#
Name | Description |
---|---|
blur() | Remove focus |
focus() | Get focus |
Option#
Property | Description | Type | Default |
---|---|---|---|
children | Suggestion content | ReactNode | - |
value | The value of suggestion, the value will insert into input filed while selected | string | - |