Skip to content

Commit 97852d1

Browse files
authored
refactor: Refactor with hooks & use React18 (#126)
* refactor: use hooks * test: init 18 * test: key code * test: all test * refactor: more hooks
1 parent a226f8e commit 97852d1

16 files changed

+782
-729
lines changed

examples/debug.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* eslint no-console: 0 */
2+
3+
import React from 'react';
4+
import Mentions from '../src';
5+
import '../assets/index.less';
6+
7+
const { Option } = Mentions;
8+
9+
export default () => (
10+
<Mentions rows={3} defaultValue="Hello World" open>
11+
<Option value="light">Light</Option>
12+
<Option value="bamboo">Bamboo</Option>
13+
<Option value="cat">Cat</Option>
14+
</Mentions>
15+
);

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,21 @@
3939
"react-dom": ">=16.9.0"
4040
},
4141
"devDependencies": {
42+
"@testing-library/jest-dom": "^5.16.4",
43+
"@testing-library/react": "^13.3.0",
4244
"@types/classnames": "^2.2.6",
43-
"@types/enzyme": "^3.1.15",
4445
"@types/react": "^18.0.8",
4546
"@types/react-dom": "^18.0.3",
4647
"@types/warning": "^3.0.0",
4748
"@umijs/fabric": "^2.0.8",
48-
"enzyme": "^3.11.0",
49-
"enzyme-to-json": "^3.1.4",
5049
"eslint": "^7.0.0",
5150
"father": "^2.13.6",
5251
"jest": "^28.0.3",
5352
"lodash.debounce": "^4.0.8",
5453
"np": "^7.0.0",
5554
"prettier": "^2.0.5",
56-
"react": "^16.0.0",
57-
"react-dom": "^16.0.0",
55+
"react": "^18.0.0",
56+
"react-dom": "^18.0.0",
5857
"typescript": "^4.0.3"
5958
},
6059
"dependencies": {
@@ -63,6 +62,6 @@
6362
"rc-menu": "~9.6.0",
6463
"rc-textarea": "^0.3.0",
6564
"rc-trigger": "^5.0.4",
66-
"rc-util": "^5.0.1"
65+
"rc-util": "^5.22.5"
6766
}
6867
}

src/DropdownMenu.tsx

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Menu, { MenuItem } from 'rc-menu';
22
import * as React from 'react';
3-
import { MentionsContextConsumer, MentionsContextProps } from './MentionsContext';
4-
import { OptionProps } from './Option';
3+
import MentionsContext from './MentionsContext';
4+
import type { OptionProps } from './Option';
55

66
interface DropdownMenuProps {
77
prefixCls?: string;
@@ -12,54 +12,50 @@ interface DropdownMenuProps {
1212
* We only use Menu to display the candidate.
1313
* The focus is controlled by textarea to make accessibility easy.
1414
*/
15-
class DropdownMenu extends React.Component<DropdownMenuProps, {}> {
16-
public renderDropdown = ({
15+
function DropdownMenu(props: DropdownMenuProps) {
16+
const {
1717
notFoundContent,
1818
activeIndex,
1919
setActiveIndex,
2020
selectOption,
2121
onFocus,
2222
onBlur,
23-
}: MentionsContextProps) => {
24-
const { prefixCls, options } = this.props;
25-
const activeOption = options[activeIndex] || {};
23+
} = React.useContext(MentionsContext);
2624

27-
return (
28-
<Menu
29-
prefixCls={`${prefixCls}-menu`}
30-
activeKey={activeOption.key}
31-
onSelect={({ key }) => {
32-
const option = options.find(({ key: optionKey }) => optionKey === key);
33-
selectOption(option);
34-
}}
35-
onFocus={onFocus}
36-
onBlur={onBlur}
37-
>
38-
{options.map((option, index) => {
39-
const { key, disabled, children, className, style } = option;
40-
return (
41-
<MenuItem
42-
key={key}
43-
disabled={disabled}
44-
className={className}
45-
style={style}
46-
onMouseEnter={() => {
47-
setActiveIndex(index);
48-
}}
49-
>
50-
{children}
51-
</MenuItem>
52-
);
53-
})}
25+
const { prefixCls, options } = props;
26+
const activeOption = options[activeIndex] || {};
5427

55-
{!options.length && <MenuItem disabled>{notFoundContent}</MenuItem>}
56-
</Menu>
57-
);
58-
};
28+
return (
29+
<Menu
30+
prefixCls={`${prefixCls}-menu`}
31+
activeKey={activeOption.key}
32+
onSelect={({ key }) => {
33+
const option = options.find(({ key: optionKey }) => optionKey === key);
34+
selectOption(option);
35+
}}
36+
onFocus={onFocus}
37+
onBlur={onBlur}
38+
>
39+
{options.map((option, index) => {
40+
const { key, disabled, children, className, style } = option;
41+
return (
42+
<MenuItem
43+
key={key}
44+
disabled={disabled}
45+
className={className}
46+
style={style}
47+
onMouseEnter={() => {
48+
setActiveIndex(index);
49+
}}
50+
>
51+
{children}
52+
</MenuItem>
53+
);
54+
})}
5955

60-
public render() {
61-
return <MentionsContextConsumer>{this.renderDropdown}</MentionsContextConsumer>;
62-
}
56+
{!options.length && <MenuItem disabled>{notFoundContent}</MenuItem>}
57+
</Menu>
58+
);
6359
}
6460

6561
export default DropdownMenu;

0 commit comments

Comments
 (0)