From 066be35de37c88fcbb8f05daa79359e20733b523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=91=E9=9B=A8?= Date: Wed, 23 Nov 2022 21:00:24 +0800 Subject: [PATCH] feat: support options (#38876) * feat: support options * feat: update package * feat: update package * feat: update doc * feat: update doc * feat: update doc * feat: add test case --- components/mentions/__tests__/index.test.tsx | 37 ++++++++++++++- components/mentions/demo/async.md | 27 +++++++---- components/mentions/demo/autoSize.md | 27 +++++++---- components/mentions/demo/basic.md | 28 +++++++---- components/mentions/demo/form.md | 49 +++++++++++++++----- components/mentions/demo/placement.md | 27 +++++++---- components/mentions/demo/prefix.md | 17 +++---- components/mentions/demo/readonly.md | 33 +++++++------ components/mentions/index.en-US.md | 23 +++++++-- components/mentions/index.tsx | 31 ++++++++++++- components/mentions/index.zh-CN.md | 29 +++++++++--- package.json | 6 +-- 12 files changed, 247 insertions(+), 87 deletions(-) diff --git a/components/mentions/__tests__/index.test.tsx b/components/mentions/__tests__/index.test.tsx index 320336eafd..37fad702e2 100644 --- a/components/mentions/__tests__/index.test.tsx +++ b/components/mentions/__tests__/index.test.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import Mentions from '..'; +import Mentions,{ Option } from '..'; import focusTest from '../../../tests/shared/focusTest'; import mountTest from '../../../tests/shared/mountTest'; import rtlTest from '../../../tests/shared/rtlTest'; @@ -84,4 +84,39 @@ describe('Mentions', () => { expect(wrapper.container.querySelectorAll('li.ant-mentions-dropdown-menu-item').length).toBe(1); expect(wrapper.container.querySelectorAll('.bamboo-light').length).toBeTruthy(); }); + it('do not lose label when use children Option', () => { + const wrapper = render( + Afc163 + ZombieJ + Yesmeck + ); + simulateInput(wrapper, '@'); + const {container} = wrapper + fireEvent.mouseEnter( + container.querySelector('li.ant-mentions-dropdown-menu-item:last-child')!, + ); + fireEvent.focus(container.querySelector('textarea')!); + act(() => { + jest.runAllTimers(); + }); + expect( + wrapper.container.querySelector('.ant-mentions-dropdown-menu-item-active')?.textContent, + ).toBe('Yesmeck'); + }); + it('warning if use Mentions.Option', () => { + const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + render( + + + + + , + ); + expect(errorSpy).toHaveBeenCalledWith( + 'Warning: [antd: Mentions] `Mentions.Option` is deprecated. Please use `options` instead.', + ); + }); + }); diff --git a/components/mentions/demo/async.md b/components/mentions/demo/async.md index d6ceb52a3f..c80faeee6e 100644 --- a/components/mentions/demo/async.md +++ b/components/mentions/demo/async.md @@ -14,11 +14,10 @@ title: async ```tsx +import React, { useCallback, useRef, useState } from 'react'; import { Mentions } from 'antd'; import debounce from 'lodash/debounce'; -import React, { useCallback, useRef, useState } from 'react'; -const { Option } = Mentions; const App: React.FC = () => { const [loading, setLoading] = useState(false); const [users, setUsers] = useState<{ login: string; avatar_url: string }[]>([]); @@ -52,14 +51,22 @@ const App: React.FC = () => { }; return ( - - {users.map(({ login, avatar_url: avatar }) => ( - - ))} - + ({ + key: login, + value: login, + className: 'antd-demo-dynamic-option', + label: ( + <> + {login} + {login} + + ), + }))} + /> ); }; diff --git a/components/mentions/demo/autoSize.md b/components/mentions/demo/autoSize.md index ef982729c7..7e88ff09e1 100644 --- a/components/mentions/demo/autoSize.md +++ b/components/mentions/demo/autoSize.md @@ -14,17 +14,28 @@ title: Height autoSize. ```tsx -import { Mentions } from 'antd'; import React from 'react'; - -const { Option } = Mentions; +import { Mentions } from 'antd'; const App: React.FC = () => ( - - - - - + ); export default App; diff --git a/components/mentions/demo/basic.md b/components/mentions/demo/basic.md index 7293c29d44..2b0be58699 100644 --- a/components/mentions/demo/basic.md +++ b/components/mentions/demo/basic.md @@ -14,17 +14,15 @@ title: Basic usage. ```tsx -import { Mentions } from 'antd'; -import type { OptionProps } from 'antd/es/mentions'; import React from 'react'; - -const { Option } = Mentions; +import { Mentions } from 'antd'; +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); }; @@ -34,11 +32,21 @@ const App: React.FC = () => ( onChange={onChange} onSelect={onSelect} defaultValue="@afc163" - > - - - - + options={[ + { + value: 'afc163', + label: 'afc163', + }, + { + value: 'zombieJ', + label: 'zombieJ', + }, + { + value: 'yesmeck', + label: 'yesmeck', + }, + ]} + /> ); export default App; diff --git a/components/mentions/demo/form.md b/components/mentions/demo/form.md index 7fa2a22b6e..b138ab9efc 100644 --- a/components/mentions/demo/form.md +++ b/components/mentions/demo/form.md @@ -14,10 +14,10 @@ title: Controlled mode, for example, to work with `Form`. ```tsx -import { Button, Form, Mentions } from 'antd'; 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(); @@ -52,11 +52,23 @@ const App: React.FC = () => { wrapperCol={{ span: 16 }} rules={[{ validator: checkMention }]} > - - - - - + { wrapperCol={{ span: 16 }} rules={[{ required: true }]} > - - - - - +