Skip to content

Commit 633e0da

Browse files
committed
Add automatic flexbox styling to Progress.Bar using width={null}
1 parent 3791527 commit 633e0da

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

Bar.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default class ProgressBar extends Component {
1919
color: PropTypes.string,
2020
height: PropTypes.number,
2121
indeterminate: PropTypes.bool,
22+
onLayout: PropTypes.func,
2223
progress: PropTypes.number,
2324
style: View.propTypes.style,
2425
unfilledColor: PropTypes.string,
@@ -40,6 +41,7 @@ export default class ProgressBar extends Component {
4041
super(props);
4142
const progress = Math.min(Math.max(props.progress, 0), 1);
4243
this.state = {
44+
width: 0,
4345
progress: new Animated.Value(props.indeterminate ? INDETERMINATE_WIDTH_FACTOR : progress),
4446
animationValue: new Animated.Value(BAR_WIDTH_ZERO_POSITION),
4547
};
@@ -95,6 +97,15 @@ export default class ProgressBar extends Component {
9597
});
9698
}
9799

100+
handleLayout = (event) => {
101+
if (!this.props.width) {
102+
this.setState({ width: event.nativeEvent.layout.width });
103+
}
104+
if (this.props.onLayout) {
105+
this.props.onLayout(event);
106+
}
107+
};
108+
98109
render() {
99110
const {
100111
borderColor,
@@ -109,7 +120,7 @@ export default class ProgressBar extends Component {
109120
...restProps
110121
} = this.props;
111122

112-
const innerWidth = width - (borderWidth * 2);
123+
const innerWidth = Math.max(0, width || this.state.width) - (borderWidth * 2);
113124
const containerStyle = {
114125
width,
115126
borderWidth,
@@ -121,7 +132,6 @@ export default class ProgressBar extends Component {
121132
const progressStyle = {
122133
backgroundColor: color,
123134
height,
124-
width: innerWidth,
125135
transform: [{
126136
translateX: this.state.animationValue.interpolate({
127137
inputRange: [0, 1],
@@ -138,7 +148,7 @@ export default class ProgressBar extends Component {
138148
};
139149

140150
return (
141-
<View style={[containerStyle, style]} {...restProps}>
151+
<View style={[containerStyle, style]} onLayout={this.handleLayout} {...restProps}>
142152
<Animated.View style={progressStyle} />
143153
{children}
144154
</View>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ All of the props under *Properties* in addition to the following:
5656

5757
| Prop | Description | Default |
5858
|---|---|---|
59-
|**`width`**|Full width of the progress bar. |`150`|
59+
|**`width`**|Full width of the progress bar, set to `null` to use automatic flexbox sizing. |`150`|
6060
|**`height`**|Height of the progress bar. |`6`|
6161
|**`borderRadius`**|Rounding of corners, set to `0` to disable. |`4`|
6262

0 commit comments

Comments
 (0)