Skip to content

Commit 3f49e74

Browse files
Emil Sjolanderfacebook-github-bot
authored andcommitted
Add percentage support to react native
Summary: Adds support for percentage value in react native. syntax: property: 100 | property | '100%' supported properties: padding margin width height minWidth minHeight maxWidth maxHeight flexBasis ``` class Playground extends React.Component { render() { return ( <View style={{backgroundColor: 'white', padding: 10, paddingTop: 30, height: '100%'}}> <Text> If you want to quickly test out something, open the Playground.js file and start coding. </Text> <View style={{backgroundColor: 'red', height: 50, width: 50}}/> <View style={{backgroundColor: 'blue', height: '50%', width: '50%'}}/> </View> ); } } ``` Reviewed By: astreet Differential Revision: D4376549 fbshipit-source-id: c41d68a7555396f95d063a7527ee081773ac56dc
1 parent 00d5674 commit 3f49e74

File tree

16 files changed

+559
-208
lines changed

16 files changed

+559
-208
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* The examples provided by Facebook are for non-commercial testing and
3+
* evaluation purposes only.
4+
*
5+
* Facebook reserves all rights not expressly granted.
6+
*
7+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
8+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9+
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
10+
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
11+
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
12+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13+
*/
14+
15+
#import <XCTest/XCTest.h>
16+
17+
#import <React/RCTConvert.h>
18+
#import <React/RCTUtils.h>
19+
20+
@interface RCTConvert_YGValueTests : XCTestCase
21+
22+
@end
23+
24+
@implementation RCTConvert_YGValueTests
25+
26+
- (void)testUndefined
27+
{
28+
YGValue value = [RCTConvert YGValue:nil];
29+
XCTAssertEqual(value.unit, YGUnitUndefined);
30+
}
31+
32+
- (void)testNumberPoints
33+
{
34+
YGValue value = [RCTConvert YGValue:@100];
35+
XCTAssertEqual(value.unit, YGUnitPixel);
36+
XCTAssertEqual(value.value, 100);
37+
}
38+
39+
- (void)testStringPercent
40+
{
41+
YGValue value = [RCTConvert YGValue:@"100%"];
42+
XCTAssertEqual(value.unit, YGUnitPercent);
43+
XCTAssertEqual(value.value, 100);
44+
}
45+
46+
@end

Libraries/StyleSheet/LayoutPropTypes.js

