Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/modal sheet #338

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions __mocks__/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { RenderHookWrapper, testQuery } from './apollo';
export * from './utils';
49 changes: 49 additions & 0 deletions __mocks__/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
export const flatListScrollEventData = {
nativeEvent: {
contentOffset: {
x: 0,
y: 425,
},
contentSize: {
// Dimensions of the scrollable content
height: 885,
width: 328,
},
layoutMeasurement: {
// Dimensions of the device
height: 469,
width: 328,
},
},
};

const getRandomZeroBasedIndex = <T>(array: T[]) =>
(Math.random() * (array.length - 1 - 0 + 1)) << 0;

Check warning on line 21 in __mocks__/utils.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected use of '<<'

export const randomPositiveNumber = (max: number, min: number = 0) =>
Math.floor(Math.random() * (max - min + 1)) + min;

export const randomArrayElement = <T>(array: T[], avoid?: number[]) => {
if (!avoid || !avoid.length) {
const randomZeroBasedIndex = getRandomZeroBasedIndex(array);
return array[randomZeroBasedIndex];
}
while (true) {
const randomZeroBasedIndex = getRandomZeroBasedIndex(array);
if (!avoid.includes(randomZeroBasedIndex)) {
return array[randomZeroBasedIndex];
}
}
};

export const randomArrayIndex = <T>(array: T[], avoid?: number[]) => {
if (!avoid || !avoid.length) {
return getRandomZeroBasedIndex(array);
}
while (true) {
const randomZeroBasedIndex = getRandomZeroBasedIndex(array);
if (!avoid.includes(randomZeroBasedIndex)) {
return randomZeroBasedIndex;
}
}
};
5 changes: 4 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ module.exports = {
},
transformIgnorePatterns: [],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
setupFiles: ['./jest.setup.js'],
setupFiles: [
'./jest.setup.js',
'./node_modules/react-native-gesture-handler/jestSetup.js',
],
};
353 changes: 178 additions & 175 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 5 additions & 15 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import React from 'react';
import { StyleSheet } from 'react-native';
import { GestureHandlerRootView } from 'react-native-gesture-handler';

import {
ThemeContextProvider,
Expand All @@ -11,18 +9,10 @@ import { Navigation } from '@navigation';

export const App = () => (
<ThemeContextProvider>
<GestureHandlerRootView style={sheet.gestureHandler}>
<GraphQLClient>
<AlertMessageProvider>
<Navigation />
</AlertMessageProvider>
</GraphQLClient>
</GestureHandlerRootView>
<GraphQLClient>
<AlertMessageProvider>
<Navigation />
</AlertMessageProvider>
</GraphQLClient>
</ThemeContextProvider>
);

const sheet = StyleSheet.create({
gestureHandler: {
flex: 1,
},
});
2 changes: 2 additions & 0 deletions src/components/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export {
type SVGIconProps,
} from './svg-icon';
export { Typography };
export { ModalSelectButton } from './modal-select-button/ModalSelectButton';
export { ModalSheet } from './modal-sheet/ModalSheet';
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { TouchableOpacityProps } from 'react-native';
import styled, { IStyledComponent } from 'styled-components/native';
import { Substitute } from 'styled-components/native/dist/types';

import { isEqualsOrLargerThanIphoneX } from '@utils';
import { Typography } from '@common-components';

interface SelectButtonStyleProps {
borderBottomRightRadius?: number;
borderBottomLeftRadius?: number;
}

export const SelectButton: IStyledComponent<
'native',
Substitute<TouchableOpacityProps, SelectButtonStyleProps>
> = styled.TouchableOpacity<SelectButtonStyleProps>`
width: 100%;
height: ${({ theme }) =>
isEqualsOrLargerThanIphoneX()
? theme.metrics.getWidthFromDP('20')
: theme.metrics.getWidthFromDP('16')}px;
justify-content: center;
align-items: center;
background-color: ${({ theme }) => theme.colors.primary};
border-bottom-right-radius: ${({ borderBottomRightRadius }) =>
borderBottomRightRadius ?? 0}px;
border-bottom-left-radius: ${({ borderBottomLeftRadius }) =>
borderBottomLeftRadius ?? 0}px;
opacity: ${({ disabled }) => (disabled ? 0.5 : 1)};
`;

export const SelectButtonText = styled(Typography.MediumText).attrs(
({ theme }) => ({
color: theme.colors.buttonText,
bold: true,
}),
)`
font-size: ${({ theme }) => theme.metrics.xl}px;
color: ${({ theme }) => theme.colors.buttonText};
text-transform: uppercase;
`;
110 changes: 110 additions & 0 deletions src/components/common/modal-select-button/ModalSelectButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import React from 'react';
import { ThemeProvider } from 'styled-components/native';
import { fireEvent, render, RenderAPI } from '@testing-library/react-native';

import { dark as theme } from '@styles/themes/dark';

import { ModalSelectButtonProps, ModalSelectButton } from './ModalSelectButton';
import { randomPositiveNumber } from '../../../../__mocks__';

jest.mock('@utils', () => ({
isEqualsOrLargerThanIphoneX: jest.fn(),
}));

const TITLE = 'TITLE';

const renderModalSelectButton = (props: ModalSelectButtonProps) => (
<ThemeProvider theme={theme}>
<ModalSelectButton {...props} />
</ThemeProvider>
);

describe('Components/Common/ModalSelectButton', () => {
const elements = {
selectButton: (api: RenderAPI) => api.getByTestId('select-button'),
selectButtonText: (api: RenderAPI) => api.getByTestId('select-button-text'),
};

describe('Render correctly', () => {
it('should render correctly when "borderBottomRightRadius" is set', () => {
const borderBottomRightRadius = randomPositiveNumber(10, 1);
const component = render(
renderModalSelectButton({
borderBottomRightRadius,
onPress: jest.fn(),
isDisabled: false,
title: TITLE,
}),
);
expect(
elements.selectButton(component).props.style.borderBottomRightRadius,
).toEqual(borderBottomRightRadius);
expect(
elements.selectButton(component).props.style.borderBottomLeftRadius,
).toEqual(0);
expect(elements.selectButtonText(component).children[0]).toEqual(TITLE);
});

it('should render correctly when "borderBottomLeftRadius" is set', () => {
const borderBottomLeftRadius = randomPositiveNumber(10, 1);
const component = render(
renderModalSelectButton({
borderBottomLeftRadius,
onPress: jest.fn(),
isDisabled: false,
title: TITLE,
}),
);
expect(
elements.selectButton(component).props.style.borderBottomLeftRadius,
).toEqual(borderBottomLeftRadius);
expect(
elements.selectButton(component).props.style.borderBottomRightRadius,
).toEqual(0);
expect(elements.selectButtonText(component).children[0]).toEqual(TITLE);
});

it('should render correctly when "borderBottomLeftRadius" and "borderBottomLeftRadius" are set', () => {
const borderBottomLeftRadius = randomPositiveNumber(10, 1);
const borderBottomRightRadius = randomPositiveNumber(10, 1);
const component = render(
renderModalSelectButton({
borderBottomLeftRadius,
borderBottomRightRadius,
onPress: jest.fn(),
isDisabled: false,
title: TITLE,
}),
);
expect(
elements.selectButton(component).props.style.borderBottomLeftRadius,
).toEqual(borderBottomLeftRadius);
expect(
elements.selectButton(component).props.style.borderBottomRightRadius,
).toEqual(borderBottomRightRadius);
expect(elements.selectButtonText(component).children[0]).toEqual(TITLE);
});
});

describe('Press button', () => {
it('should not call the "onPress" when user press and "isDisabled" is "false"', () => {
const onPress = jest.fn();
const component = render(
renderModalSelectButton({ onPress, isDisabled: false, title: TITLE }),
);
expect(onPress).toHaveBeenCalledTimes(0);
fireEvent.press(elements.selectButton(component));
expect(onPress).toHaveBeenCalledTimes(1);
});

it('should call the "onPress" when user press and "isDisabled" is "true"', () => {
const onPress = jest.fn();
const component = render(
renderModalSelectButton({ onPress, isDisabled: true, title: 'TITLE' }),
);
expect(onPress).toHaveBeenCalledTimes(0);
fireEvent.press(elements.selectButton(component));
expect(onPress).toHaveBeenCalledTimes(0);
});
});
});
24 changes: 24 additions & 0 deletions src/components/common/modal-select-button/ModalSelectButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';

import * as Styles from './ModalSelectButton.styles';

export type ModalSelectButtonProps = {
borderBottomRightRadius?: number;
borderBottomLeftRadius?: number;
isDisabled?: boolean;
onPress: () => void;
title: string;
};

export const ModalSelectButton = (props: ModalSelectButtonProps) => (
<Styles.SelectButton
borderBottomRightRadius={props.borderBottomRightRadius}
borderBottomLeftRadius={props.borderBottomLeftRadius}
disabled={props.isDisabled}
onPress={props.onPress}
testID="select-button">
<Styles.SelectButtonText testID="select-button-text">
{props.title}
</Styles.SelectButtonText>
</Styles.SelectButton>
);
67 changes: 67 additions & 0 deletions src/components/common/modal-sheet/ModalSheet.styles.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { StyleSheet } from 'react-native';
import styled from 'styled-components/native';

import { Typography } from '@common-components';
import { dark, light } from '@styles/themes';
import { borderRadius } from '@styles/border-radius';
import metrics from '@styles/metrics';

export const DEFAULT_MODAL_SHEET_HEIGHT = metrics.getHeightFromDP('60%');

export const GripWrapper = styled.View`
width: 100%;
align-items: center;
padding-vertical: ${({ theme }) => theme.metrics.md}px;
`;

export const Grip = styled.View`
width: ${({ theme }) => theme.metrics.getWidthFromDP('16%')}px;
height: ${({ theme }) => theme.metrics.getWidthFromDP('1.5%')}px;
border-radius: ${({ theme }) => theme.metrics.xl}px;
background-color: ${({ theme }) => theme.colors.inactiveWhite};
`;

export const Title = styled(Typography.SmallText).attrs(({ theme }) => ({
color: theme.colors.buttonText,
alignment: 'center',
bold: true,
}))`
margin-horizontal: ${({ theme }) => theme.metrics.lg}px;
margin-bottom: ${({ theme }) => theme.metrics.lg}px;
`;

export const LineDivider = styled.View`
width: 100%;
height: ${({ theme }) => theme.metrics.getWidthFromDP('0.5%')}px;
background-color: ${light.colors.androidToolbar};
`;

export const ListHeaderWrapper = styled.View`
margin-top: ${({ theme }) => theme.metrics.sm}px;
`;

export const sheet = StyleSheet.create({
backgroundDarkLayer: {
...StyleSheet.absoluteFillObject,
backgroundColor: dark.colors.darkLayer,
},
bottomGap: {
width: '100%',
position: 'absolute',
bottom: 0,
left: 0,
right: 0,
},
card: {
borderTopLeftRadius: borderRadius.md,
borderTopRightRadius: borderRadius.md,
backgroundColor: dark.colors.white,
bottom: 0,
left: 0,
right: 0,
},
gestureHandlerRootView: {
width: '100%',
height: '100%',
},
});
Loading
Loading