Skip to content

Commit c08a459

Browse files
emilisbethanshar
authored andcommitted
Performance improvements (#470)
* Do not call renderSelection() in Card component if 'selected' prop is undefined * Refactored borderRadius and backgroundColor modifiers * Rename variable * Add numbers validation to border radius regex * Refactor flex modifier * Use last bg color modifier
1 parent de7cbbd commit c08a459

File tree

4 files changed

+25
-40
lines changed

4 files changed

+25
-40
lines changed

src/commons/modifiers.js

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ export function extractColorValue(props) {
1818
return Colors[color];
1919
}
2020

21-
// todo: refactor this and use BACKGROUND_KEY_PATTERN
2221
export function extractBackgroundColorValue(props) {
2322
let backgroundColor;
24-
_.forEach(Colors, (value, key) => {
25-
if (props[`background-${key}`] === true || props[`bg-${key}`] === true) {
26-
backgroundColor = value;
27-
}
28-
});
23+
24+
const keys = Object.keys(props);
25+
const bgProp = _.findLast(keys, (prop) => Colors.getBackgroundKeysPattern().test(prop));
26+
if (bgProp) {
27+
const key = bgProp.replace(Colors.getBackgroundKeysPattern(), '');
28+
backgroundColor = Colors[key];
29+
}
2930

3031
return backgroundColor;
3132
}
@@ -152,13 +153,11 @@ export function extractFlexStyle(props) {
152153
flexG: 'flexGrow',
153154
flexS: 'flexShrink',
154155
};
155-
const flexPropKey = _.chain(props)
156-
.keys(props)
157-
.filter(key => FLEX_KEY_PATTERN.test(key))
158-
.last()
159-
.value();
160-
if (flexPropKey && props[flexPropKey] === true) {
161-
let [flexKey, flexValue] = flexPropKey.split('-');
156+
157+
const keys = Object.keys(props);
158+
const flexProp = keys.find((item) => FLEX_KEY_PATTERN.test(item));
159+
if (flexProp && props[flexProp] === true) {
160+
let [flexKey, flexValue] = flexProp.split('-');
162161
flexKey = STYLE_KEY_CONVERTERS[flexKey];
163162
flexValue = _.isEmpty(flexValue) ? 1 : Number(flexValue);
164163

@@ -167,16 +166,13 @@ export function extractFlexStyle(props) {
167166
}
168167

169168
export function extractBorderRadiusValue(props) {
170-
const borderRadiusPropsKeys = _.chain(props)
171-
.keys()
172-
.filter(key => BorderRadiuses.getKeysPattern().test(key))
173-
.value();
174169
let borderRadius;
175-
_.forEach(borderRadiusPropsKeys, key => {
176-
if (props[key] === true) {
177-
borderRadius = BorderRadiuses[key];
178-
}
179-
});
170+
171+
const keys = Object.keys(props);
172+
const radiusProp = keys.find((prop) => BorderRadiuses.getKeysPattern().test(prop));
173+
if (radiusProp) {
174+
borderRadius = BorderRadiuses[radiusProp];
175+
}
180176

181177
return borderRadius;
182178
}

src/components/card/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ class Card extends BaseComponent {
9292
};
9393

9494
static defaultProps = {
95-
selected: false,
9695
enableShadow: true,
9796
};
9897

@@ -170,9 +169,13 @@ class Card extends BaseComponent {
170169
}
171170

172171
renderSelection() {
173-
const {selectionOptions, borderRadius} = this.getThemeProps();
172+
const {selectionOptions, borderRadius, selected} = this.getThemeProps();
174173
const {animatedSelected} = this.state;
175174

175+
if (_.isUndefined(selected)) {
176+
return null;
177+
}
178+
176179
return (
177180
<Animated.View
178181
style={[this.styles.selectedBorder, borderRadius && {borderRadius}, {opacity: animatedSelected}]}

src/style/borderRadiuses.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,7 @@ class BorderRadiuses {
1818
}
1919

2020
getKeysPattern() {
21-
return new RegExp(
22-
_.chain(this)
23-
.keys()
24-
.map(key => [`${key}`])
25-
.flatten()
26-
.join('|')
27-
.value(),
28-
);
21+
return /^(br[0-9]+)/;
2922
}
3023
}
3124

src/style/colors.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,7 @@ class Colors {
4949
}
5050

5151
getBackgroundKeysPattern() {
52-
return new RegExp(
53-
_.chain(this)
54-
.keys()
55-
.map(key => [`bg-${key}`, `background-${key}`])
56-
.flatten()
57-
.join('|')
58-
.value(),
59-
);
52+
return /^(bg-|background-)/;
6053
}
6154

6255
isEmpty(color) {

0 commit comments

Comments
 (0)