Skip to content

Commit 737d47a

Browse files
author
Jianan Li
committed
Refactoring
1 parent 74a99fe commit 737d47a

File tree

11 files changed

+53
-38
lines changed

11 files changed

+53
-38
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ coverage
2828
es
2929
yarn.lock
3030
package-lock.json
31+
pnpm-lock.yaml
3132
.storybook
3233
.doc
3334

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ ReactDOM.render(App, container);
7878
<td>default active key</td>
7979
</tr>
8080
<tr>
81-
<td>destroyInactivePanel</td>
81+
<td>destroyOnHidden</td>
8282
<td>Boolean</td>
8383
<th>false</th>
8484
<td>If destroy the panel which not active, default false. </td>

docs/examples/_util/motionUtil.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { CSSMotionProps, MotionEndEventHandler, MotionEventHandler } from 'rc-motion';
1+
import type {
2+
CSSMotionProps,
3+
MotionEndEventHandler,
4+
MotionEventHandler,
5+
} from '@rc-component/motion';
26

37
const getCollapsedHeight: MotionEventHandler = () => ({ height: 0, opacity: 0 });
48
const getRealHeight: MotionEventHandler = (node) => ({ height: node.scrollHeight, opacity: 1 });

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
},
4747
"dependencies": {
4848
"@babel/runtime": "^7.10.1",
49+
"@rc-component/motion": "^1.1.4",
4950
"@rc-component/util": "^1.0.1",
50-
"classnames": "2.x",
51-
"rc-motion": "^2.3.4"
51+
"classnames": "2.x"
5252
},
5353
"devDependencies": {
5454
"@rc-component/father-plugin": "^2.0.1",
@@ -57,8 +57,9 @@
5757
"@testing-library/user-event": "^14.5.2",
5858
"@types/classnames": "^2.2.9",
5959
"@types/jest": "^29.4.0",
60-
"@types/react": "^18.0.0",
61-
"@types/react-dom": "^18.0.0",
60+
"@types/node": "^22.15.18",
61+
"@types/react": "^19.1.4",
62+
"@types/react-dom": "^19.1.5",
6263
"@umijs/fabric": "^4.0.0",
6364
"dumi": "^2.1.1",
6465
"eslint": "^8.55.0",
@@ -73,12 +74,12 @@
7374
"np": "^9.1.0",
7475
"prettier": "^3.0.3",
7576
"rc-test": "^7.0.14",
76-
"react": "^18.2.0",
77-
"react-dom": "^18.2.0",
77+
"react": "^19.1.0",
78+
"react-dom": "^19.1.0",
7879
"typescript": "^5.0.0"
7980
},
8081
"peerDependencies": {
81-
"react": ">=16.9.0",
82-
"react-dom": ">=16.9.0"
82+
"react": ">=18.0.0",
83+
"react-dom": ">=18.0.0"
8384
}
8485
}

