-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from GetLuko/sami/feat/buttonsBar
Feat : Add ButtonBar component
- Loading branch information
Showing
9 changed files
with
165 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Box, ButtonBar, ButtonProps } from '@getluko/streamline'; | ||
import React from 'react'; | ||
|
||
const buttonsArray: ButtonProps[] = [ | ||
{ | ||
text: 'One', | ||
onPress: () => console.log('onPress One'), | ||
iconName: 'Area', | ||
size: 'mini', | ||
}, | ||
{ | ||
text: 'Two', | ||
onPress: () => console.log('onPress Two'), | ||
iconName: 'Area', | ||
size: 'mini', | ||
}, | ||
{ | ||
text: 'Three', | ||
onPress: () => console.log('onPress Three'), | ||
iconName: 'Area', | ||
size: 'mini', | ||
}, | ||
{ | ||
text: 'Four', | ||
onPress: () => console.log('onPress Four'), | ||
iconName: 'Area', | ||
size: 'mini', | ||
}, | ||
{ | ||
text: 'Five', | ||
onPress: () => console.log('onPress Five'), | ||
iconName: 'Area', | ||
size: 'mini', | ||
}, | ||
]; | ||
|
||
export const ButtonBarSandbox = () => { | ||
return ( | ||
<Box> | ||
<ButtonBar buttons={buttonsArray.slice(0, 1)} /> | ||
<ButtonBar buttons={buttonsArray.slice(0, 2)} /> | ||
<ButtonBar buttons={buttonsArray.slice(0, 3)} /> | ||
<ButtonBar buttons={buttonsArray.slice(0, 4)} /> | ||
<ButtonBar buttons={buttonsArray.slice(0, 5)} /> | ||
<ButtonBar buttons={buttonsArray.slice(0, 5)} isSkeleton /> | ||
<ButtonBar buttons={[]} /> | ||
</Box> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/streamline/src/components/button-bar/button-bar.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { fireEvent } from '@testing-library/react-native'; | ||
import React from 'react'; | ||
|
||
import { renderWithProvider } from '../../testing/render-with-provider'; | ||
import { ButtonProps } from '../../types'; | ||
import { Button } from '../buttons/button/button'; | ||
import { ButtonBar } from './button-bar'; | ||
|
||
describe('ButtonBar', () => { | ||
const buttons: ButtonProps[] = [ | ||
{ text: 'Button 1', iconName: 'Area', size: 'mini', onPress: jest.fn() }, | ||
{ text: 'Button 2', iconName: 'Area', size: 'mini', onPress: jest.fn() }, | ||
]; | ||
|
||
it('renders correctly with buttons', () => { | ||
const { getByText } = renderWithProvider(<ButtonBar buttons={buttons} />); | ||
|
||
buttons.forEach((button) => { | ||
expect(getByText(button.text)).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
it('fires the onPress function when a button is clicked', () => { | ||
const { getByText } = renderWithProvider(<ButtonBar buttons={buttons} />); | ||
const buttonToClick = getByText('Button 1'); | ||
fireEvent.press(buttonToClick); | ||
|
||
expect(buttons[0].onPress).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should render skeleton items when isSkeleton prop is true', () => { | ||
// It is safe when it is used for props test | ||
// https://github.com/callstack/react-native-testing-library/issues/427#issuecomment-654405308 | ||
const { UNSAFE_getAllByType } = renderWithProvider( | ||
<ButtonBar buttons={[]} isSkeleton={true} testID="ButtonBar" /> | ||
); | ||
|
||
const buttons = UNSAFE_getAllByType(Button); | ||
|
||
expect(buttons.length).toBe(5); | ||
|
||
buttons.forEach((button) => { | ||
expect(button.props).toHaveProperty('isSkeleton', true); | ||
}); | ||
}); | ||
|
||
it('does not scroll when isSkeleton is true', () => { | ||
const { getByTestId } = renderWithProvider( | ||
<ButtonBar buttons={[]} isSkeleton testID="ButtonBar" /> | ||
); | ||
const scrollView = getByTestId('ButtonBar_container'); | ||
|
||
expect(scrollView.props).toHaveProperty('scrollEnabled', false); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
packages/streamline/src/components/button-bar/button-bar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react'; | ||
import { ScrollView } from 'react-native'; | ||
|
||
import { Box } from '../../primitives/box/box'; | ||
import { useStreamlineTheme } from '../../theme'; | ||
import { Button } from '../buttons/button/button'; | ||
import { ButtonProps } from '../buttons/button/button.types'; | ||
|
||
type Props = { | ||
buttons: ButtonProps[]; | ||
isSkeleton?: boolean; | ||
testID?: string; | ||
}; | ||
|
||
export const ButtonBar = ({ buttons, isSkeleton = false, testID }: Props) => { | ||
const { spacing } = useStreamlineTheme(); | ||
|
||
const skeletonItems: ButtonProps[] = Array(5) | ||
.fill(0) | ||
.map((_, index) => ({ | ||
iconName: 'Area', | ||
text: `Skeleton${index}`, | ||
size: 'mini', | ||
})); | ||
|
||
const buttonMenu = | ||
buttons.length === 0 && isSkeleton ? skeletonItems : buttons; | ||
|
||
return ( | ||
<ScrollView | ||
testID={testID ? `${testID}_container` : undefined} | ||
scrollEnabled={!isSkeleton} | ||
horizontal={true} | ||
showsHorizontalScrollIndicator={false} | ||
contentContainerStyle={{ padding: spacing.md, flexDirection: 'row' }} | ||
> | ||
{buttonMenu.map((button, index) => ( | ||
<Box key={button.text} marginRight="md"> | ||
<Button | ||
testID={testID ? `${testID}_button_${index}` : undefined} | ||
appearance="secondary" | ||
{...button} | ||
isSkeleton={isSkeleton} | ||
/> | ||
</Box> | ||
))} | ||
</ScrollView> | ||
); | ||
}; | ||
|
||
export default ButtonBar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters