Skip to content

Commit

Permalink
feat: reset and force update
Browse files Browse the repository at this point in the history
  • Loading branch information
heiyu4585 committed Nov 19, 2022
1 parent c46d650 commit 1ccfa74
Show file tree
Hide file tree
Showing 13 changed files with 284 additions and 111 deletions.
60 changes: 60 additions & 0 deletions components/mentions/demo/async.md
Expand Up @@ -6,6 +6,66 @@

async

```tsx
import { Mentions } from 'antd';
import debounce from 'lodash/debounce';
import React, { useCallback, useRef, useState } from 'react';

const App: React.FC = () => {
const [loading, setLoading] = useState(false);
const [users, setUsers] = useState<{ login: string; avatar_url: string }[]>([]);
const ref = useRef<string>();

const loadGithubUsers = (key: string) => {
if (!key) {
setUsers([]);
return;
}

fetch(`https://api.github.com/search/users?q=${key}`)
.then(res => res.json())
.then(({ options = [] }) => {
if (ref.current !== key) return;

setLoading(false);
setUsers(options.slice(0, 10));
});
};

const debounceLoadGithubUsers = useCallback(debounce(loadGithubUsers, 800), []);

const onSearch = (search: string) => {
console.log('Search:', search);
ref.current = search;
setLoading(!!search);
setUsers([]);

debounceLoadGithubUsers(search);
};

return (
<Mentions
style={{ width: '100%' }}
loading={loading}
onSearch={onSearch}
options={users.map(({ login, avatar_url: avatar }) => ({
key: login,
value: login,
className: 'antd-demo-dynamic-option',
label: (
<>
<img src={avatar} alt={login} />
<span>{login}</span>
</>
),
}))}
/>
);
};

