Skip to content
This repository has been archived by the owner on Oct 17, 2020. It is now read-only.

Add drawer component #727

Merged
merged 2 commits into from
May 1, 2020
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
14 changes: 14 additions & 0 deletions frontend/src/component/ui/Drawer.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@import '../styles/color';
@import '../styles/easing';

$drawer-width: 240px;

.drawer {
width: $drawer-width;
margin-left: -$drawer-width;
transition: margin-left 300ms $ease-in-out-quart;

&.open {
margin-left: 0;
}
}
49 changes: 49 additions & 0 deletions frontend/src/component/ui/Drawer.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from 'react';
import { render } from '@testing-library/react';
import { Drawer } from './Drawer';

describe('Drawer component', () => {
test('should render without crash', () => {
render(<Drawer />);
});

test('should render in open state by default', () => {
const { container } = render(<Drawer />);

const drawer = container.querySelector('.drawer');
expect(drawer).toBeTruthy();
expect(drawer!.className).toContain('open');
});

test('should close the drawer when triggered to close', () => {
const drawerRef = React.createRef<Drawer>();
const { container } = render(<Drawer ref={drawerRef} />);

const drawer = container.querySelector('.drawer');
expect(drawer).toBeTruthy();
expect(drawer!.className).toContain('open');

drawerRef.current!.close();
expect(drawer!.className).not.toContain('open');
});

test('should open the drawer when triggered to open', () => {
const drawerRef = React.createRef<Drawer>();
const { container } = render(<Drawer ref={drawerRef} />);

const drawer = container.querySelector('.drawer');
expect(drawer).toBeTruthy();
drawerRef.current!.close();
expect(drawer!.className).not.toContain('open');

drawerRef.current!.open();
expect(drawer!.className).toContain('open');
});

test('should render component children correctly', () => {
const childContent = 'children of drawer component';
const { container } = render(<Drawer> {childContent} </Drawer>);

expect(container.textContent).toContain(childContent);
});
});
41 changes: 41 additions & 0 deletions frontend/src/component/ui/Drawer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, { Component } from 'react';

import './Drawer.scss';
import classNames from 'classnames';

interface IProps {}

interface IState {
isOpen: boolean;
}

export class Drawer extends Component<IProps, IState> {
constructor(props: IProps) {
super(props);
this.state = {
isOpen: true
};
}

open() {
this.setState({ isOpen: true });
}

close() {
this.setState({ isOpen: false });
}

render() {
const isOpen = this.state.isOpen;
return (
<div
className={classNames({
drawer: true,
open: isOpen
})}
>
{this.props.children}
</div>
);
}
}