@@ -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 >
0 commit comments