Lines changed: 114 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,184 +30,256 @@ var LayoutPropTypes = {
3030
/** `width` sets the width of this component.
3131
*
3232
* It works similarly to `width` in CSS, but in React Native you
33-
* must use logical pixel units, rather than percents, ems, or any of that.
33+
* must use points or percentages. Ems and other units are not supported.
3434
* See https://developer.mozilla.org/en-US/docs/Web/CSS/width for more details.
3535
*/
36-
width: ReactPropTypes.number,
36+
width: ReactPropTypes.oneOfType([
37+
ReactPropTypes.number,
38+
ReactPropTypes.string,
39+
]),
3740

3841
/** `height` sets the height of this component.
3942
*
4043
* It works similarly to `height` in CSS, but in React Native you
41-
* must use logical pixel units, rather than percents, ems, or any of that.
44+
* must use points or percentages. Ems and other units are not supported.
4245
* See https://developer.mozilla.org/en-US/docs/Web/CSS/height for more details.
4346
*/
44-
height: ReactPropTypes.number,
47+
height: ReactPropTypes.oneOfType([
48+
ReactPropTypes.number,
49+
ReactPropTypes.string,
50+
]),
4551

4652
/** `top` is the number of logical pixels to offset the top edge of
4753
* this component.
4854
*
49-
* It works similarly to `top` in CSS, but in React Native you must
50-
* use logical pixel units, rather than percents, ems, or any of that.
55+
* It works similarly to `top` in CSS, but in React Native you
56+
* must use points or percentages. Ems and other units are not supported.
5157
*
5258
* See https://developer.mozilla.org/en-US/docs/Web/CSS/top
5359
* for more details of how `top` affects layout.
5460
*/
55-
top: ReactPropTypes.number,
61+
top: ReactPropTypes.oneOfType([
62+
ReactPropTypes.number,
63+
ReactPropTypes.string,
64+
]),
5665

5766
/** `left` is the number of logical pixels to offset the left edge of
5867
* this component.
5968
*
60-
* It works similarly to `left` in CSS, but in React Native you must
61-
* use logical pixel units, rather than percents, ems, or any of that.
69+
* It works similarly to `left` in CSS, but in React Native you
70+
* must use points or percentages. Ems and other units are not supported.
6271
*
6372
* See https://developer.mozilla.org/en-US/docs/Web/CSS/left
6473
* for more details of how `left` affects layout.
6574
*/
66-
left: ReactPropTypes.number,
75+
left: ReactPropTypes.oneOfType([
76+
ReactPropTypes.number,
77+
ReactPropTypes.string,
78+
]),
6779

6880
/** `right` is the number of logical pixels to offset the right edge of
6981
* this component.
7082
*
71-
* It works similarly to `right` in CSS, but in React Native you must
72-
* use logical pixel units, rather than percents, ems, or any of that.
83+
* It works similarly to `right` in CSS, but in React Native you
84+
* must use points or percentages. Ems and other units are not supported.
7385
*
7486
* See https://developer.mozilla.org/en-US/docs/Web/CSS/right
7587
* for more details of how `right` affects layout.
7688
*/
77-
right: ReactPropTypes.number,
89+
right: ReactPropTypes.oneOfType([
90+
ReactPropTypes.number,
91+
ReactPropTypes.string,
92+
]),
7893

7994
/** `bottom` is the number of logical pixels to offset the bottom edge of
8095
* this component.
8196
*
82-
* It works similarly to `bottom` in CSS, but in React Native you must
83-
* use logical pixel units, rather than percents, ems, or any of that.
97+
* It works similarly to `bottom` in CSS, but in React Native you
98+
* must use points or percentages. Ems and other units are not supported.
8499
*
85100
* See https://developer.mozilla.org/en-US/docs/Web/CSS/bottom
86101
* for more details of how `bottom` affects layout.
87102
*/
88-
bottom: ReactPropTypes.number,
103+
bottom: ReactPropTypes.oneOfType([
104+
ReactPropTypes.number,
105+
ReactPropTypes.string,
106+
]),
89107

90108
/** `minWidth` is the minimum width for this component, in logical pixels.
91109
*
92110
* It works similarly to `min-width` in CSS, but in React Native you
93-
* must use logical pixel units, rather than percents, ems, or any of that.
111+
* must use points or percentages. Ems and other units are not supported.
94112
*
95113
* See https://developer.mozilla.org/en-US/docs/Web/CSS/min-width
96114
* for more details.
97115
*/
98-
minWidth: ReactPropTypes.number,
116+
minWidth: ReactPropTypes.oneOfType([
117+
ReactPropTypes.number,
118+
ReactPropTypes.string,
119+
]),
99120

100121
/** `maxWidth` is the maximum width for this component, in logical pixels.
101122
*
102123
* It works similarly to `max-width` in CSS, but in React Native you
103-
* must use logical pixel units, rather than percents, ems, or any of that.
124+
* must use points or percentages. Ems and other units are not supported.
104125
*
105126
* See https://developer.mozilla.org/en-US/docs/Web/CSS/max-width
106127
* for more details.
107128
*/
108-
maxWidth: ReactPropTypes.number,
129+
maxWidth: ReactPropTypes.oneOfType([
130+
ReactPropTypes.number,
131+
ReactPropTypes.string,
132+
]),
109133

110134
/** `minHeight` is the minimum height for this component, in logical pixels.
111135
*
112136
* It works similarly to `min-height` in CSS, but in React Native you
113-
* must use logical pixel units, rather than percents, ems, or any of that.
137+
* must use points or percentages. Ems and other units are not supported.
114138
*
115139
* See https://developer.mozilla.org/en-US/docs/Web/CSS/min-height
116140
* for more details.
117141
*/
118-
minHeight: ReactPropTypes.number,
142+
minHeight: ReactPropTypes.oneOfType([
143+
ReactPropTypes.number,
144+
ReactPropTypes.string,
145+
]),
119146

120147
/** `maxHeight` is the maximum height for this component, in logical pixels.
121148
*
122149
* It works similarly to `max-height` in CSS, but in React Native you
123-
* must use logical pixel units, rather than percents, ems, or any of that.
150+
* must use points or percentages. Ems and other units are not supported.
124151
*
125152
* See https://developer.mozilla.org/en-US/docs/Web/CSS/max-height
126153
* for more details.
127154
*/
128-
maxHeight: ReactPropTypes.number,
155+
maxHeight: ReactPropTypes.oneOfType([
156+
ReactPropTypes.number,
157+
ReactPropTypes.string,
158+
]),
129159

130160
/** Setting `margin` has the same effect as setting each of
131161
* `marginTop`, `marginLeft`, `marginBottom`, and `marginRight`.
132162
* See https://developer.mozilla.org/en-US/docs/Web/CSS/margin
133163
* for more details.
134164
*/
135-
margin: ReactPropTypes.number,
165+
margin: ReactPropTypes.oneOfType([
166+
ReactPropTypes.number,
167+
ReactPropTypes.string,
168+
]),
136169

137170
/** Setting `marginVertical` has the same effect as setting both
138171
* `marginTop` and `marginBottom`.
139172
*/
140-
marginVertical: ReactPropTypes.number,
173+
marginVertical: ReactPropTypes.oneOfType([
174+
ReactPropTypes.number,
175+
ReactPropTypes.string,
176+
]),
141177

142178
/** Setting `marginHorizontal` has the same effect as setting
143179
* both `marginLeft` and `marginRight`.
144180
*/
145-
marginHorizontal: ReactPropTypes.number,
181+
marginHorizontal: ReactPropTypes.oneOfType([
182+
ReactPropTypes.number,
183+
ReactPropTypes.string,
184+
]),
146185

147186
/** `marginTop` works like `margin-top` in CSS.
148187
* See https://developer.mozilla.org/en-US/docs/Web/CSS/margin-top
149188
* for more details.
150189
*/
151-
marginTop: ReactPropTypes.number,
190+
marginTop: ReactPropTypes.oneOfType([
191+
ReactPropTypes.number,
192+
ReactPropTypes.string,
193+
]),
152194

153195
/** `marginBottom` works like `margin-bottom` in CSS.
154196
* See https://developer.mozilla.org/en-US/docs/Web/CSS/margin-bottom
155197
* for more details.
156198
*/
157-
marginBottom: ReactPropTypes.number,
199+
marginBottom: ReactPropTypes.oneOfType([
200+
ReactPropTypes.number,
201+
ReactPropTypes.string,
202+
]),
158203

159204
/** `marginLeft` works like `margin-left` in CSS.
160205
* See https://developer.mozilla.org/en-US/docs/Web/CSS/margin-left
161206
* for more details.
162207
*/
163-
marginLeft: ReactPropTypes.number,
208+
marginLeft: ReactPropTypes.oneOfType([
209+
ReactPropTypes.number,
210+
ReactPropTypes.string,
211+
]),
164212

165213
/** `marginRight` works like `margin-right` in CSS.
166214
* See https://developer.mozilla.org/en-US/docs/Web/CSS/margin-right
167215
* for more details.
168216
*/
169-
marginRight: ReactPropTypes.number,
217+
marginRight: ReactPropTypes.oneOfType([
218+
ReactPropTypes.number,
219+
ReactPropTypes.string,
220+
]),
170221

171222
/** Setting `padding` has the same effect as setting each of
172223
* `paddingTop`, `paddingBottom`, `paddingLeft`, and `paddingRight`.
173224
* See https://developer.mozilla.org/en-US/docs/Web/CSS/padding
174225
* for more details.
175226
*/
176-
padding: ReactPropTypes.number,
227+
padding: ReactPropTypes.oneOfType([
228+
ReactPropTypes.number,
229+
ReactPropTypes.string,
230+
]),
177231

178232
/** Setting `paddingVertical` is like setting both of
179233
* `paddingTop` and `paddingBottom`.
180234
*/
181-
paddingVertical: ReactPropTypes.number,
235+
paddingVertical: ReactPropTypes.oneOfType([
236+
ReactPropTypes.number,
237+
ReactPropTypes.string,
238+
]),
182239

183240
/** Setting `paddingHorizontal` is like setting both of
184241
* `paddingLeft` and `paddingRight`.
185242
*/
186-
paddingHorizontal: ReactPropTypes.number,
243+
paddingHorizontal: ReactPropTypes.oneOfType([
244+
ReactPropTypes.number,
245+
ReactPropTypes.string,
246+
]),
187247

188248
/** `paddingTop` works like `padding-top` in CSS.
189249
* See https://developer.mozilla.org/en-US/docs/Web/CSS/padding-top
190250
* for more details.
191251
*/
192-
paddingTop: ReactPropTypes.number,
252+
paddingTop: ReactPropTypes.oneOfType([
253+
ReactPropTypes.number,
254+
ReactPropTypes.string,
255+
]),
193256

194257
/** `paddingBottom` works like `padding-bottom` in CSS.
195258
* See https://developer.mozilla.org/en-US/docs/Web/CSS/padding-bottom
196259
* for more details.
197260
*/
198-
paddingBottom: ReactPropTypes.number,
261+
paddingBottom: ReactPropTypes.oneOfType([
262+
ReactPropTypes.number,
263+
ReactPropTypes.string,
264+
]),
199265

200266
/** `paddingLeft` works like `padding-left` in CSS.
201267
* See https://developer.mozilla.org/en-US/docs/Web/CSS/padding-left
202268
* for more details.
203269
*/
204-
paddingLeft: ReactPropTypes.number,
270+
paddingLeft: ReactPropTypes.oneOfType([
271+
ReactPropTypes.number,
272+
ReactPropTypes.string,
273+
]),
205274

206275
/** `paddingRight` works like `padding-right` in CSS.
207276
* See https://developer.mozilla.org/en-US/docs/Web/CSS/padding-right
208277
* for more details.
209278
*/
210-
paddingRight: ReactPropTypes.number,
279+
paddingRight: ReactPropTypes.oneOfType([
280+
ReactPropTypes.number,
281+
ReactPropTypes.string,
282+
]),
211283

212284
/** `borderWidth` works like `border-width` in CSS.
213285
* See https://developer.mozilla.org/en-US/docs/Web/CSS/border-width
@@ -365,7 +437,10 @@ var LayoutPropTypes = {
365437
flex: ReactPropTypes.number,
366438
flexGrow: ReactPropTypes.number,
367439
flexShrink: ReactPropTypes.number,
368-
flexBasis: ReactPropTypes.number,
440+
flexBasis: ReactPropTypes.oneOfType([
441+
ReactPropTypes.number,
442+
ReactPropTypes.string,
443+
]),
369444

370445
/**
371446
* Aspect ratio control the size of the undefined dimension of a node. Aspect ratio is a

React/Base/RCTConvert.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
#import <QuartzCore/QuartzCore.h>
1111
#import <UIKit/UIKit.h>
1212

13-
#import <yoga/Yoga.h>
1413
#import <React/RCTAnimationType.h>
1514
#import <React/RCTBorderStyle.h>
1615
#import <React/RCTDefines.h>
1716
#import <React/RCTLog.h>
1817
#import <React/RCTPointerEvents.h>
1918
#import <React/RCTTextDecorationLineType.h>
19+
#import <yoga/Yoga.h>
2020

2121
/**
2222
* This class provides a collection of conversion functions for mapping
@@ -90,6 +90,8 @@ typedef NSURL RCTFileURL;
9090
+ (UIColor *)UIColor:(id)json;
9191
+ (CGColorRef)CGColor:(id)json CF_RETURNS_NOT_RETAINED;
9292

93+
+ (YGValue)YGValue:(id)json;
94+
9395
+ (NSArray<NSArray *> *)NSArrayArray:(id)json;
9496
+ (NSArray<NSString *> *)NSStringArray:(id)json;
9597
+ (NSArray<NSArray<NSString *> *> *)NSStringArrayArray:(id)json;

0 commit comments

Comments
 (0)