-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Wugaoliang
committed
Jun 16, 2021
1 parent
696bc79
commit c80179c
Showing
10 changed files
with
200 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react' | ||
import { Select } from '@hi-ui/hiui' | ||
import { Select as AntSelect } from 'antd' | ||
|
||
import 'antd/dist/antd.css' | ||
|
||
const { Option } = AntSelect | ||
const Page = () => { | ||
return ( | ||
<div> | ||
<Select data={[]}></Select> | ||
|
||
<h2>四类</h2> | ||
<AntSelect defaultValue="lucy" style={{ width: 120 }}> | ||
<Option value="jack">Jack</Option> | ||
<Option value="lucy">Lucy</Option> | ||
<Option value="disabled" disabled> | ||
Disabled | ||
</Option> | ||
<Option value="Yiminghe">yiminghe</Option> | ||
</AntSelect> | ||
<div style={{ height: '100vh' }}></div> | ||
</div> | ||
) | ||
} | ||
export default Page |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import React from 'react' | ||
const CacheContext = React.createContext() | ||
export default CacheContext |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React, { useReducer, useCallback } from 'react' | ||
import CacheContext from './CacheContext' | ||
import cacheReducer from './cacheReducer' | ||
import * as cacheTypes from './cache-types' | ||
function KeepAliveProvider(props) { | ||
const [cacheStates, dispatch] = useReducer(cacheReducer, {}) | ||
const mount = useCallback( | ||
({ cacheId, element }) => { | ||
if (cacheStates[cacheId]) { | ||
const cacheState = cacheStates[cacheId] | ||
if (cacheState.status === cacheTypes.DESTROY) { | ||
const doms = cacheState.doms | ||
doms.forEach((dom) => dom.parentNode.removeChild(dom)) | ||
dispatch({ type: cacheTypes.CREATE, payload: { cacheId, element } }) | ||
} | ||
} else { | ||
dispatch({ type: cacheTypes.CREATE, payload: { cacheId, element } }) | ||
} | ||
}, | ||
[cacheStates] | ||
) | ||
const handleScroll = useCallback( | ||
(cacheId, { target }) => { | ||
if (cacheStates[cacheId]) { | ||
const scrolls = cacheStates[cacheId].scrolls | ||
scrolls[target] = target.scrollTop | ||
} | ||
}, | ||
[cacheStates] | ||
) | ||
return ( | ||
<CacheContext.Provider value={{ mount, cacheStates, dispatch, handleScroll }}> | ||
{props.children} | ||
{Object.values(cacheStates) | ||
.filter((cacheState) => cacheState.status !== cacheTypes.DESTROY) | ||
.map(({ cacheId, element }) => ( | ||
<div | ||
id={`cache_${cacheId}`} | ||
key={cacheId} | ||
ref={(dom) => { | ||
const cacheState = cacheStates[cacheId] | ||
if (dom && (!cacheState.doms || cacheState.status === cacheTypes.DESTROY)) { | ||
const doms = Array.from(dom.childNodes) | ||
dispatch({ type: cacheTypes.CREATED, payload: { cacheId, doms } }) | ||
} | ||
}} | ||
> | ||
{element} | ||
</div> | ||
))} | ||
</CacheContext.Provider> | ||
) | ||
} | ||
export default KeepAliveProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const CREATE = 'CREATE' // 创建 | ||
export const CREATED = 'CREATED' // 创建成功 | ||
export const ACTIVE = 'ACTIVE' // 激活 | ||
export const DESTROY = 'DESTROY' // 销毁 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import * as cacheTypes from './cache-types' | ||
function cacheReducer(cacheStates = {}, { type, payload }) { | ||
switch (type) { | ||
case cacheTypes.CREATE: | ||
return { | ||
...cacheStates, | ||
[payload.cacheId]: { | ||
scrolls: {}, | ||
cacheId: payload.cacheId, | ||
element: payload.element, | ||
status: cacheTypes.CREATE | ||
} | ||
} | ||
case cacheTypes.CREATED: | ||
return { | ||
...cacheStates, | ||
[payload.cacheId]: { | ||
...cacheStates[payload.cacheId], | ||
doms: payload.doms, | ||
status: cacheTypes.CREATED | ||
} | ||
} | ||
case cacheTypes.ACTIVE: | ||
return { | ||
...cacheStates, | ||
[payload.cacheId]: { | ||
...cacheStates[payload.cacheId], | ||
status: cacheTypes.ACTIVE | ||
} | ||
} | ||
case cacheTypes.DESTROY: | ||
return { | ||
...cacheStates, | ||
[payload.cacheId]: { | ||
...cacheStates[payload.cacheId], | ||
status: cacheTypes.DESTROY | ||
} | ||
} | ||
default: | ||
return cacheStates | ||
} | ||
} | ||
export default cacheReducer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as KeepAliveProvider } from './KeepAliveProvider' | ||
export { default as withKeepAlive } from './withKeepAlive' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React, { useContext, useRef, useEffect } from 'react' | ||
import CacheContext from './CacheContext' | ||
import * as cacheTypes from './cache-types' | ||
const withKeepAlive = (OldComponent, { cacheId = window.location.pathname, scroll = false }) => { | ||
// eslint-disable-next-line react/display-name | ||
return (props) => { | ||
const { mount, cacheStates, dispatch, handleScroll } = useContext(CacheContext) | ||
const ref = useRef(null) | ||
useEffect(() => { | ||
if (scroll) { | ||
ref.current.addEventListener('scroll', handleScroll.bind(null, cacheId), true) | ||
} | ||
}, [handleScroll]) | ||
useEffect(() => { | ||
const cacheState = cacheStates[cacheId] | ||
if (cacheState && cacheState.doms && cacheState.status !== cacheTypes.DESTROY) { | ||
const doms = cacheState.doms | ||
doms.forEach((dom) => ref.current.appendChild(dom)) | ||
if (scroll) { | ||
doms.forEach((dom) => { | ||
if (cacheState.scrolls[dom]) dom.scrollTop = cacheState.scrolls[dom] | ||
}) | ||
} | ||
} else { | ||
mount({ cacheId, element: <OldComponent {...props} dispatch={dispatch} /> }) | ||
} | ||
}, [cacheStates, dispatch, mount, props]) | ||
return <div id={`keepalive_${cacheId}`} ref={ref} /> | ||
} | ||
} | ||
export default withKeepAlive |
Empty file.