|
1 | | -import Align, { type RefAlign } from 'rc-align'; |
2 | | -import React, { Component } from 'react'; |
3 | | - |
4 | | -const allPoints = ['tl', 'tc', 'tr', 'cl', 'cc', 'cr', 'bl', 'bc', 'br']; |
5 | | - |
6 | | -interface TestState { |
7 | | - monitor: boolean; |
8 | | - random: boolean; |
9 | | - disabled: boolean; |
10 | | - randomWidth: number; |
11 | | - align: any; |
12 | | - sourceWidth: number; |
13 | | -} |
14 | | - |
15 | | -class Test extends Component<{}, TestState> { |
16 | | - state = { |
17 | | - monitor: true, |
18 | | - random: false, |
19 | | - disabled: false, |
20 | | - randomWidth: 100, |
21 | | - align: { |
22 | | - points: ['cc', 'cc'], |
23 | | - }, |
24 | | - sourceWidth: 50, |
25 | | - }; |
26 | | - |
27 | | - id: NodeJS.Timer; |
28 | | - $container: HTMLElement; |
29 | | - $align: RefAlign; |
30 | | - |
31 | | - componentDidMount() { |
32 | | - this.id = setInterval(() => { |
33 | | - const { random } = this.state; |
34 | | - if (random) { |
35 | | - this.setState({ |
36 | | - randomWidth: 60 + 40 * Math.random(), |
37 | | - }); |
38 | | - } |
39 | | - }, 1000); |
40 | | - } |
41 | | - |
42 | | - componentWillUnmount() { |
43 | | - clearInterval(this.id); |
44 | | - } |
45 | | - |
46 | | - getTarget = () => { |
47 | | - if (!this.$container) { |
48 | | - // parent ref not attached |
49 | | - this.$container = document.getElementById('container'); |
50 | | - } |
51 | | - return this.$container; |
52 | | - }; |
53 | | - |
54 | | - containerRef = ele => { |
55 | | - this.$container = ele; |
56 | | - }; |
57 | | - |
58 | | - alignRef = node => { |
59 | | - this.$align = node; |
60 | | - }; |
61 | | - |
62 | | - toggleMonitor = () => { |
63 | | - this.setState(({ monitor }) => ({ |
64 | | - monitor: !monitor, |
65 | | - })); |
66 | | - }; |
67 | | - |
68 | | - toggleRandom = () => { |
69 | | - this.setState(({ random }) => ({ |
70 | | - random: !random, |
71 | | - })); |
72 | | - }; |
73 | | - |
74 | | - toggleDisabled = () => { |
75 | | - this.setState(({ disabled }) => ({ |
76 | | - disabled: !disabled, |
77 | | - })); |
78 | | - }; |
79 | | - |
80 | | - randomAlign = () => { |
81 | | - const randomPoints = []; |
82 | | - randomPoints.push(allPoints[Math.floor(Math.random() * 100) % allPoints.length]); |
83 | | - randomPoints.push(allPoints[Math.floor(Math.random() * 100) % allPoints.length]); |
84 | | - this.setState({ |
85 | | - align: { |
86 | | - points: randomPoints, |
87 | | - }, |
88 | | - }); |
89 | | - }; |
90 | | - |
91 | | - forceAlign = () => { |
92 | | - this.$align.forceAlign(); |
93 | | - }; |
94 | | - |
95 | | - toggleSourceSize = () => { |
96 | | - this.setState({ |
97 | | - // eslint-disable-next-line react/no-access-state-in-setstate |
98 | | - sourceWidth: this.state.sourceWidth + 10, |
99 | | - }); |
100 | | - }; |
101 | | - |
102 | | - render() { |
103 | | - const { random, randomWidth } = this.state; |
104 | | - |
105 | | - return ( |
106 | | - <div |
107 | | - style={{ |
108 | | - margin: 50, |
109 | | - }} |
110 | | - > |
111 | | - <p> |
112 | | - <button type="button" onClick={this.forceAlign}> |
113 | | - Force align |
114 | | - </button> |
115 | | - |
116 | | - <button type="button" onClick={this.toggleSourceSize}> |
117 | | - Resize Source |
118 | | - </button> |
119 | | - |
120 | | - <button type="button" onClick={this.randomAlign}> |
121 | | - Random Align |
122 | | - </button> |
123 | | - |
124 | | - <label> |
125 | | - <input type="checkbox" checked={this.state.monitor} onChange={this.toggleMonitor} /> |
126 | | - Monitor window resize |
127 | | - </label> |
128 | | - <label> |
129 | | - <input type="checkbox" checked={this.state.random} onChange={this.toggleRandom} /> |
130 | | - Random Size |
131 | | - </label> |
132 | | - <label> |
133 | | - <input type="checkbox" checked={this.state.disabled} onChange={this.toggleDisabled} /> |
134 | | - Disabled |
135 | | - </label> |
136 | | - </p> |
137 | | - <div |
138 | | - ref={this.containerRef} |
139 | | - id="container" |
140 | | - style={{ |
141 | | - width: '80%', |
142 | | - // maxWidth: 1000, |
143 | | - height: 500, |
144 | | - border: '1px solid red', |
145 | | - ...(random |
146 | | - ? { |
147 | | - width: `${randomWidth}%`, |
148 | | - } |
149 | | - : null), |
150 | | - }} |
151 | | - > |
152 | | - <Align |
153 | | - ref={this.alignRef} |
154 | | - target={this.getTarget} |
155 | | - monitorWindowResize={this.state.monitor} |
156 | | - align={this.state.align} |
157 | | - disabled={this.state.disabled} |
158 | | - > |
159 | | - <div |
160 | | - style={{ |
161 | | - position: 'absolute', |
162 | | - width: this.state.sourceWidth, |
163 | | - height: 50, |
164 | | - background: 'yellow', |
165 | | - }} |
166 | | - > |
167 | | - <input defaultValue="source" style={{ width: '100%' }} /> |
168 | | - </div> |
169 | | - </Align> |
170 | | - </div> |
171 | | - </div> |
172 | | - ); |
173 | | - } |
174 | | -} |
175 | | - |
176 | | -export default Test; |
| 1 | +export default () => 'Hello World'; |
0 commit comments