scrolllist.stories.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React from 'react';
  2. import { storiesOf } from '@storybook/react';
  3. import ScrollList from '../index';
  4. import ScrollItem, {Item} from '../scrollItem';
  5. import Button from '../../button';
  6. const stories = storiesOf('scrollList', module);
  7. interface SelectValue {
  8. type?: string;
  9. index?:number;
  10. value: any
  11. }
  12. class ScrollListDemo extends React.Component<{}, {
  13. selectIndex1: number,
  14. selectIndex2:number,
  15. selectIndex3:number,
  16. [x:string]:any,
  17. }> {
  18. ampms : Item[];
  19. hours: Item[];
  20. minutes: Item[];
  21. constructor(props:any) {
  22. super(props);
  23. this.state = {
  24. selectIndex1: 1,
  25. selectIndex2: 1,
  26. selectIndex3: 1,
  27. };
  28. this.ampms = [
  29. {
  30. value: '上午',
  31. },
  32. {
  33. value: '下午',
  34. },
  35. ];
  36. this.hours = new Array(12).fill(0).map((itm, index) => {
  37. return {
  38. value: index + 1,
  39. };
  40. });
  41. this.minutes = new Array(60).fill(0).map((itm, index) => {
  42. return {
  43. value: index,
  44. disabled: Math.random() > 0.5 ? true : false,
  45. };
  46. });
  47. this.onSelectAP = this.onSelectAP.bind(this);
  48. this.onSelectHour = this.onSelectHour.bind(this);
  49. this.onSelectMinute = this.onSelectMinute.bind(this);
  50. this.handleClose = this.handleClose.bind(this);
  51. this.renderFooter = this.renderFooter.bind(this);
  52. }
  53. onSelectAP(data: SelectValue) {
  54. this.setState({
  55. ['selectIndex' + data.type]: data.index,
  56. });
  57. }
  58. onSelectHour(data:SelectValue) {
  59. console.log('You have choose the hour for: ', data.value);
  60. this.setState({
  61. ['selectIndex' + data.type]: data.index,
  62. });
  63. }
  64. onSelectMinute(data:SelectValue) {
  65. console.log('You have choose the minute for: ', data.value);
  66. this.setState({
  67. ['selectIndex' + data.type]: data.index,
  68. });
  69. }
  70. handleClose() {
  71. console.log('close');
  72. }
  73. renderFooter() {
  74. return (
  75. <Button size="small" type="primary" onClick={this.handleClose}>
  76. Ok
  77. </Button>
  78. );
  79. }
  80. render() {
  81. const scrollStyle = {
  82. border: 'unset',
  83. boxShadow: 'unset',
  84. };
  85. return (
  86. <ScrollList style={scrollStyle} header={'无限滚动列表'} footer={this.renderFooter()}>
  87. <ScrollItem
  88. mode="wheel"
  89. cycled={false}
  90. list={this.ampms}
  91. type={1}
  92. selectedIndex={this.state.selectIndex1}
  93. onSelect={this.onSelectAP}
  94. />
  95. <ScrollItem
  96. mode="wheel"
  97. cycled={true}
  98. list={this.hours}
  99. type={2}
  100. selectedIndex={this.state.selectIndex2}
  101. onSelect={this.onSelectHour}
  102. />
  103. <ScrollItem
  104. mode="wheel"
  105. cycled={true}
  106. list={this.minutes}
  107. type={3}
  108. selectedIndex={this.state.selectIndex3}
  109. onSelect={this.onSelectMinute}
  110. />
  111. </ScrollList>
  112. );
  113. }
  114. }
  115. stories.add('wheel list demo', () => <ScrollListDemo />);