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

upd: Modal #80

Merged
merged 1 commit into from
Jan 21, 2022
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
20 changes: 18 additions & 2 deletions src/components/Modal/Modal.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,35 @@ import { Icon } from '../Icon';
import { iconTypes } from '../Icon/collection';
import colors from '../../styles/colors';
import { Input } from '../Input';
import { useArgs } from '@storybook/addons';

export default {
title: 'Popup/Modal',
component: Modal,
} as ComponentMeta<typeof Modal>;

const Template: ComponentStory<typeof Modal> = (args) => <Modal {...args} />;
const Template: ComponentStory<typeof Modal> = (args) => {
const [{}, updateArgs] = useArgs();

return (
<Modal
{...args}
onCancel={() => {
console.log('canceled');
updateArgs({ isVisible: false });
}}
onOk={() => {
console.log('pressed ok');
updateArgs({ isVisible: false });
}}
/>
);
};

export const Regular = Template.bind({});
Regular.args = {
id: 'regular',
title: 'Confirm',
isVisible: true,
children: [
<div
key={'0'}
Expand Down
15 changes: 13 additions & 2 deletions src/components/Modal/Modal.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { ModalProps } from './types';
import colors from '../../styles/colors';
import fonts from '../../styles/fonts';

const ModalStyled = styled.div<Pick<ModalProps, 'isVisible'>>`
${(p) => (p.isVisible ? 'display: grid;' : 'display: none;')};
const ModalStyled = styled.div`
${fonts.text};
background-color: ${colors.white};
box-shadow: 0 4px 10px rgba(48, 71, 105, 0.1);
Expand Down Expand Up @@ -43,11 +42,23 @@ const ModalFooter = styled.div`
padding: 15px 32px 20px;
`;

const ModalWrapperStyled = styled.div<Pick<ModalProps, 'isVisible'>>`
${(p) => (p.isVisible ? 'display: grid;' : 'display: none;')};
background: rgba(0, 0, 0, 0.3);
bottom: 0;
left: 0;
position: fixed;
right: 0;
top: 0;
z-index: 5;
`;

const ModalStyles = {
ModalContent,
ModalFooter,
ModalHeader,
ModalStyled,
ModalWrapperStyled,
};

export default ModalStyles;
40 changes: 32 additions & 8 deletions src/components/Modal/Modal.test.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
import ReactDOM from 'react-dom';
import React from 'react';
import { composeStories } from '@storybook/testing-react';
import * as stories from './Modal.stories';
const { Regular } = composeStories(stories);
import Modal from './Modal';
import { Icon } from '../Icon';
import { iconTypes } from '../Icon/collection';
import color from '../../styles/colors';

describe('Modal - Regular', () => {
let container: HTMLDivElement;
let modalTestId = 'modal-test-id';
let headerTestId = 'modal-header-test-id';
let contentTestId = 'modal-content-test-id';
let footerTestId = 'modal-footer-test-id';
const modalTestId = 'modal-test-id';
const headerTestId = 'modal-header-test-id';
const contentTestId = 'modal-content-test-id';
const footerTestId = 'modal-footer-test-id';

beforeEach(() => {
const args = {
id: 'regular',
title: 'Confirm',
children: [
<div
key={'0'}
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'column',
}}
>
<Icon
svg={iconTypes.cloud}
size={64}
fill={color.blueDark2}
/>
<p>Proceed uploading?</p>
</div>,
],
isVisible: true,
};
container = document.createElement('div');
document.body.appendChild(container);
ReactDOM.render(<Regular />, container);
ReactDOM.render(<Modal {...args} />, container);
});

afterEach(() => {
Expand Down
105 changes: 60 additions & 45 deletions src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ import Button from '../Button/Button';
import ModalStyles from './Modal.styles';
import { iconTypes } from '../Icon/collection';

const { ModalHeader, ModalStyled, ModalFooter, ModalContent } = ModalStyles;
const {
ModalHeader,
ModalStyled,
ModalFooter,
ModalContent,
ModalWrapperStyled,
} = ModalStyles;

const Modal: React.FC<ModalProps> = ({
id = String(Date.now()),
children,
isOkDisabled,
isCancelDisabled,
isVisible,
isVisible = true,
cancelText = 'Cancel',
okText = 'Ok',
onCancel,
Expand All @@ -25,51 +31,60 @@ const Modal: React.FC<ModalProps> = ({
}, [isVisible]);

return (
<ModalStyled id={id} isVisible={visible} data-testid="modal-test-id">
<ModalHeader data-testid={'modal-header-test-id'}>
<h3>{title}</h3>
<Button
data-testid={'modal-close-test-id'}
icon={iconTypes.x}
iconLayout={'icon-only'}
onClick={() => setVisibility(false)}
theme={'outline'}
/>
</ModalHeader>
<ModalWrapperStyled
id={id}
isVisible={visible}
data-testid="modal-test-id"
>
<ModalStyled>
<ModalHeader data-testid={'modal-header-test-id'}>
<h3>{title}</h3>
<Button
data-testid={'modal-close-test-id'}
icon={iconTypes.x}
iconLayout={'icon-only'}
onClick={() => setVisibility(false)}
theme={'outline'}
/>
</ModalHeader>

<ModalContent id={'content'} data-testid={'modal-content-test-id'}>
{children}
</ModalContent>
<ModalContent
id={'content'}
data-testid={'modal-content-test-id'}
>
{children}
</ModalContent>

<ModalFooter data-testid={'modal-footer-test-id'}>
<Button
data-testid={'modal-cancel-button-test-id'}
disabled={isCancelDisabled}
text={cancelText}
onClick={
onCancel
? onCancel
: () => {
setVisibility(false);
}
}
theme={'outline'}
/>
<Button
data-testid={'modal-ok-button-test-id'}
onClick={
onOk
? onOk
: () => {
console.log('ok triggered');
}
}
disabled={isOkDisabled}
text={okText}
theme={'primary'}
/>
</ModalFooter>
</ModalStyled>
<ModalFooter data-testid={'modal-footer-test-id'}>
<Button
data-testid={'modal-cancel-button-test-id'}
disabled={isCancelDisabled}
text={cancelText}
onClick={
onCancel
? onCancel
: () => {
setVisibility(false);
}
}
theme={'outline'}
/>
<Button
data-testid={'modal-ok-button-test-id'}
onClick={
onOk
? onOk
: () => {
console.log('ok triggered');
}
}
disabled={isOkDisabled}
text={okText}
theme={'primary'}
/>
</ModalFooter>
</ModalStyled>
</ModalWrapperStyled>
);
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/Modal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface ModalProps {
/**
* set if modal is visible
*/
isVisible: boolean;
isVisible?: boolean;

/**
* Run function on 'Cancel'
Expand Down