forked from input-output-hk/react-polymorph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeader.js
More file actions
142 lines (117 loc) · 3.73 KB
/
Header.js
File metadata and controls
142 lines (117 loc) · 3.73 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// @flow
import React, { Component } from 'react';
import type { ComponentType, Element } from 'react';
import { pickBy, isEmpty } from 'lodash';
// components
import { withTheme } from './HOC/withTheme';
// utility functions
import { composeTheme, addThemeId, didThemePropsChange } from '../utils/themes';
// constants
import { IDENTIFIERS } from '../themes/API';
type Props = {
bold: boolean,
center: boolean,
children: Element<*>,
className: string,
context: {
theme: Object,
ROOT_THEME_API: Object
},
h1: boolean,
h2: boolean,
h3: boolean,
h4: boolean,
light: boolean,
lowerCase: boolean,
medium: boolean,
regular: boolean,
right: boolean,
left: boolean,
skin: ComponentType<any>,
theme: Object,
themeId: string,
themeOverrides: Object,
thin: boolean,
upperCase: boolean
};
type State = { composedTheme: Object };
class HeaderBase extends Component <Props, State> {
// define static properties
static displayName = 'Header';
static defaultProps = {
theme: null,
themeId: IDENTIFIERS.HEADER,
themeOverrides: {}
};
constructor(props: Props) {
super(props);
const { context, themeId, theme, themeOverrides } = props;
this.state = {
composedTheme: composeTheme(
addThemeId(theme || context.theme, themeId),
addThemeId(themeOverrides, themeId),
context.ROOT_THEME_API
)
};
}
componentWillReceiveProps(nextProps: Props) {
didThemePropsChange(this.props, nextProps, this.setState.bind(this));
}
_assembleInlineStyles = ({ center, lowerCase, left, right, upperCase }) => {
const inlineStyles = {};
const textAlign = pickBy({ center, left, right });
const textTransform = pickBy({ lowerCase, upperCase });
if (!isEmpty(textAlign)) {
inlineStyles.textAlign = Object.keys(textAlign)[0];
}
if (!isEmpty(textTransform)) {
inlineStyles.textTransform = Object.keys(textTransform)[0];
}
return inlineStyles;
};
_assembleHeaderTheme = (styleProps: Object) => {
const activeClasses = this._getActiveClasses(styleProps);
const theme = this.state.composedTheme[this.props.themeId];
return activeClasses.reduce((reducedTheme, activeClass) => {
if (activeClass && Object.hasOwnProperty.call(theme, activeClass)) {
reducedTheme[activeClass] = theme[activeClass];
}
return reducedTheme;
}, {});
};
_getActiveFont = ({ light, medium, regular, thin, bold }) => {
const fontProps = pickBy({ light, medium, regular, thin, bold });
if (isEmpty(fontProps)) { return; }
// returns the first active font if more than 1 is passed
return Object.keys(fontProps)[0];
};
_getActiveTheme = ({ h1, h2, h3, h4 }) => {
const themeProps = pickBy({ h1, h2, h3, h4 });
if (isEmpty(themeProps)) { return; }
// returns the first active theme if more than 1 is passed
return Object.keys(themeProps)[0];
};
_getActiveClasses = (styleProps: Object) => {
const activeClasses = ['header'];
const activeTheme = this._getActiveTheme(styleProps);
const activeFont = this._getActiveFont(styleProps);
if (activeTheme) { return [...activeClasses, activeTheme]; }
if (activeFont) { return [...activeClasses, activeFont]; }
return [...activeClasses, activeTheme, activeFont].filter(val => val);
};
render() {
const { children, className, skin: HeaderSkin, ...styleProps } = this.props;
const reducedTheme = this._assembleHeaderTheme(styleProps);
const inlineStyles = this._assembleInlineStyles(styleProps);
return (
<HeaderSkin
className={className}
inlineStyles={inlineStyles}
theme={reducedTheme}
>
{children}
</HeaderSkin>
);
}
}
export const Header = withTheme(HeaderBase);