Merge pull request #5 from andrerfneves/feature/styled-components-base

Feature/styled components base
This commit is contained in:
George Lima 2018-11-28 23:50:19 -03:00 committed by GitHub
commit cf1e5af600
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 190 additions and 199 deletions

View File

@ -1,4 +1,5 @@
[ignore]
.*/node_modules/polished/.*
[include]

View File

@ -1,8 +1,6 @@
import configureStore from 'redux-mock-store';
import { ADD_TODO } from '../../app/constants/actions';
import { addTodo } from '../../app/actions/add-todo';
import { ADD_TODO, addTodo } from '../../app/redux/modules/todo';
const store = configureStore()();

View File

@ -3,7 +3,7 @@ import { render } from 'react-testing-library';
import { MemoryRouter } from 'react-router-dom';
import 'jest-dom/extend-expect';
import { SidebarComponent } from '../../app/components/sidebar/index';
import { SidebarComponent } from '../../app/components/Sidebar';
describe('<Sidebar />', () => {
describe('render()', () => {

View File

@ -1,5 +1,4 @@
import todoReducer from '../../app/reducers/todo';
import { ADD_TODO } from '../../app/constants/actions';
import todoReducer, { ADD_TODO } from '../../app/redux/modules/todo';
describe('Todo Reducer', () => {
test('should return the valid initial state', () => {

View File

@ -3,15 +3,23 @@
import React from 'react';
import { Provider } from 'react-redux';
import { ConnectedRouter } from 'connected-react-router';
import { ThemeProvider } from 'styled-components';
import { configureStore, history } from './redux/create';
import { Router } from './router/container';
import theme, { GlobalStyle } from './theme';
const store = configureStore({});
export default () => (
<Provider store={store}>
<ConnectedRouter history={history}>
<Router />
</ConnectedRouter>
</Provider>
<ThemeProvider theme={theme}>
<Provider store={store}>
<ConnectedRouter history={history}>
<div>
<GlobalStyle />
<Router />
</div>
</ConnectedRouter>
</Provider>
</ThemeProvider>
);

View File

@ -2,21 +2,26 @@
import React from 'react';
import styled from 'styled-components';
import { styles } from './styles';
const Layout = styled.div`
display: flex;
flex-direction: column;
width: 200px;
position: absolute;
width: calc(100vh - 200px);
left: 0;
top: 0;
height: 100vh;
background: #ccc;
`;
type Props = {
chidren: any, // eslint-disable-line
};
const Layout = styled.div`${styles.layout}`;
export const LayoutComponent = (props: Props) => {
// $FlowFixMe
const { children } = props; // eslint-disable-line
return (
<Layout>
{children}
</Layout>
);
return <Layout>{children}</Layout>;
};

View File

@ -3,13 +3,21 @@
import React from 'react';
import styled from 'styled-components';
import { Link } from 'react-router-dom';
import { MENU_OPTIONS } from '../../constants/sidebar';
import { styles } from './styles';
import { MENU_OPTIONS } from '../constants/sidebar';
// TODO: Not sure this is the best approach to styling
// in a StyledComponents-powered application
const Wrapper = styled.div`
${styles.wrapper}
display: flex;
flex-direction: column;
width: 200px;
position: absolute;
left: 0;
top: 0;
height: 100vh;
background-color: ${props => props.theme.colors.secondary};
`;
const StyledLink = styled(Link)`
color: ${props => props.theme.colors.primary};
`;
type MenuItem = { route: string, label: string };
@ -21,9 +29,9 @@ type Props = {
export const SidebarComponent = ({ options }: Props) => (
<Wrapper>
{(options || []).map(item => (
<Link key={item.route} to={item.route}>
<StyledLink key={item.route} to={item.route}>
{item.label}
</Link>
</StyledLink>
))}
</Wrapper>
);

View File

@ -3,7 +3,8 @@ name: Sidebar
---
import { Playground, PropsTable } from 'docz'
import { SidebarComponent } from './index.js'
import { SidebarComponent } from './Sidebar.js'
import { DoczWrapper } from '../theme.js'
# Sidebar
@ -12,5 +13,7 @@ import { SidebarComponent } from './index.js'
## Basic usage
<Playground>
<SidebarComponent />
<DoczWrapper>
{() => <SidebarComponent />}
</DoczWrapper>
</Playground>

View File

@ -1,7 +1,7 @@
// @flow
import React, { Component } from 'react';
import type { TodoType } from '../../types/todo';
import type { TodoType } from '../types/todo';
type Props = {
updateTodo: Function,
@ -33,17 +33,19 @@ export default class TodoEditInput extends Component<Props, State> {
updateTodo(id, trimValue);
this.setState({ value: '' });
}
}
};
handleCancel = (id: string) => {
const { cancelUpdateTodo } = this.props;
cancelUpdateTodo(id);
}
};
handleInputChange = (event: SyntheticInputEvent<HTMLInputElement>) => {
const { target: { value } } = event;
const {
target: { value },
} = event;
this.setState({ value });
}
};
render() {
const { value } = this.state;
@ -51,28 +53,13 @@ export default class TodoEditInput extends Component<Props, State> {
return (
<div className='todo-item__view todo-item__view--edit'>
<form
className='todo-item__input'
onSubmit={e => this.handleSubmit(e, todo.id)}
>
<input
value={value}
onChange={this.handleInputChange}
className='todo-item__input-field'
autoFocus
/>
<button
type='submit'
className='todo-item__input-button'
>
<form className='todo-item__input' onSubmit={e => this.handleSubmit(e, todo.id)}>
<input value={value} onChange={this.handleInputChange} className='todo-item__input-field' autoFocus />
<button type='submit' className='todo-item__input-button'>
Update
</button>
</form>
<button
type='button'
className='todo-item__input-cancel'
onClick={() => this.handleCancel(todo.id)}
>
<button type='button' className='todo-item__input-cancel' onClick={() => this.handleCancel(todo.id)}>
Cancel
</button>
</div>

View File

@ -26,30 +26,22 @@ export default class TodoInput extends Component<Props, State> {
addTodo(trimValue);
this.setState({ value: '' });
}
}
};
handleInputChange = (event: SyntheticInputEvent<HTMLInputElement>) => {
const { target: { value } } = event;
const {
target: { value },
} = event;
this.setState({ value });
}
};
render() {
const { value } = this.state;
return (
<form
className='todo__input'
onSubmit={this.handleSubmit}
>
<input
value={value}
onChange={this.handleInputChange}
className='todo__input-field'
/>
<button
type='submit'
className='todo__input-button'
>
<form className='todo__input' onSubmit={this.handleSubmit}>
<input value={value} onChange={this.handleInputChange} className='todo__input-field' />
<button type='submit' className='todo__input-button'>
Submit
</button>
</form>

View File

@ -1,9 +1,9 @@
// @flow
import React, { PureComponent } from 'react';
import TodoEditInput from './todo-edit-input';
import TodoListItem from './todo-list-item';
import type { TodoType } from '../../types/todo';
import TodoEditInput from './TodoEditInput';
import TodoListItem from './TodoListItem';
import type { TodoType } from '../types/todo';
type Props = {
todos: Array<TodoType>,
@ -17,26 +17,14 @@ export default class TodoList extends PureComponent<Props> {
renderTodoView = (todo: TodoType) => {
const { deleteTodo, toggleEdit } = this.props;
return (
<TodoListItem
todo={todo}
deleteTodo={deleteTodo}
toggleEdit={toggleEdit}
/>
);
}
return <TodoListItem todo={todo} deleteTodo={deleteTodo} toggleEdit={toggleEdit} />;
};
renderEditView = (todo: TodoType) => {
const { updateTodo, cancelUpdateTodo } = this.props;
return (
<TodoEditInput
todo={todo}
updateTodo={updateTodo}
cancelUpdateTodo={cancelUpdateTodo}
/>
);
}
return <TodoEditInput todo={todo} updateTodo={updateTodo} cancelUpdateTodo={cancelUpdateTodo} />;
};
renderList = () => {
const { todos } = this.props;
@ -45,24 +33,15 @@ export default class TodoList extends PureComponent<Props> {
return (
<ul className='todo__list'>
{sortTodosByTime.map(todo => (
<li
key={todo.id}
className='todo__list-item todo-item'
>
{todo.editing ? (
this.renderEditView(todo)
) : (
this.renderTodoView(todo)
)}
<li key={todo.id} className='todo__list-item todo-item'>
{todo.editing ? this.renderEditView(todo) : this.renderTodoView(todo)}
</li>
))}
</ul>
);
}
};
renderEmptyState = () => (
<p className='todo__list todo__list--empty'>No todos right now</p>
);
renderEmptyState = () => <p className='todo__list todo__list--empty'>No todos right now</p>;
render() {
const { todos } = this.props;

View File

@ -0,0 +1,55 @@
// @flow
import React, { PureComponent } from 'react';
import type { TodoType } from '../types/todo';
type Props = {
todo: TodoType,
deleteTodo: Function,
toggleEdit: Function,
};
export default class TodoListItem extends PureComponent<Props> {
handleDelete = (id: string) => {
if (!id) return;
const { deleteTodo } = this.props;
deleteTodo(id);
};
handleEditToggle = (id: string) => {
if (!id) return;
const { toggleEdit } = this.props;
toggleEdit(id);
};
render() {
const { todo } = this.props;
return (
<div className='todo-item__view todo-item__view--view'>
<span className='todo-item__text'>{todo.text}</span>
<div className='todo-item__buttons'>
<button type='button' onClick={() => this.handleEditToggle(todo.id)} className='todo-item__button'>
<svg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 528.899 528.899'>
<path
className='todo-item__svg'
d='M328.883 89.125l107.59 107.589-272.34 272.34L56.604 361.465l272.279-272.34zm189.23-25.948l-47.981-47.981c-18.543-18.543-48.653-18.543-67.259 0l-45.961 45.961 107.59 107.59 53.611-53.611c14.382-14.383 14.382-37.577 0-51.959zM.3 512.69c-1.958 8.812 5.998 16.708 14.811 14.565l119.891-29.069L27.473 390.597.3 512.69z'
/>
</svg>
</button>
<button type='button' onClick={() => this.handleDelete(todo.id)} className='todo-item__button'>
<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 512 512'>
<path
className='todo-item__svg'
fill='#1D1D1B'
d='M459.232 60.687h-71.955c-1.121-17.642-15.631-31.657-33.553-31.657H161.669c-17.921 0-32.441 14.015-33.553 31.657H64.579c-18.647 0-33.767 15.12-33.767 33.768v8.442c0 18.648 15.12 33.768 33.767 33.768h21.04v342.113c0 13.784 11.179 24.963 24.963 24.963h308.996c13.784 0 24.964-11.179 24.964-24.963V136.665h14.691c18.663 0 33.768-15.12 33.768-33.768v-8.442c-.001-18.648-15.105-33.768-33.769-33.768zM196.674 443.725c0 12.58-10.197 22.803-22.802 22.803-12.598 0-22.803-10.223-22.803-22.803v-284.9c0-12.597 10.205-22.802 22.803-22.802 12.605 0 22.802 10.206 22.802 22.802v284.9zm91.213 0c0 12.58-10.205 22.803-22.803 22.803s-22.803-10.223-22.803-22.803v-284.9c0-12.597 10.205-22.802 22.803-22.802s22.803 10.206 22.803 22.802v284.9zm91.212 0c0 12.58-10.205 22.803-22.803 22.803-12.613 0-22.803-10.223-22.803-22.803v-284.9c0-12.597 10.189-22.802 22.803-22.802 12.598 0 22.803 10.206 22.803 22.802v284.9z'
/>
</svg>
</button>
</div>
</div>
);
}
}

View File

@ -1,15 +0,0 @@
// @flow
export const styles: Object = {
layout: `
display: flex;
flex-direction: column;
width: 200px;
position: absolute;
width: calc(100vh - 200px);
left: 0;
top: 0;
height: 100vh;
background: #ccc;
`,
};

View File

@ -1,14 +0,0 @@
// @flow
export const styles: Object = {
wrapper: `
display: flex;
flex-direction: column;
width: 200px;
position: absolute;
left: 0;
top: 0;
height: 100vh;
background: #ccc;
`,
};

View File

@ -1,56 +0,0 @@
// @flow
import React, { PureComponent } from 'react';
import type { TodoType } from '../../types/todo';
type Props = {
todo: TodoType,
deleteTodo: Function,
toggleEdit: Function,
};
export default class TodoListItem extends PureComponent<Props> {
handleDelete = (id: string) => {
if (!id) return;
const { deleteTodo } = this.props;
deleteTodo(id);
}
handleEditToggle = (id: string) => {
if (!id) return;
const { toggleEdit } = this.props;
toggleEdit(id);
}
render() {
const {
todo,
} = this.props;
return (
<div className='todo-item__view todo-item__view--view'>
<span className='todo-item__text'>
{todo.text}
</span>
<div className='todo-item__buttons'>
<button
type='button'
onClick={() => this.handleEditToggle(todo.id)}
className='todo-item__button'
>
<svg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 528.899 528.899'><path className='todo-item__svg' d='M328.883 89.125l107.59 107.589-272.34 272.34L56.604 361.465l272.279-272.34zm189.23-25.948l-47.981-47.981c-18.543-18.543-48.653-18.543-67.259 0l-45.961 45.961 107.59 107.59 53.611-53.611c14.382-14.383 14.382-37.577 0-51.959zM.3 512.69c-1.958 8.812 5.998 16.708 14.811 14.565l119.891-29.069L27.473 390.597.3 512.69z' /></svg>
</button>
<button
type='button'
onClick={() => this.handleDelete(todo.id)}
className='todo-item__button'
>
<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 512 512'><path className='todo-item__svg' fill='#1D1D1B' d='M459.232 60.687h-71.955c-1.121-17.642-15.631-31.657-33.553-31.657H161.669c-17.921 0-32.441 14.015-33.553 31.657H64.579c-18.647 0-33.767 15.12-33.767 33.768v8.442c0 18.648 15.12 33.768 33.767 33.768h21.04v342.113c0 13.784 11.179 24.963 24.963 24.963h308.996c13.784 0 24.964-11.179 24.964-24.963V136.665h14.691c18.663 0 33.768-15.12 33.768-33.768v-8.442c-.001-18.648-15.105-33.768-33.769-33.768zM196.674 443.725c0 12.58-10.197 22.803-22.802 22.803-12.598 0-22.803-10.223-22.803-22.803v-284.9c0-12.597 10.205-22.802 22.803-22.802 12.605 0 22.802 10.206 22.802 22.802v284.9zm91.213 0c0 12.58-10.205 22.803-22.803 22.803s-22.803-10.223-22.803-22.803v-284.9c0-12.597 10.205-22.802 22.803-22.802s22.803 10.206 22.803 22.802v284.9zm91.212 0c0 12.58-10.205 22.803-22.803 22.803-12.613 0-22.803-10.223-22.803-22.803v-284.9c0-12.597 10.189-22.802 22.803-22.802 12.598 0 22.803 10.206 22.803 22.802v284.9z' /></svg>
</button>
</div>
</div>
);
}
}

4
app/constants/themes.js Normal file
View File

@ -0,0 +1,4 @@
// @flow
export const LIGHT = 'light';
export const DARK = 'dark';

View File

@ -1,7 +1,7 @@
// @flow
import { connect } from 'react-redux';
import { SidebarComponent } from '../components/sidebar';
import { SidebarComponent } from '../components/Sidebar';
const mapStateToProps = (state: Object) => ({
todos: state.todos,

35
app/theme.js Normal file
View File

@ -0,0 +1,35 @@
// @flow
import React from 'react';
import { ThemeProvider, createGlobalStyle } from 'styled-components';
// $FlowFixMe
import { normalize } from 'polished';
import theme from 'styled-theming';
import { DARK } from './constants/themes';
const appTheme = {
mode: DARK,
fontFamily: 'Open Sans',
colors: {
primary: theme('mode', {
light: '#fff',
dark: '#000',
}),
secondary: theme('mode', {
light: '#000',
dark: '#fff',
}),
},
size: {
title: 18,
paragraph: 12,
},
};
/* eslint-disable react/prop-types */
// $FlowFixMe
export const DoczWrapper = ({ children }) => <ThemeProvider theme={appTheme}>{children()}</ThemeProvider>;
export const GlobalStyle = createGlobalStyle`${normalize()}`;
export default appTheme;

View File

@ -1,8 +1,8 @@
// @flow
import React from 'react';
import TodoInput from '../components/todo/todo-input';
import TodoList from '../components/todo/todo-list';
import TodoInput from '../components/TodoInput';
import TodoList from '../components/TodoList';
import type { TodoType } from '../types/todo';
import checklist from '../assets/images/checklist.svg';
@ -17,12 +17,7 @@ type Props = {
export default (props: Props) => {
const {
addTodo,
todos,
deleteTodo,
toggleEdit,
updateTodo,
cancelUpdateTodo,
addTodo, todos, deleteTodo, toggleEdit, updateTodo, cancelUpdateTodo,
} = props;
return (

View File

@ -59,7 +59,7 @@
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"file-loader": "^2.0.0",
"flow-bin": "^0.86.0",
"flow-bin": "^0.87.0",
"glow": "^1.2.2",
"html-webpack-plugin": "^3.1.0",
"jest": "^23.6.0",
@ -86,6 +86,7 @@
"connected-react-router": "^5.0.1",
"flow-coverage-report": "^0.6.0",
"history": "^4.7.2",
"polished": "^2.3.0",
"react": "^16.6.0",
"react-dom": "^16.6.0",
"react-redux": "^5.0.7",
@ -93,6 +94,7 @@
"redux": "^4.0.1",
"redux-thunk": "^2.2.0",
"styled-components": "^4.1.1",
"styled-theming": "^2.2.0",
"uuid": "^3.3.2"
},
"pre-commit": [

View File

@ -5752,10 +5752,10 @@ flow-annotation-check@1.8.1:
glob "7.1.1"
load-pkg "^3.0.1"
flow-bin@^0.86.0:
version "0.86.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.86.0.tgz#153a28722b4dc13b7200c74b644dd4d9f4969a11"
integrity sha512-ulRvFH3ewGIYwg+qPk/OJXoe3Nhqi0RyR0wqgK0b1NzUDEC6O99zU39MBTickXvlrr6iwRO6Wm4lVGeDmnzbew==
flow-bin@^0.87.0:
version "0.87.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.87.0.tgz#fab7f984d8cc767e93fa9eb01cf7d57ed744f19d"
integrity sha512-mnvBXXZkUp4y6A96bR5BHa3q1ioIIN2L10w5osxJqagAakTXFYZwjl0t9cT3y2aCEf1wnK6n91xgYypQS/Dqbw==
flow-coverage-report@^0.6.0:
version "0.6.0"
@ -12959,6 +12959,11 @@ styled-components@^4.1.1:
stylis-rule-sheet "^0.0.10"
supports-color "^5.5.0"
styled-theming@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/styled-theming/-/styled-theming-2.2.0.tgz#3084e43d40eaab4bc11ebafd3de04e3622fee37e"
integrity sha1-MITkPUDqq0vBHrr9PeBONiL+434=
stylehacks@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/stylehacks/-/stylehacks-4.0.1.tgz#3186595d047ab0df813d213e51c8b94e0b9010f2"