2018-09-12 17:01:48 +08:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import HisotyListItem from './history-list-item';
|
|
|
|
import Loading from '../loading';
|
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
hasMore: PropTypes.bool.isRequired,
|
|
|
|
isReloadingData: PropTypes.bool.isRequired,
|
|
|
|
historyList: PropTypes.array.isRequired,
|
|
|
|
reloadMore: PropTypes.func.isRequired,
|
2018-12-25 10:39:57 +08:00
|
|
|
onItemClick: PropTypes.func.isRequired,
|
2019-01-31 17:37:02 +08:00
|
|
|
onItemRestore: PropTypes.func.isRequired,
|
2018-09-12 17:01:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class HistoryListView extends React.Component {
|
|
|
|
|
2018-12-25 10:39:57 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
isItemFreezed: false,
|
|
|
|
currentItem: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount = () => {
|
|
|
|
let historyList = this.props.historyList;
|
|
|
|
if (historyList.length > 0) {
|
2024-07-18 11:58:42 +08:00
|
|
|
this.setState({ currentItem: historyList[0] });
|
2018-12-25 10:39:57 +08:00
|
|
|
if (historyList === 1) {
|
|
|
|
this.props.onItemClick(historyList[0]);
|
|
|
|
} else {
|
|
|
|
this.props.onItemClick(historyList[0], historyList[1]);
|
|
|
|
}
|
|
|
|
}
|
2023-09-13 08:40:50 +08:00
|
|
|
};
|
2018-12-25 10:39:57 +08:00
|
|
|
|
|
|
|
onFreezedItemToggle = () => {
|
2024-07-18 11:58:42 +08:00
|
|
|
this.setState({ isItemFreezed: !this.state.isItemFreezed });
|
2023-09-13 08:40:50 +08:00
|
|
|
};
|
2018-12-25 10:39:57 +08:00
|
|
|
|
2018-09-12 17:01:48 +08:00
|
|
|
onScrollHandler = (event) => {
|
|
|
|
const clientHeight = event.target.clientHeight;
|
|
|
|
const scrollHeight = event.target.scrollHeight;
|
2024-07-18 11:58:42 +08:00
|
|
|
const scrollTop = event.target.scrollTop;
|
2018-09-12 17:01:48 +08:00
|
|
|
const isBottom = (clientHeight + scrollTop + 1 >= scrollHeight);
|
|
|
|
let hasMore = this.props.hasMore;
|
|
|
|
if (isBottom && hasMore) {
|
|
|
|
this.props.reloadMore();
|
|
|
|
}
|
2023-09-13 08:40:50 +08:00
|
|
|
};
|
2018-09-12 17:01:48 +08:00
|
|
|
|
2018-12-25 10:39:57 +08:00
|
|
|
onItemClick = (item, currentIndex) => {
|
2024-07-18 11:58:42 +08:00
|
|
|
this.setState({ currentItem: item });
|
2018-12-25 10:39:57 +08:00
|
|
|
if (currentIndex !== this.props.historyList.length) {
|
|
|
|
let preItem = this.props.historyList[currentIndex + 1];
|
|
|
|
this.props.onItemClick(item, preItem);
|
2018-09-26 02:27:14 -07:00
|
|
|
} else {
|
2018-12-25 10:39:57 +08:00
|
|
|
this.props.onItemClick(item);
|
2018-09-26 02:27:14 -07:00
|
|
|
}
|
2023-09-13 08:40:50 +08:00
|
|
|
};
|
2018-09-26 02:27:14 -07:00
|
|
|
|
2018-09-12 17:01:48 +08:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<ul className="history-list-container" onScroll={this.onScrollHandler}>
|
2018-12-25 10:39:57 +08:00
|
|
|
{this.props.historyList.map((item, index) => {
|
2018-09-12 17:01:48 +08:00
|
|
|
return (
|
|
|
|
<HisotyListItem
|
2020-11-02 13:56:35 +08:00
|
|
|
key={index}
|
2018-09-12 17:01:48 +08:00
|
|
|
item={item}
|
2018-12-25 10:39:57 +08:00
|
|
|
index={index}
|
|
|
|
currentItem={this.state.currentItem}
|
|
|
|
isItemFreezed={this.state.isItemFreezed}
|
|
|
|
onItemClick={this.onItemClick}
|
|
|
|
onItemRestore={this.props.onItemRestore}
|
|
|
|
onFreezedItemToggle={this.onFreezedItemToggle}
|
2018-09-12 17:01:48 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
{this.props.isReloadingData && <li><Loading /></li>}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryListView.propTypes = propTypes;
|
|
|
|
|
2023-09-13 08:40:50 +08:00
|
|
|
export default HistoryListView;
|