Skip to content

Commit 9e0d4f6

Browse files
authored
feat: add wrapperProps & contentProps
1 parent 091dd61 commit 9e0d4f6

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

index.d.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
import { ReactNode, ReactNodeArray, Context, Component, ComponentType } from 'react'
1+
import {
2+
ReactNode,
3+
ReactNodeArray,
4+
Context,
5+
Component,
6+
ComponentType,
7+
} from 'react'
28

3-
export declare type GetProps<C> = C extends ComponentType<infer P> ? P : never;
9+
export declare type GetProps<C> = C extends ComponentType<infer P> ? P : never
10+
11+
type DivProps = React.HTMLAttributes<HTMLDivElement>
412

513
export interface KeepAliveProps {
614
children: ReactNode | ReactNodeArray
@@ -10,6 +18,8 @@ export interface KeepAliveProps {
1018
when?: boolean | Array<boolean> | (() => boolean | Array<boolean>)
1119
saveScrollPosition?: boolean | string
1220
autoFreeze?: boolean
21+
wrapperProps?: DivProps
22+
contentProps?: DivProps
1323
[key: string]: any
1424
}
1525

@@ -53,5 +63,9 @@ export interface AliveController {
5363
}
5464
export function useAliveController(): AliveController
5565

56-
export declare function withActivation<C extends ComponentType<GetProps<C>>>(component: C): C
57-
export declare function withAliveScope<C extends ComponentType<GetProps<C>>>(component: C): C
66+
export declare function withActivation<C extends ComponentType<GetProps<C>>>(
67+
component: C
68+
): C
69+
export declare function withAliveScope<C extends ComponentType<GetProps<C>>>(
70+
component: C
71+
): C

src/core/KeepAlive.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,13 @@ class KeepAlive extends Component {
244244
}
245245

246246
render() {
247+
const { wrapperProps = {} } = this.props || {}
247248
return (
248249
<div
249-
key="keep-alive-placeholder"
250+
{...wrapperProps}
251+
key='keep-alive-placeholder'
250252
nodeKeyIgnore
251-
className="ka-wrapper"
253+
className={`ka-wrapper ${wrapperProps.className || ''}`}
252254
ref={(node) => {
253255
this.placeholder = node
254256
}}
@@ -259,9 +261,20 @@ class KeepAlive extends Component {
259261

260262
// 兼容 SSR 服务端渲染
261263
function SSRKeepAlive({ children }) {
264+
const { wrapperProps = {}, contentProps = {} } = this.props || {}
262265
return (
263-
<div key="keep-alive-placeholder" nodeKeyIgnore className="ka-wrapper">
264-
<div key="keeper-container" nodeKeyIgnore className="ka-content">
266+
<div
267+
{...wrapperProps}
268+
key='keep-alive-placeholder'
269+
nodeKeyIgnore
270+
className={`ka-wrapper ${wrapperProps.className || ''}`}
271+
>
272+
<div
273+
{...contentProps}
274+
key='keeper-container'
275+
nodeKeyIgnore
276+
className={`ka-content ${contentProps.className || ''}`}
277+
>
265278
{children}
266279
</div>
267280
</div>

src/core/Keeper.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export default class Keeper extends PureComponent {
5757
store.set(id, this.cache)
5858
}
5959

60-
6160
unmounted = false
6261
safeSetState = (nextState, callback) => {
6362
// fix #170
@@ -82,9 +81,8 @@ export default class Keeper extends PureComponent {
8281
this.unmounted = true
8382
}
8483

85-
freezeTimeout = null
86-
87-
;[LIFECYCLE_ACTIVATE]() {
84+
freezeTimeout = null;
85+
[LIFECYCLE_ACTIVATE]() {
8886
clearTimeout(this.freezeTimeout)
8987
// 激活后,立即解冻
9088
this.safeSetState({
@@ -94,7 +92,7 @@ export default class Keeper extends PureComponent {
9492
this.listeners.forEach((listener) => run(listener, [LIFECYCLE_ACTIVATE]))
9593
}
9694

97-
;[LIFECYCLE_UNACTIVATE]() {
95+
[LIFECYCLE_UNACTIVATE]() {
9896
this.eventBus.emit(LIFECYCLE_UNACTIVATE)
9997
const listeners = [...this.listeners]
10098

@@ -202,7 +200,7 @@ export default class Keeper extends PureComponent {
202200
})
203201

204202
render() {
205-
const { id, autoFreeze = true, ...props } = this.props
203+
const { id, autoFreeze = true, contentProps = {}, ...props } = this.props
206204
const { children, bridgeProps, key, freeze } = this.state
207205

208206
return (
@@ -212,7 +210,12 @@ export default class Keeper extends PureComponent {
212210
this.wrapper = node
213211
}}
214212
>
215-
<div key="keeper-container" nodeKeyIgnore className="ka-content">
213+
<div
214+
{...contentProps}
215+
key='keeper-container'
216+
nodeKeyIgnore
217+
className={`ka-content ${contentProps.className || ''}`}
218+
>
216219
<Bridge id={id} bridgeProps={bridgeProps}>
217220
<AliveNodeProvider value={this.contextValue}>
218221
{React.Children.map(children, (child, idx) =>

0 commit comments

Comments
 (0)