src/Collapse.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function getActiveKeysArray(activeKey: React.Key | React.Key[]) {
2020
const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) => {
2121
const {
2222
prefixCls = 'rc-collapse',
23-
destroyInactivePanel = false,
23+
destroyOnHidden = false,
2424
style,
2525
accordion,
2626
className,
@@ -72,7 +72,7 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) =>
7272
openMotion,
7373
expandIcon,
7474
collapsible,
75-
destroyInactivePanel,
75+
destroyOnHidden,
7676
onItemClick,
7777
activeKey,
7878
classNames: customizeClassNames,

src/Panel.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import classNames from 'classnames';
2-
import CSSMotion from 'rc-motion';
2+
import CSSMotion from '@rc-component/motion';
33
import KeyCode from '@rc-component/util/lib/KeyCode';
44
import React from 'react';
55
import type { CollapsePanelProps } from './interface';
@@ -23,7 +23,7 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop
2323
header,
2424
expandIcon,
2525
openMotion,
26-
destroyInactivePanel,
26+
destroyOnHidden,
2727
children,
2828
...resetProps
2929
} = props;
@@ -104,7 +104,7 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop
104104
leavedClassName={`${prefixCls}-panel-hidden`}
105105
{...openMotion}
106106
forceRender={forceRender}
107-
removeOnLeave={destroyInactivePanel}
107+
removeOnLeave={destroyOnHidden}
108108
>
109109
{({ className: motionClassName, style: motionStyle }, motionRef) => {
110110
return (
@@ -117,7 +117,7 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop
117117
styles={styles}
118118
isActive={isActive}
119119
forceRender={forceRender}
120-
role={accordion ? 'tabpanel' : void 0}
120+
role={accordion ? 'tabpanel' : undefined}
121121
>
122122
{children}
123123
</PanelContent>

src/PanelContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { CollapsePanelProps } from './interface';
44

55
const PanelContent = React.forwardRef<
66
HTMLDivElement,
7-
CollapsePanelProps & { children: React.ReactNode }
7+
React.PropsWithChildren<Readonly<CollapsePanelProps>>
88
>((props, ref) => {
99
const {
1010
prefixCls,

src/hooks/useItems.tsx

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type Props = Pick<
77
CollapsePanelProps,
88
'prefixCls' | 'onItemClick' | 'openMotion' | 'expandIcon' | 'classNames' | 'styles'
99
> &
10-
Pick<CollapseProps, 'accordion' | 'collapsible' | 'destroyInactivePanel'> & {
10+
Pick<CollapseProps, 'accordion' | 'collapsible' | 'destroyOnHidden'> & {
1111
activeKey: React.Key[];
1212
};
1313

@@ -16,7 +16,7 @@ const convertItemsToNodes = (items: ItemType[], props: Props) => {
1616
prefixCls,
1717
accordion,
1818
collapsible,
19-
destroyInactivePanel,
19+
destroyOnHidden,
2020
onItemClick,
2121
activeKey,
2222
openMotion,
@@ -32,18 +32,20 @@ const convertItemsToNodes = (items: ItemType[], props: Props) => {
3232
key: rawKey,
3333
collapsible: rawCollapsible,
3434
onItemClick: rawOnItemClick,
35-
destroyInactivePanel: rawDestroyInactivePanel,
35+
destroyOnHidden: rawDestroyOnHidden,
3636
...restProps
3737
} = item;
3838

3939
// You may be puzzled why you want to convert them all into strings, me too.
4040
// Maybe: https://github.com/react-component/collapse/blob/aac303a8b6ff30e35060b4f8fecde6f4556fcbe2/src/Collapse.tsx#L15
4141
const key = String(rawKey ?? index);
4242
const mergeCollapsible = rawCollapsible ?? collapsible;
43-
const mergeDestroyInactivePanel = rawDestroyInactivePanel ?? destroyInactivePanel;
43+
const mergedDestroyOnHidden = rawDestroyOnHidden ?? destroyOnHidden;
4444

4545
const handleItemClick = (value: React.Key) => {
46-
if (mergeCollapsible === 'disabled') return;
46+
if (mergeCollapsible === 'disabled') {
47+
return;
48+
}
4749
onItemClick(value);
4850
rawOnItemClick?.(value);
4951
};
@@ -70,7 +72,7 @@ const convertItemsToNodes = (items: ItemType[], props: Props) => {
7072
header={label}
7173
collapsible={mergeCollapsible}
7274
onItemClick={handleItemClick}
73-
destroyInactivePanel={mergeDestroyInactivePanel}
75+
destroyOnHidden={mergedDestroyOnHidden}
7476
>
7577
{children}
7678
</CollapsePanel>
@@ -86,13 +88,15 @@ const getNewChild = (
8688
index: number,
8789
props: Props,
8890
) => {
89-
if (!child) return null;
91+
if (!child) {
92+
return null;
93+
}
9094

9195
const {
9296
prefixCls,
9397
accordion,
9498
collapsible,
95-
destroyInactivePanel,
99+
destroyOnHidden,
96100
onItemClick,
97101
activeKey,
98102
openMotion,
@@ -106,7 +110,7 @@ const getNewChild = (
106110
const {
107111
header,
108112
headerClass,
109-
destroyInactivePanel: childDestroyInactivePanel,
113+
destroyOnHidden: childDestroyOnHidden,
110114
collapsible: childCollapsible,
111115
onItemClick: childOnItemClick,
112116
} = child.props;
@@ -121,7 +125,9 @@ const getNewChild = (
121125
const mergeCollapsible = childCollapsible ?? collapsible;
122126

123127
const handleItemClick = (value: React.Key) => {
124-
if (mergeCollapsible === 'disabled') return;
128+
if (mergeCollapsible === 'disabled') {
129+
return;
130+
}
125131
onItemClick(value);
126132
childOnItemClick?.(value);
127133
};
@@ -135,7 +141,7 @@ const getNewChild = (
135141
styles,
136142
isActive,
137143
prefixCls,
138-
destroyInactivePanel: childDestroyInactivePanel ?? destroyInactivePanel,
144+
destroyOnHidden: childDestroyOnHidden ?? destroyOnHidden,
139145
openMotion,
140146
accordion,
141147
children: child.props.children,
@@ -155,7 +161,7 @@ const getNewChild = (
155161
}
156162
});
157163

158-
return React.cloneElement(child, childProps);
164+
return React.cloneElement<CollapsePanelProps>(child, childProps);
159165
};
160166

161167
function useItems(
@@ -166,8 +172,9 @@ function useItems(
166172
if (Array.isArray(items)) {
167173
return convertItemsToNodes(items, props);
168174
}
169-
170-
return toArray(rawChildren).map((child, index) => getNewChild(child, index, props));
175+
return toArray(rawChildren).map((child, index) =>
176+
getNewChild(child as React.ReactElement<CollapsePanelProps>, index, props),
177+
);
171178
}
172179

173180
export default useItems;

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from 'react';
12
import Collapse from './Collapse';
23

34
export type { CollapsePanelProps, CollapseProps } from './interface';

src/interface.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CSSMotionProps } from 'rc-motion';
1+
import type { CSSMotionProps } from '@rc-component/motion';
22
import type * as React from 'react';
33

44
export type CollapsibleType = 'header' | 'icon' | 'disabled';
@@ -28,7 +28,7 @@ export interface CollapseProps {
2828
accordion?: boolean;
2929
className?: string;
3030
style?: object;
31-
destroyInactivePanel?: boolean;
31+
destroyOnHidden?: boolean;
3232
expandIcon?: (props: object) => React.ReactNode;
3333
collapsible?: CollapsibleType;
3434
children?: React.ReactNode;
@@ -42,9 +42,10 @@ export interface CollapseProps {
4242
}
4343

4444
export type SemanticName = 'header' | 'title' | 'body' | 'icon';
45+
4546
export interface CollapsePanelProps extends React.DOMAttributes<HTMLDivElement> {
4647
id?: string;
47-
header?: string | React.ReactNode;
48+
header?: React.ReactNode;
4849
prefixCls?: string;
4950
headerClass?: string;
5051
showArrow?: boolean;
@@ -54,10 +55,10 @@ export interface CollapsePanelProps extends React.DOMAttributes<HTMLDivElement>
5455
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
5556
isActive?: boolean;
5657
openMotion?: CSSMotionProps;
57-
destroyInactivePanel?: boolean;
58+
destroyOnHidden?: boolean;
5859
accordion?: boolean;
5960
forceRender?: boolean;
60-
extra?: string | React.ReactNode;
61+
extra?: React.ReactNode;
6162
onItemClick?: (panelKey: React.Key) => void;
6263
expandIcon?: (props: object) => React.ReactNode;
6364
panelKey?: React.Key;

0 commit comments

Comments
 (0)