Skip to content

Commit

Permalink
feat: progress bar fill style (#4222)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewalczak authored Jan 8, 2024
1 parent 74b011d commit 3475624
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/components/ProgressBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,19 @@ export type Props = React.ComponentPropsWithRef<typeof View> & {
* Whether to show the ProgressBar (true, the default) or hide it (false).
*/
visible?: boolean;
/**
* Style of filled part of the ProgresBar.
*/
fillStyle?: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
style?: StyleProp<ViewStyle>;
/**
* @optional
*/
theme?: ThemeProp;
/**
* testID to be used on tests.
*/
testID?: string;
};

const INDETERMINATE_DURATION = 2000;
Expand All @@ -67,11 +75,13 @@ const { isRTL } = I18nManager;
const ProgressBar = ({
color,
indeterminate,
style,
progress = 0,
visible = true,
theme: themeOverrides,
animatedValue,
style,
fillStyle,
testID = 'progress-bar',
...rest
}: Props) => {
const isWeb = Platform.OS === 'web';
Expand Down Expand Up @@ -196,6 +206,7 @@ const ProgressBar = ({
indeterminate ? {} : { min: 0, max: 100, now: progress * 100 }
}
style={isWeb && styles.webContainer}
testID={testID}
>
<Animated.View
style={[
Expand All @@ -206,6 +217,7 @@ const ProgressBar = ({
>
{width ? (
<Animated.View
testID={`${testID}-fill`}
style={[
styles.progressBar,
{
Expand Down Expand Up @@ -252,6 +264,7 @@ const ProgressBar = ({
},
],
},
fillStyle,
]}
/>
) : null}
Expand Down
19 changes: 18 additions & 1 deletion src/components/__tests__/ProgressBar.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { Animated, Platform } from 'react-native';
import { Animated, Platform, StyleSheet } from 'react-native';

import { render, waitFor } from '@testing-library/react-native';

Expand All @@ -13,6 +13,12 @@ const layoutEvent = {
},
};

const styles = StyleSheet.create({
fill: {
borderRadius: 4,
},
});

const a11yRole = 'progressbar';

class ClassProgressBar extends React.Component<Props> {
Expand Down Expand Up @@ -73,3 +79,14 @@ it('renders progress bar with full height on web', () => {
height: '100%',
});
});

it('renders progress bar with custom style of filled part', async () => {
const tree = render(
<ProgressBar progress={0.2} fillStyle={styles.fill} testID="progress-bar" />
);
await waitFor(() => tree.getByRole(a11yRole).props.onLayout(layoutEvent));

expect(tree.getByTestId('progress-bar-fill')).toHaveStyle({
borderRadius: 4,
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exports[`renders colored progress bar 1`] = `
accessible={true}
onLayout={[Function]}
style={false}
testID="progress-bar"
>
<View
collapsable={false}
Expand Down Expand Up @@ -47,6 +48,7 @@ exports[`renders colored progress bar 1`] = `
"width": 100,
}
}
testID="progress-bar-fill"
/>
</View>
</View>
Expand All @@ -70,6 +72,7 @@ exports[`renders hidden progress bar 1`] = `
accessible={true}
onLayout={[Function]}
style={false}
testID="progress-bar"
>
<View
collapsable={false}
Expand Down Expand Up @@ -99,6 +102,7 @@ exports[`renders hidden progress bar 1`] = `
"width": 100,
}
}
testID="progress-bar-fill"
/>
</View>
</View>
Expand All @@ -116,6 +120,7 @@ exports[`renders indeterminate progress bar 1`] = `
accessible={true}
onLayout={[Function]}
style={false}
testID="progress-bar"
>
<View
collapsable={false}
Expand Down Expand Up @@ -145,6 +150,7 @@ exports[`renders indeterminate progress bar 1`] = `
"width": 100,
}
}
testID="progress-bar-fill"
/>
</View>
</View>
Expand All @@ -168,6 +174,7 @@ exports[`renders progress bar with specific progress 1`] = `
accessible={true}
onLayout={[Function]}
style={false}
testID="progress-bar"
>
<View
collapsable={false}
Expand Down Expand Up @@ -197,6 +204,7 @@ exports[`renders progress bar with specific progress 1`] = `
"width": 100,
}
}
testID="progress-bar-fill"
/>
</View>
</View>
Expand Down

0 comments on commit 3475624

Please sign in to comment.