Select大数据量优化,针对 antd3x Select
https://blog.csdn.net/liuyuhua666/article/details/103703478

下拉刷新组件
参考 https://www.cnblogs.com/zling-gnilz/p/11064113.html
实现效果:https://lrhvt.csb.app select.wmv (332.24KB)``jsx const SearchInput = () => { const arr = [{ label: "test text", key: "test" }]; for (let i = 0; i < 500; i++) { arr.push({ label:label-${i}`, key: i, }); } return ; };

  1. <a name="SLkOe"></a>
  2. ## onPopupScroll
  3. 1. select组件下,onPopupScroll的API这样子可以自定义模拟 scrollToEnd
  4. 2. 参考:[https://github.com/ant-design/ant-design/issues/12406#issuecomment-424968753](https://github.com/ant-design/ant-design/issues/12406#issuecomment-424968753)
  5. 1. Select 组件提供 onScrollToEnd API,这样可以用来支持无限分页滚动
  6. ```jsx
  7. onPopupScroll = e => {
  8. e.persist();
  9. let target = e.target;
  10. if (target.scrollTop + target.offsetHeight === target.scrollHeight) {
  11. // scrollToEnd, do something!!!
  12. }
  13. };

select下拉刷新组件

  1. import React from "react";
  2. import { Select } from "antd";
  3. import PropTypes from "prop-types";
  4. const { Option } = Select;
  5. export default class CustomSelect extends React.Component {
  6. static propTypes = {
  7. dataSource: PropTypes.arrayOf(
  8. PropTypes.shape({
  9. key: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
  10. .isRequired,
  11. label: PropTypes.string
  12. })
  13. ),
  14. onChange: PropTypes.func
  15. };
  16. static defaultProps = {
  17. dataSource: []
  18. };
  19. constructor(props) {
  20. super(props);
  21. this.state = {
  22. page: 1,
  23. showArr: props.dataSource
  24. };
  25. this.pageSize = 20;
  26. this.list = [];
  27. }
  28. pageSt = 0;
  29. handleChange = v => {
  30. const { onChange, dataSource } = this.props;
  31. onChange && onChange(v);
  32. this.setState({ page: 1, showArr: dataSource });
  33. };
  34. handlePopupScroll = e => {
  35. const { page, showArr } = this.state;
  36. e.persist();
  37. const { target } = e;
  38. const st = target.scrollTop;
  39. if (st === 0 && this.pageSt) {
  40. target.scrollTop = this.pageSt;
  41. }
  42. if (
  43. st + target.offsetHeight + 2 >= target.scrollHeight &&
  44. this.list.length < showArr.length
  45. ) {
  46. this.setState({ page: page + 1 });
  47. this.pageSt = st;
  48. } else {
  49. this.pageSt = 0;
  50. }
  51. };
  52. handleFocus = () => {
  53. this.prevScroll = 0;
  54. const { dataSource } = this.props;
  55. this.setState({ page: 1, showArr: dataSource });
  56. };
  57. handleSearch = v => {
  58. const { dataSource } = this.props;
  59. v = v || "";
  60. const filterWord = v.trim().toLowerCase();
  61. const showArr = dataSource.filter(item =>
  62. item.label.toLowerCase().includes(filterWord)
  63. );
  64. this.setState({ page: 1, showArr });
  65. };
  66. render() {
  67. const { dataSource, ...rest } = this.props;
  68. const { showArr, page } = this.state;
  69. if (showArr.length > this.pageSize) {
  70. this.list = showArr.slice(0, this.pageSize * page);
  71. // 当value是外部赋值的时候,判断value是否在当前的list中,如果不在,单独加进来
  72. if (this.props.value != null) {
  73. let valueObj = this.list.find(item => item.key === this.props.value);
  74. if (!valueObj) {
  75. valueObj = dataSource.find(item => item.key === this.props.value);
  76. valueObj && this.list.unshift(valueObj);
  77. }
  78. }
  79. } else {
  80. this.list = showArr;
  81. }
  82. return (
  83. <Select
  84. {...rest}
  85. onChange={this.handleChange}
  86. filterOption={false}
  87. onPopupScroll={this.handlePopupScroll}
  88. onFocus={this.handleFocus}
  89. onSearch={this.handleSearch}
  90. >
  91. {this.list.map(opt => (
  92. <Option key={opt.key} value={opt.key}>
  93. {opt.label || opt.key}
  94. </Option>
  95. ))}
  96. </Select>
  97. );
  98. }
  99. }