File size: 1,399 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React from 'react';
import propTypes from 'prop-types';

import style from './index.less';
import { blockShape } from '../../unit/const';

const xy = { // 方块在下一个中的坐标
  I: [1, 0],
  L: [0, 0],
  J: [0, 0],
  Z: [0, 0],
  S: [0, 0],
  O: [0, 1],
  T: [0, 0],
};

const empty = [
  [0, 0, 0, 0],
  [0, 0, 0, 0],
];

export default class Next extends React.Component {
  constructor() {
    super();
    this.state = {
      block: empty,
    };
  }
  componentWillMount() {
    this.build(this.props.data);
  }
  componentWillReceiveProps(nextProps) {
    this.build(nextProps.data);
  }
  shouldComponentUpdate(nextProps) {
    return nextProps.data !== this.props.data;
  }
  build(type) {
    const shape = blockShape[type];
    const block = empty.map(e => ([...e]));
    shape.forEach((m, k1) => {
      m.forEach((n, k2) => {
        if (n) {
          block[k1 + xy[type][0]][k2 + xy[type][1]] = 1;
        }
      });
    });
    this.setState({ block });
  }
  render() {
    return (
      <div className={style.next}>
        {
          this.state.block.map((arr, k1) => (
            <div key={k1}>
              {
                arr.map((e, k2) => (
                  <b className={e ? 'c' : ''} key={k2} />
                ))
              }
            </div>
          ))
        }
      </div>
    );
  }
}

Next.propTypes = {
  data: propTypes.string,
};