Skip to content

Commit

Permalink
Merge pull request #178 from GetStream/vishal/prop-type-fix
Browse files Browse the repository at this point in the history
Fixing theming logic for nested styles
  • Loading branch information
vishalnarkhede committed Apr 9, 2021
2 parents 7060571 + be2ccd8 commit 683aea5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/components/BackButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default class BackButton extends React.Component {
const { blue, pressed } = this.props;

return (
<TouchableOpacity style={styles.backButton} onPress={pressed}>
<TouchableOpacity style={styles.container} onPress={pressed}>
<Image
source={
blue
Expand Down
5 changes: 1 addition & 4 deletions src/components/StatusUpdateForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,7 @@ class StatusUpdateFormInner extends React.Component {
<UrlPreview
onPressDismiss={this._onPressDismiss}
og={this.state.og}
styles={
// $FlowFixMe
this.props.styles.urlPreview
}
styles={this.props.styles.urlPreview}
/>
)}

Expand Down
36 changes: 12 additions & 24 deletions src/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const styles = {
},
}),
backButton: StyleSheet.create({
backButton: {
container: {
width: 50,
paddingRight: 6,
paddingTop: 6,
Expand Down Expand Up @@ -445,20 +445,6 @@ export const styles = {
}),
};

const depthOf = function(object) {
let level = 1;
let key;
for (key in object) {
if (!object.hasOwnProperty(key)) continue;

if (typeof object[key] == 'object') {
const depth = depthOf(object[key]) + 1;
level = Math.max(depth, level);
}
}
return level;
};

export function getStyle(styleName) {
return styles[styleName] || {};
}
Expand All @@ -472,21 +458,23 @@ export function buildStylesheet(styleName, styleOverwrites) {
if (!styleOverwrites || Object.keys(styleOverwrites).length === 0) {
return baseStyle;
}
const falseObj = {};

const base = Object.keys(baseStyle)
.map((k) => ({ [k]: StyleSheet.flatten(baseStyle[k]) }))
.reduce((accumulated, v) => Object.assign(accumulated, v), {});

const styleKeysExceptStyleName = Object.keys(styles).filter(
(k) => k !== styleName,
);
const topLevelOverwrites = Object.keys(styleOverwrites)
.map((k) => {
if (depthOf(styleOverwrites[k]) === 1) {
return { [k]: StyleSheet.flatten(styleOverwrites[k]) };
}
return falseObj;
})
.filter((v) => v !== falseObj)
/**
* Exclude styling around child components. For example, when you provide styles for
* urlPreview as part of styles for StatusUpdateForm. They will be handled separately
* in UrlPreview component.
*/
.filter((k) => !styleKeysExceptStyleName.includes(k))
.map((k) => ({ [k]: StyleSheet.flatten(styleOverwrites[k]) }))
.reduce((accumulated, v) => Object.assign(accumulated, v), {});

// console.log(_.defaultsDeep(topLevelOverwrites, base));
return StyleSheet.create(_.defaultsDeep(topLevelOverwrites, base));
}

0 comments on commit 683aea5

Please sign in to comment.