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 | 'use strict';
|
26 |
|
27 | var assign = require('object-assign');
|
28 | var PropTypes = require('prop-types');
|
29 | var React = require('react');
|
30 | var ReactART = require('react-art');
|
31 |
|
32 | var createReactClass = require('create-react-class');
|
33 |
|
34 | var Shape = ReactART.Shape;
|
35 | var Path = ReactART.Path;
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | var Rectangle = createReactClass({
|
42 | displayName: 'Rectangle',
|
43 |
|
44 | propTypes: {
|
45 | width: PropTypes.number.isRequired,
|
46 | height: PropTypes.number.isRequired,
|
47 | radius: PropTypes.number,
|
48 | radiusTopLeft: PropTypes.number,
|
49 | radiusTopRight: PropTypes.number,
|
50 | radiusBottomRight: PropTypes.number,
|
51 | radiusBottomLeft: PropTypes.number,
|
52 | },
|
53 |
|
54 | render: function render() {
|
55 | var width = this.props.width;
|
56 | var height = this.props.height;
|
57 | var radius = this.props.radius ? this.props.radius : 0;
|
58 |
|
59 |
|
60 |
|
61 | var tl = this.props.radiusTopLeft ? this.props.radiusTopLeft : radius;
|
62 | var tr = this.props.radiusTopRight ? this.props.radiusTopRight : radius;
|
63 | var br = this.props.radiusBottomRight
|
64 | ? this.props.radiusBottomRight
|
65 | : radius;
|
66 | var bl = this.props.radiusBottomLeft ? this.props.radiusBottomLeft : radius;
|
67 |
|
68 | var path = Path();
|
69 |
|
70 |
|
71 |
|
72 | if (width < 0) {
|
73 | path.move(width, 0);
|
74 | width = -width;
|
75 | }
|
76 | if (height < 0) {
|
77 | path.move(0, height);
|
78 | height = -height;
|
79 | }
|
80 | if (tl < 0) {
|
81 | tl = 0;
|
82 | }
|
83 | if (tr < 0) {
|
84 | tr = 0;
|
85 | }
|
86 | if (br < 0) {
|
87 | br = 0;
|
88 | }
|
89 | if (bl < 0) {
|
90 | bl = 0;
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 | if (tl + tr > width) {
|
96 | tl = 0;
|
97 | tr = 0;
|
98 | }
|
99 | if (bl + br > width) {
|
100 | bl = 0;
|
101 | br = 0;
|
102 | }
|
103 | if (tl + bl > height) {
|
104 | tl = 0;
|
105 | bl = 0;
|
106 | }
|
107 | if (tr + br > height) {
|
108 | tr = 0;
|
109 | br = 0;
|
110 | }
|
111 |
|
112 | path.move(0, tl);
|
113 |
|
114 | if (tl > 0) {
|
115 | path.arc(tl, -tl);
|
116 | }
|
117 | path.line(width - (tr + tl), 0);
|
118 |
|
119 | if (tr > 0) {
|
120 | path.arc(tr, tr);
|
121 | }
|
122 | path.line(0, height - (tr + br));
|
123 |
|
124 | if (br > 0) {
|
125 | path.arc(-br, br);
|
126 | }
|
127 | path.line(-width + (br + bl), 0);
|
128 |
|
129 | if (bl > 0) {
|
130 | path.arc(-bl, -bl);
|
131 | }
|
132 | path.line(0, -height + (bl + tl));
|
133 |
|
134 | return React.createElement(Shape, assign({}, this.props, {d: path}));
|
135 | },
|
136 | });
|
137 |
|
138 | module.exports = Rectangle;
|