export default App;
```

<style>
.antd-demo-dynamic-option img {
width: 20px;
Expand Down
31 changes: 19 additions & 12 deletions components/mentions/demo/async.tsx
Expand Up @@ -2,7 +2,6 @@ import React, { useCallback, useRef, useState } from 'react';
import { Mentions } from 'antd';
import debounce from 'lodash/debounce';

const { Option } = Mentions;
const App: React.FC = () => {
const [loading, setLoading] = useState(false);
const [users, setUsers] = useState<{ login: string; avatar_url: string }[]>([]);
Expand All @@ -15,12 +14,12 @@ const App: React.FC = () => {
}

fetch(`https://api.github.com/search/users?q=${key}`)
.then(res => res.json())
.then(({ items = [] }) => {
.then((res) => res.json())
.then(({ options = [] }) => {
if (ref.current !== key) return;

setLoading(false);
setUsers(items.slice(0, 10));
setUsers(options.slice(0, 10));
});
};

Expand All @@ -36,14 +35,22 @@ const App: React.FC = () => {
};

return (
<Mentions style={{ width: '100%' }} loading={loading} onSearch={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>
<Mentions
style={{ width: '100%' }}
loading={loading}
onSearch={onSearch}
options={users.map(({ login, avatar_url: avatar }) => ({
key: login,
value: login,
className: 'antd-demo-dynamic-option',
label: (
<>
<img src={avatar} alt={login} />
<span>{login}</span>
</>
),
}))}
/>
);
};

Expand Down
25 changes: 18 additions & 7 deletions components/mentions/demo/autoSize.tsx
@@ -1,14 +1,25 @@
import React from 'react';
import { Mentions } from 'antd';

const { Option } = Mentions;

const App: React.FC = () => (
<Mentions autoSize style={{ width: '100%' }}>
<Option value="afc163">afc163</Option>
<Option value="zombieJ">zombieJ</Option>
<Option value="yesmeck">yesmeck</Option>
</Mentions>
<Mentions
autoSize
style={{ width: '100%' }}
options={[
{
value: 'afc163',
label: 'afc163',
},
{
value: 'zombieJ',
label: 'zombieJ',
},
{
value: 'yesmeck',
label: 'yesmeck',
},
]}
/>
);

export default App;
26 changes: 17 additions & 9 deletions components/mentions/demo/basic.tsx
@@ -1,14 +1,12 @@
import React from 'react';
import { Mentions } from 'antd';
import type { OptionProps } from 'antd/es/mentions';

const { Option } = Mentions;
import type { MentionsOptionProps } from 'antd/es/mentions';

const onChange = (value: string) => {
console.log('Change:', value);
};

const onSelect = (option: OptionProps) => {
const onSelect = (option: MentionsOptionProps) => {
console.log('select', option);
};

Expand All @@ -18,11 +16,21 @@ const App: React.FC = () => (
onChange={onChange}
onSelect={onSelect}
defaultValue="@afc163"
>
<Option value="afc163">afc163</Option>
<Option value="zombieJ">zombieJ</Option>
<Option value="yesmeck">yesmeck</Option>
</Mentions>
options={[
{
value: 'afc163',
label: 'afc163',
},
{
value: 'zombieJ',
label: 'zombieJ',
},
{
value: 'yesmeck',
label: 'yesmeck',
},
]}
/>
);

export default App;
47 changes: 36 additions & 11 deletions components/mentions/demo/form.tsx
@@ -1,7 +1,7 @@
import React from 'react';
import { Button, Form, Mentions } from 'antd';

const { Option, getMentions } = Mentions;
const { getMentions } = Mentions;

const App: React.FC = () => {
const [form] = Form.useForm();
Expand Down Expand Up @@ -36,11 +36,23 @@ const App: React.FC = () => {
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>
<Mentions
rows={1}
options={[
{
value: 'afc163',
label: 'afc163',
},
{
value: 'zombieJ',
label: 'zombieJ',
},
{
value: 'yesmeck',
label: 'yesmeck',
},
]}
/>
</Form.Item>
<Form.Item
name="bio"
Expand All @@ -49,11 +61,24 @@ const App: React.FC = () => {
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>
<Mentions
rows={3}
placeholder="You can use @ to ref user here"
options={[
{
value: 'afc163',
label: 'afc163',
},
{
value: 'zombieJ',
label: 'zombieJ',
},
{
value: 'yesmeck',
label: 'yesmeck',
},
]}
/>
</Form.Item>
<Form.Item wrapperCol={{ span: 14, offset: 6 }}>
<Button htmlType="submit" type="primary">
Expand Down
25 changes: 18 additions & 7 deletions components/mentions/demo/placement.tsx
@@ -1,14 +1,25 @@
import React from 'react';
import { Mentions } from 'antd';

const { Option } = Mentions;

const App: React.FC = () => (
<Mentions style={{ width: '100%' }} placement="top">
<Option value="afc163">afc163</Option>
<Option value="zombieJ">zombieJ</Option>
<Option value="yesmeck">yesmeck</Option>
</Mentions>
<Mentions
style={{ width: '100%' }}
placement="top"
options={[
{
value: 'afc163',
label: 'afc163',
},
{
value: 'zombieJ',
label: 'zombieJ',
},
{
value: 'yesmeck',
label: 'yesmeck',
},
]}
/>
);

export default App;
15 changes: 6 additions & 9 deletions components/mentions/demo/prefix.tsx
@@ -1,8 +1,6 @@
import React, { useState } from 'react';
import { Mentions } from 'antd';

const { Option } = Mentions;

const MOCK_DATA = {
'@': ['afc163', 'zombiej', 'yesmeck'],
'#': ['1.0', '2.0', '3.0'],
Expand All @@ -23,13 +21,12 @@ const App: React.FC = () => {
placeholder="input @ to mention people, # to mention tag"
prefix={['@', '#']}
onSearch={onSearch}
>
{(MOCK_DATA[prefix] || []).map(value => (
<Option key={value} value={value}>
{value}
</Option>
))}
</Mentions>
options={(MOCK_DATA[prefix] || []).map((value) => ({
key: value,
value,
label: value,
}))}
/>
);
};

Expand Down
31 changes: 17 additions & 14 deletions components/mentions/demo/readonly.tsx
@@ -1,25 +1,28 @@
import React from 'react';
import { Mentions } from 'antd';

const { Option } = Mentions;

const getOptions = () =>
['afc163', 'zombiej', 'yesmeck'].map(value => (
<Option key={value} value={value}>
{value}
</Option>
));
const options = ['afc163', 'zombiej', 'yesmeck'].map((value) => ({
value,
key: value,
label: value,
}));

const App: React.FC = () => (
<>
<div style={{ marginBottom: 10 }}>
<Mentions style={{ width: '100%' }} placeholder="this is disabled Mentions" disabled>
{getOptions()}
</Mentions>
<Mentions
style={{ width: '100%' }}
placeholder="this is disabled Mentions"
disabled
options={options}
/>
</div>
<Mentions style={{ width: '100%' }} placeholder="this is readOnly Mentions" readOnly>
{getOptions()}
</Mentions>
<Mentions
style={{ width: '100%' }}
placeholder="this is readOnly Mentions"
readOnly
options={options}
/>
</>
);

Expand Down

0 comments on commit 1ccfa74

Please sign in to comment.