Skip to content

Commit ae43d8d

Browse files
Merge pull request #1222 from buildo/giogonzo-patch-1
Improve Meter.tsx styleability (closes #1223)
2 parents b729ba9 + 73b08c5 commit ae43d8d

File tree

5 files changed

+58
-21
lines changed

5 files changed

+58
-21
lines changed

src/Meter/Examples.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const ranges = [
1111

1212
const meterProps = {
1313
ranges,
14+
baseBackgroundColor: '#f8f8f8',
1415
style: { marginBottom: 20 }
1516
};
1617

@@ -40,7 +41,7 @@ const ranges = [
4041
const meterProps = {
4142
ranges,
4243
baseFillingColor: '#CCCCCC',
43-
baseBackgroundColor: '#FAFAFA',
44+
baseBackgroundColor: '#F8F8F8',
4445
style: { marginBottom: 20 }
4546
};
4647

@@ -86,6 +87,7 @@ const meterProps = {
8687
min: 400,
8788
max: 700,
8889
ranges,
90+
baseBackgroundColor: '#f8f8f8',
8991
style: { marginBottom: 20 }
9092
};
9193

src/Meter/Meter.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,13 @@ export class Meter extends React.PureComponent<Meter.Props> {
158158
return {
159159
basisSize: `${computePercentage(value, min, max)}%`,
160160
fillingStyle: {
161-
backgroundColor: range ? range.fillingColor : baseFillingColor
161+
background: range && range.fillingColor || baseFillingColor
162162
},
163163
labelStyle: {
164-
color: range ? range.labelColor : baseLabelColor
164+
color: range && range.labelColor || baseLabelColor
165165
},
166166
barStyle: {
167-
backgroundColor: range ? range.backgroundColor : baseBackgroundColor
167+
background: range && range.backgroundColor || baseBackgroundColor
168168
}
169169
};
170170
}
@@ -185,7 +185,7 @@ export class Meter extends React.PureComponent<Meter.Props> {
185185
const formattedLabel = labelFormatter(value, min, max);
186186

187187
return (
188-
<FlexView {...{ id, className, style }} grow>
188+
<FlexView {...{ id, className, style }} grow vAlignContent='center'>
189189
<FlexView
190190
className='bar'
191191
grow

src/Meter/meter.scss

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
@import '../utils/theme/variables.scss';
22

3+
$height: 18px !default;
4+
$font-size: 14px !default;
5+
$font-weight: $brc-regular !default;
6+
$border-radius: 10px !default;
7+
$label-margin: 5px !default;
8+
39
.meter {
410
.bar {
511
overflow: hidden;
6-
border-radius: 10px;
7-
height: 18px;
8-
background-color: $brc-paleGrey;
12+
border-radius: $border-radius;
13+
height: $height;
14+
15+
.filling {
16+
border-bottom-left-radius: $border-radius;
17+
border-top-left-radius: $border-radius;
18+
}
919
}
1020

11-
.filling {
12-
border-bottom-left-radius: 10px;
13-
border-top-left-radius: 10px;
21+
.label {
22+
font-size: $font-size;
23+
font-weight: $font-weight;
24+
padding-left: $label-margin;
1425
}
1526
}

test/components/Meter.test.tsx

+31-7
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ describe('Meter', () => {
5858
});
5959

6060
it('computes barStyle', () => {
61-
expect(meter.find('.bar').prop('style').backgroundColor).toBe(ranges[0].backgroundColor);
61+
expect(meter.find('.bar').prop('style').background).toBe(ranges[0].backgroundColor);
6262
});
6363

6464
it('computes fillingStyle', () => {
65-
expect(meter.find('.filling').prop('style').backgroundColor).toBe(ranges[0].fillingColor);
65+
expect(meter.find('.filling').prop('style').background).toBe(ranges[0].fillingColor);
6666
});
6767

6868
it('computes labelStyle', () => {
@@ -94,7 +94,7 @@ describe('Meter', () => {
9494
const meter = shallow(
9595
<Meter value={60} ranges={ranges} />
9696
);
97-
expect(meter.find('.filling').prop('style').backgroundColor).toBe(ranges[0].fillingColor);
97+
expect(meter.find('.filling').prop('style').background).toBe(ranges[0].fillingColor);
9898
});
9999

100100
it('defaults to the base color as background color if there\'s no matching range', () => {
@@ -104,7 +104,7 @@ describe('Meter', () => {
104104
const meter = shallow(
105105
<Meter value={20} ranges={ranges} baseFillingColor='#ccc' />
106106
);
107-
expect(meter.find('.filling').prop('style').backgroundColor).toBe('#ccc');
107+
expect(meter.find('.filling').prop('style').background).toBe('#ccc');
108108
});
109109

110110
it('doesn\'t define a background color if there\'s no matching range and no default is given', () => {
@@ -114,7 +114,7 @@ describe('Meter', () => {
114114
const meter = shallow(
115115
<Meter value={20} ranges={ranges} />
116116
);
117-
expect(meter.find('.filling').prop('style').backgroundColor).toBeUndefined();
117+
expect(meter.find('.filling').prop('style').background).toBeUndefined();
118118
});
119119

120120
it('defaults to base color as bar background color should if there\'s no matching range', () => {
@@ -124,7 +124,7 @@ describe('Meter', () => {
124124
const meter = shallow(
125125
<Meter value={20} ranges={ranges} baseBackgroundColor='#ccc' />
126126
);
127-
expect(meter.find('.bar').prop('style').backgroundColor).toBe('#ccc');
127+
expect(meter.find('.bar').prop('style').background).toBe('#ccc');
128128
});
129129

130130
it('doesn\'t define a bar background color if there\'s no matching range and no default is given', () => {
@@ -134,7 +134,31 @@ describe('Meter', () => {
134134
const meter = shallow(
135135
<Meter value={20} ranges={ranges} />
136136
);
137-
expect(meter.find('.bar').prop('style').backgroundColor).toBeUndefined();
137+
expect(meter.find('.bar').prop('style').background).toBeUndefined();
138+
});
139+
140+
it('computes colors respecting base colors', () => {
141+
const ranges = [
142+
{ startValue: 0, endValue: 50, fillingColor: 'yellow' },
143+
{ startValue: 50, endValue: 80 }
144+
];
145+
const baseColors = {
146+
baseBackgroundColor: 'red',
147+
baseFillingColor: 'green',
148+
baseLabelColor: 'blue',
149+
};
150+
const meter1 = shallow(
151+
<Meter value={30} ranges={ranges} {...baseColors} />
152+
);
153+
expect(meter1.find('.bar').prop('style').background).toBe('red');
154+
expect(meter1.find('.filling').prop('style').background).toBe('yellow');
155+
expect(meter1.find('.label').prop('style').color).toBe('blue');
156+
const meter2 = shallow(
157+
<Meter value={60} ranges={ranges} {...baseColors} />
158+
);
159+
expect(meter2.find('.bar').prop('style').background).toBe('red');
160+
expect(meter2.find('.filling').prop('style').background).toBe('green');
161+
expect(meter2.find('.label').prop('style').color).toBe('blue');
138162
});
139163

140164
});

test/components/__snapshots__/Meter.test.tsx.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`Meter renders correctly 1`] = `
44
<div
5-
className="react-flex-view meter fancy-class-name"
5+
className="react-flex-view align-content-center meter fancy-class-name"
66
id="12345"
77
style={
88
Object {
@@ -23,7 +23,7 @@ exports[`Meter renders correctly 1`] = `
2323
"MozBoxFlex": "1 1 auto",
2424
"WebkitBoxFlex": "1 1 auto",
2525
"WebkitFlex": "1 1 auto",
26-
"backgroundColor": "grey",
26+
"background": "grey",
2727
"flex": "1 1 auto",
2828
"msFlex": "1 1 auto",
2929
}
@@ -36,7 +36,7 @@ exports[`Meter renders correctly 1`] = `
3636
"MozBoxFlex": "0 0 25%",
3737
"WebkitBoxFlex": "0 0 25%",
3838
"WebkitFlex": "0 0 25%",
39-
"backgroundColor": "green",
39+
"background": "green",
4040
"flex": "0 0 25%",
4141
"msFlex": "0 0 25%",
4242
}

0 commit comments

Comments
 (0)