-
Notifications
You must be signed in to change notification settings - Fork 0
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
Ouchuxuan
committed
Jul 17, 2021
1 parent
bca8ce3
commit dd93094
Showing
14 changed files
with
849 additions
and
11 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
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
@import '~antd/dist/antd.css'; | ||
.App{ | ||
width: 100%; | ||
height: 100%; | ||
overflow: hidden; | ||
} |
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,82 @@ | ||
import React, { useContext, useEffect, useRef, useCallback } from 'react'; | ||
import { Checkbox } from 'antd'; | ||
import Proptypes from 'proptypes'; | ||
import { ListStateContext } from '../../store/ListState'; | ||
import './index.scss'; | ||
const toBottom = (scrollContainer, scrollDom) => { | ||
const scrollContainerHeight = scrollContainer.offsetHeight; | ||
const scrollTop = scrollContainer.scrollTop; | ||
const scrollDomHeight = scrollDom.offsetHeight; | ||
return (scrollDomHeight - scrollTop - scrollContainerHeight) <= 0; | ||
} | ||
|
||
export default function List(props) { | ||
const scrollContainer = useRef(); | ||
const scrollDom = useRef(); | ||
const { onScroll } = props; | ||
const { listState, dispatch: dispatchListState } = useContext(ListStateContext); | ||
const onListScroll = () => { | ||
const isToBottom = toBottom(scrollContainer.current, scrollDom.current); | ||
onScroll(isToBottom); | ||
} | ||
const onCheckBoxChange = useCallback((event) => { | ||
const checked = event.target.checked; | ||
const id = event.target.id; | ||
const result = listState.listData.map(item => { | ||
if (item.id === id) { | ||
return { | ||
...item, | ||
checked: checked | ||
} | ||
} else { | ||
return item; | ||
} | ||
}) | ||
console.log('---', result); | ||
dispatchListState({ type: 'setList', value: result }); | ||
}, [dispatchListState, listState.listData]) | ||
|
||
// 当前页全选状态切换 | ||
useEffect(() => { | ||
// 这里被执行了 | ||
let isNotAllChecked; | ||
if (listState.listData.length === 0) { | ||
isNotAllChecked = true; | ||
} else { | ||
isNotAllChecked = listState.listData.some(item => { | ||
return !item.checked | ||
}) | ||
} | ||
console.log(1111, !isNotAllChecked); | ||
dispatchListState({ type: 'allCurrentPage', value: !isNotAllChecked }) | ||
}, [dispatchListState, listState.listData]); | ||
return ( | ||
<div className="list-layout" onScroll={onListScroll} ref={scrollContainer}> | ||
<div className="list-wrapper" ref={scrollDom}> | ||
{ | ||
listState.listData.map(item => { | ||
return ( | ||
<div className="list-item" key={item.id}> | ||
<Checkbox | ||
className="check-box" | ||
checked={item.checked} | ||
onChange={onCheckBoxChange} | ||
id={item.id} | ||
/> | ||
<div className="list-item-detail"> | ||
{item.name} | ||
</div> | ||
</div> | ||
) | ||
}) | ||
} | ||
</div> | ||
</div> | ||
) | ||
} | ||
// List.defaultProps = { | ||
// dataSource: [] | ||
// } | ||
List.propTypes = { | ||
onScroll: Proptypes.func, | ||
} |
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,23 @@ | ||
.list-layout { | ||
width: 100%; | ||
height: 500px; | ||
border-bottom: 1px solid #ccc; | ||
overflow-y: auto; | ||
.list-item { | ||
height: 30px; | ||
border-bottom: 1px solid #ccc; | ||
display: flex; | ||
align-items: center; | ||
justify-content: flex-start; | ||
padding-left: 10px; | ||
.list-item-detail { | ||
margin-left: 20px; | ||
height: 100%; | ||
flex: 1; | ||
display: flex; | ||
align-items: center; | ||
font-size: 12px; | ||
color: #ddd; | ||
} | ||
} | ||
} |
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,7 @@ | ||
import React from 'react'; | ||
import './index.scss'; | ||
export default function SwitchType(props){ | ||
return ( | ||
<div className="switch-type-layout">switchtypes</div> | ||
) | ||
} |
Empty file.
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,50 @@ | ||
import React, { createContext, useReducer } from 'react'; | ||
|
||
// 创建context | ||
export const ListStateContext = createContext({}); | ||
|
||
const reducer = (state, action) => { | ||
switch (action.type) { | ||
case 'allCurrentPage': | ||
return { | ||
...state, | ||
selectCurrentPage: action.value | ||
} | ||
case 'allPage': | ||
return { | ||
...state, | ||
selectAll: action.value | ||
} | ||
case 'setList': | ||
return { | ||
...state, | ||
listData: action.value | ||
} | ||
case 'addList': | ||
return { | ||
...state, | ||
listData:[...state.listData, ...action.value] | ||
} | ||
default: | ||
return state; | ||
} | ||
} | ||
const initState = { | ||
selectCurrentPage: false, | ||
selectAll: false, | ||
listData: [] | ||
} | ||
|
||
/** | ||
* 创建一个 ListState 组件 | ||
* ListState 组件包裹的所有子组件都可以通过调用 ListStateContext 访问到 value | ||
*/ | ||
export const ListState = props => { | ||
const [listState, dispatch] = useReducer(reducer, initState) | ||
return ( | ||
<ListStateContext.Provider value={{ listState, dispatch }}> | ||
{props.children} | ||
</ListStateContext.Provider> | ||
) | ||
} | ||
|
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,18 @@ | ||
import { v4 as uuid } from 'uuid'; | ||
export const randomNum = (min, max) => { | ||
return Math.floor(Math.random() * (max - min + 1) + min); | ||
} | ||
export const getData = (count) => { | ||
return new Promise(resolve => { | ||
let list = []; | ||
for (let i = 0; i < count; i++) { | ||
const id = uuid(); | ||
list.push({ | ||
id, | ||
money: randomNum(10, 10000), | ||
name: id | ||
}); | ||
} | ||
resolve(list); | ||
}) | ||
} |
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,81 @@ | ||
import React, { useEffect, useState, useCallback, useContext, useRef} from 'react'; | ||
import { Checkbox } from 'antd'; | ||
import List from '../../components/List' | ||
import SwitchType from '../../components/SwitchType'; | ||
import { getData } from '../../utils/data'; | ||
import { ListStateContext } from '../../store/ListState'; | ||
import './index.scss'; | ||
|
||
export default function Demo4() { | ||
const [toBottom, setToBottom] = useState(false); | ||
const currentListData = useRef([]); | ||
const { listState, dispatch: dispatchListState } = useContext(ListStateContext); | ||
|
||
const handleToBottom = useCallback((isToBottom) => { | ||
setToBottom(isToBottom); | ||
}, []) | ||
|
||
const loadMore = () => { | ||
getData(20).then(res => { | ||
if (res.length) { | ||
dispatchListState({type:'addList', value:res}) | ||
} | ||
}) | ||
} | ||
|
||
const onSelectCurrentPage = useCallback((event) => { | ||
const checked = event.target.checked; | ||
const result = currentListData.current.map(item=>{ | ||
return { | ||
...item, | ||
checked:checked, | ||
} | ||
}) | ||
dispatchListState({type:'setList',value:result}); | ||
dispatchListState({ type: 'allCurrentPage', value: checked }); | ||
|
||
}, [dispatchListState]); | ||
|
||
const onSelectAll = useCallback((event) => { | ||
const checked = event.target.checked; | ||
dispatchListState({ type: 'allPage', value: checked }) | ||
}, [dispatchListState]); | ||
|
||
useEffect(() => { | ||
getData(20).then(res => { | ||
dispatchListState({type:'setList', value:res}) | ||
}) | ||
}, [dispatchListState]) | ||
|
||
useEffect(() => { | ||
setToBottom(false); | ||
}, [listState.listData]) | ||
|
||
// 缓存全选状态切换时当前列表数据 | ||
useEffect(()=>{ | ||
currentListData.current = listState.listData | ||
},[listState.listData, listState.selectCurrentPage]) | ||
|
||
// 当前页全选状态切换(这里有问题啦) | ||
useEffect(()=>{ | ||
|
||
},[dispatchListState, listState.selectCurrentPage]) | ||
|
||
return ( | ||
<div className="demo-layout"> | ||
<div className="operation-bar"> | ||
<div className="operation-item"> | ||
<Checkbox className="check-box" checked={listState.selectCurrentPage} onChange={onSelectCurrentPage} /> | ||
<div className="label">本页全选</div> | ||
</div> | ||
<div className="operation-item"> | ||
<Checkbox className="check-box" checked={listState.selectAll} onChange={onSelectAll} /> | ||
<div className="label">全部全选</div> | ||
</div> | ||
</div> | ||
<List onScroll={handleToBottom} /> | ||
{toBottom && (<div className="load-more" onClick={loadMore}>加载更多</div>)} | ||
<SwitchType></SwitchType> | ||
</div> | ||
) | ||
} |
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,42 @@ | ||
.demo-layout { | ||
width: 600px; | ||
height: 660px; | ||
margin: 0 auto; | ||
margin-top: 10px; | ||
border: 1px solid #ccc; | ||
.operation-bar { | ||
width: 100%; | ||
height: 30px; | ||
border-bottom: 1px solid #ccc; | ||
display: flex; | ||
align-items: center; | ||
justify-content: flex-start; | ||
padding-left: 10px; | ||
background-color: rgb(236, 174, 174); | ||
.operation-item { | ||
height: 100%; | ||
display: flex; | ||
align-items: center; | ||
font-size: 12px; | ||
margin-right: 20px; | ||
.label{ | ||
color: #2b2828; | ||
} | ||
.check-box{ | ||
margin-right: 5px; | ||
} | ||
} | ||
} | ||
.load-more{ | ||
cursor: pointer; | ||
font-size: 14px; | ||
width: 80px; | ||
height: 30px; | ||
border-radius: 3px; | ||
line-height: 30px; | ||
text-align: center; | ||
margin: 0 auto; | ||
margin-top: 5px; | ||
background-color:rgb(143, 219, 166); | ||
} | ||
} |
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
Oops, something went wrong.