|
10 | 10 |
|
11 | 11 | 'use strict';
|
12 | 12 |
|
| 13 | +const I18nManager = require('I18nManager'); |
| 14 | + |
13 | 15 | /**
|
14 |
| - * Resolve a style property into it's component parts, e.g. |
| 16 | + * Resolve a style property into its component parts. |
| 17 | + * |
| 18 | + * For example: |
15 | 19 | *
|
16 |
| - * resolveProperties('margin', {margin: 5, marginBottom: 10}) |
17 |
| - * -> |
18 |
| - * {top: 5, left: 5, right: 5, bottom: 10} |
| 20 | + * > resolveProperties('margin', {margin: 5, marginBottom: 10}) |
| 21 | + * {top: 5, left: 5, right: 5, bottom: 10} |
19 | 22 | *
|
20 |
| - * If none are set, returns false. |
| 23 | + * If no parts exist, this returns null. |
21 | 24 | */
|
22 |
| -function resolveBoxStyle(prefix: string, style: Object): ?Object { |
23 |
| - const res = {}; |
24 |
| - const subs = ['top', 'left', 'bottom', 'right']; |
25 |
| - let set = false; |
26 |
| - subs.forEach(sub => { |
27 |
| - res[sub] = style[prefix] || 0; |
28 |
| - }); |
29 |
| - if (style[prefix]) { |
30 |
| - set = true; |
31 |
| - } |
32 |
| - if (style[prefix + 'Vertical']) { |
33 |
| - res.top = res.bottom = style[prefix + 'Vertical']; |
34 |
| - set = true; |
| 25 | +function resolveBoxStyle( |
| 26 | + prefix: string, |
| 27 | + style: Object, |
| 28 | +): ?$ReadOnly<{| |
| 29 | + bottom: number, |
| 30 | + left: number, |
| 31 | + right: number, |
| 32 | + top: number, |
| 33 | +|}> { |
| 34 | + let hasParts = false; |
| 35 | + const result = { |
| 36 | + bottom: 0, |
| 37 | + left: 0, |
| 38 | + right: 0, |
| 39 | + top: 0, |
| 40 | + }; |
| 41 | + |
| 42 | + // TODO: Fix issues with multiple properties affecting the same side. |
| 43 | + |
| 44 | + const styleForAll = style[prefix]; |
| 45 | + if (styleForAll != null) { |
| 46 | + for (const key of Object.keys(result)) { |
| 47 | + result[key] = styleForAll; |
| 48 | + } |
| 49 | + hasParts = true; |
35 | 50 | }
|
36 |
| - if (style[prefix + 'Horizontal']) { |
37 |
| - res.left = res.right = style[prefix + 'Horizontal']; |
38 |
| - set = true; |
| 51 | + |
| 52 | + const styleForHorizontal = style[prefix + 'Horizontal']; |
| 53 | + if (styleForHorizontal != null) { |
| 54 | + result.left = styleForHorizontal; |
| 55 | + result.right = styleForHorizontal; |
| 56 | + hasParts = true; |
| 57 | + } else { |
| 58 | + const styleForLeft = style[prefix + 'Left']; |
| 59 | + if (styleForLeft != null) { |
| 60 | + result.left = styleForLeft; |
| 61 | + hasParts = true; |
| 62 | + } |
| 63 | + |
| 64 | + const styleForRight = style[prefix + 'Right']; |
| 65 | + if (styleForRight != null) { |
| 66 | + result.right = styleForRight; |
| 67 | + hasParts = true; |
| 68 | + } |
| 69 | + |
| 70 | + const styleForEnd = style[prefix + 'End']; |
| 71 | + if (styleForEnd != null) { |
| 72 | + if (I18nManager.isRTL && I18nManager.doLeftAndRightSwapInRTL) { |
| 73 | + result.left = styleForEnd; |
| 74 | + } else { |
| 75 | + result.right = styleForEnd; |
| 76 | + } |
| 77 | + hasParts = true; |
| 78 | + } |
| 79 | + const styleForStart = style[prefix + 'Start']; |
| 80 | + if (styleForStart != null) { |
| 81 | + if (I18nManager.isRTL && I18nManager.doLeftAndRightSwapInRTL) { |
| 82 | + result.right = styleForStart; |
| 83 | + } else { |
| 84 | + result.left = styleForStart; |
| 85 | + } |
| 86 | + hasParts = true; |
| 87 | + } |
39 | 88 | }
|
40 |
| - subs.forEach(sub => { |
41 |
| - const val = style[prefix + capFirst(sub)]; |
42 |
| - if (val) { |
43 |
| - res[sub] = val; |
44 |
| - set = true; |
| 89 | + |
| 90 | + const styleForVertical = style[prefix + 'Vertical']; |
| 91 | + if (styleForVertical != null) { |
| 92 | + result.bottom = styleForVertical; |
| 93 | + result.top = styleForVertical; |
| 94 | + hasParts = true; |
| 95 | + } else { |
| 96 | + const styleForBottom = style[prefix + 'Bottom']; |
| 97 | + if (styleForBottom != null) { |
| 98 | + result.bottom = styleForBottom; |
| 99 | + hasParts = true; |
| 100 | + } |
| 101 | + |
| 102 | + const styleForTop = style[prefix + 'Top']; |
| 103 | + if (styleForTop != null) { |
| 104 | + result.top = styleForTop; |
| 105 | + hasParts = true; |
45 | 106 | }
|
46 |
| - }); |
47 |
| - if (!set) { |
48 |
| - return; |
49 | 107 | }
|
50 |
| - return res; |
51 |
| -} |
52 | 108 |
|
53 |
| -function capFirst(text) { |
54 |
| - return text[0].toUpperCase() + text.slice(1); |
| 109 | + return hasParts ? result : null; |
55 | 110 | }
|
56 | 111 |
|
57 | 112 | module.exports = resolveBoxStyle;
|
0 commit comments