|
| 1 | +/* eslint-disable class-methods-use-this */ |
| 2 | +/* eslint-disable react/prop-types */ |
| 3 | +/* eslint-disable jsx-a11y/no-static-element-interactions */ |
| 4 | +import React, { Component } from 'react'; |
| 5 | +import StackGrid, { transitions, easings } from '../../../src/'; |
| 6 | +import DemoControl from '../components/DemoControl'; |
| 7 | + |
| 8 | +const itemModifier = [ |
| 9 | + 'pattern1', |
| 10 | + 'pattern2', |
| 11 | + 'pattern3', |
| 12 | + 'gray', |
| 13 | + 'gray-light', |
| 14 | + 'gray-dark', |
| 15 | + 'yellow', |
| 16 | + 'pink', |
| 17 | + 'purple', |
| 18 | +]; |
| 19 | + |
| 20 | +export default class HorizontalFlow extends Component { |
| 21 | + constructor(props) { |
| 22 | + super(props); |
| 23 | + |
| 24 | + const items = []; |
| 25 | + |
| 26 | + for (let i = 0; i < 10; i += 1) { |
| 27 | + items.push(this.createItem()); |
| 28 | + } |
| 29 | + |
| 30 | + this.state = { |
| 31 | + items, |
| 32 | + duration: 480, |
| 33 | + columnWidth: 150, |
| 34 | + gutter: 5, |
| 35 | + easing: easings.quartOut, |
| 36 | + transition: 'fadeDown', |
| 37 | + }; |
| 38 | + } |
| 39 | + |
| 40 | + createItem() { |
| 41 | + const id = Math.random().toString(36).substr(2, 9); |
| 42 | + const height = Math.floor((Math.random() * (300 - 80)) + 80); |
| 43 | + const modifier = itemModifier[Math.floor(Math.random() * itemModifier.length)]; |
| 44 | + |
| 45 | + return { id, height, modifier }; |
| 46 | + } |
| 47 | + |
| 48 | + shuffleItems = () => { |
| 49 | + const newItems = [...this.state.items]; |
| 50 | + let i = newItems.length; |
| 51 | + |
| 52 | + while (i) { |
| 53 | + const j = Math.floor(Math.random() * i); |
| 54 | + const t = newItems[--i]; // eslint-disable-line no-plusplus |
| 55 | + newItems[i] = newItems[j]; |
| 56 | + newItems[j] = t; |
| 57 | + } |
| 58 | + |
| 59 | + this.setState({ items: newItems }); |
| 60 | + } |
| 61 | + |
| 62 | + prependItem = () => { |
| 63 | + this.setState({ |
| 64 | + items: [this.createItem(), ...this.state.items], |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + appendItem = () => { |
| 69 | + this.setState({ |
| 70 | + items: [...this.state.items, this.createItem()], |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + multipleAppendItem = () => { |
| 75 | + const newItems = []; |
| 76 | + |
| 77 | + for (let i = 0; i < 5; i += 1) { |
| 78 | + newItems.push(this.createItem()); |
| 79 | + } |
| 80 | + |
| 81 | + this.setState({ |
| 82 | + items: [...this.state.items, ...newItems], |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + removeItem = (id) => { |
| 87 | + this.setState({ |
| 88 | + items: this.state.items.filter(o => o.id !== id), |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + handleDurationChange = (duration) => { |
| 93 | + this.setState({ duration }); |
| 94 | + } |
| 95 | + |
| 96 | + handleColumnWidthChange = (columnWidth) => { |
| 97 | + this.setState({ columnWidth }); |
| 98 | + } |
| 99 | + |
| 100 | + handleGutterChange = (gutter) => { |
| 101 | + this.setState({ gutter }); |
| 102 | + } |
| 103 | + |
| 104 | + handleEasingChange = (easing) => { |
| 105 | + this.setState({ easing }); |
| 106 | + } |
| 107 | + |
| 108 | + handleTransitionChange = (transition) => { |
| 109 | + this.setState({ transition }); |
| 110 | + } |
| 111 | + |
| 112 | + render() { |
| 113 | + const { |
| 114 | + items, |
| 115 | + duration, |
| 116 | + columnWidth, |
| 117 | + gutter, |
| 118 | + easing, |
| 119 | + transition: transitionSelect, |
| 120 | + } = this.state; |
| 121 | + |
| 122 | + const transition = transitions[transitionSelect]; |
| 123 | + |
| 124 | + return ( |
| 125 | + <div> |
| 126 | + <DemoControl |
| 127 | + duration={duration} |
| 128 | + columnWidth={columnWidth} |
| 129 | + gutter={gutter} |
| 130 | + easing={easing} |
| 131 | + transition={transition} |
| 132 | + onShuffle={this.shuffleItems} |
| 133 | + onPrepend={this.prependItem} |
| 134 | + onAppend={this.appendItem} |
| 135 | + onMultipleAppend={this.multipleAppendItem} |
| 136 | + onDurationChange={this.handleDurationChange} |
| 137 | + onColumnWidthChange={this.handleColumnWidthChange} |
| 138 | + onGutterChange={this.handleGutterChange} |
| 139 | + onEasingChange={this.handleEasingChange} |
| 140 | + onTransitionChange={this.handleTransitionChange} |
| 141 | + /> |
| 142 | + |
| 143 | + <StackGrid |
| 144 | + horizontal |
| 145 | + duration={duration} |
| 146 | + columnWidth={columnWidth} |
| 147 | + gutterWidth={gutter} |
| 148 | + gutterHeight={gutter} |
| 149 | + easing={easing} |
| 150 | + appear={transition.appear} |
| 151 | + appeared={transition.appeared} |
| 152 | + enter={transition.enter} |
| 153 | + entered={transition.entered} |
| 154 | + leaved={transition.leaved} |
| 155 | + onLayout={() => { |
| 156 | + console.log('[DEMO] `onLayout()` has been called.'); // eslint-disable-line |
| 157 | + }} |
| 158 | + > |
| 159 | + {items.map(item => |
| 160 | + (<div |
| 161 | + key={item.id} |
| 162 | + className={`item item--${item.modifier}`} |
| 163 | + style={{ height: item.height }} |
| 164 | + onClick={() => this.removeItem(item.id)} |
| 165 | + />) |
| 166 | + )} |
| 167 | + </StackGrid> |
| 168 | + </div> |
| 169 | + ); |
| 170 | + } |
| 171 | +} |
0 commit comments