Skip to content

Feature/atomic components #11

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"typescript": "^3.5.1"
},
"dependencies": {
"@material-ui/core": "^4.0.2",
"connected-react-router": "^6.4.0",
"formik": "^1.5.7",
"history": "^4.9.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
Expand Down
7 changes: 2 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import { Provider } from "react-redux";
import { ConnectedRouter } from "connected-react-router";
import { AppShell } from "./AppShell";
import { browserHistory, store } from "./store";
import { Index } from "./pages";

export const App = () => (
<Provider store={store}>
<AppShell>
<ConnectedRouter history={browserHistory}>
<Switch>
<Route
exact
path="/"
render={() => <div>Atomic Design with Redux</div>}
/>
<Route exact path="/" render={() => <Index />} />
<Route render={() => <div>Miss</div>} />
</Switch>
</ConnectedRouter>
Expand Down
9 changes: 8 additions & 1 deletion src/AppShell.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import * as React from "react";
import CssBaseline from "@material-ui/core/CssBaseline";
import Container from "@material-ui/core/Container";

export const AppShell: React.FC = props => {
return <div>{props.children}</div>;
return (
<React.Fragment>
<CssBaseline />
<Container maxWidth="sm">{props.children}</Container>
</React.Fragment>
);
};
10 changes: 10 additions & 0 deletions src/components/atoms/form/SubmitButtonComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as React from "react";
import Button from '@material-ui/core/Button';

export const SubmitButtonComponent: React.FC = ({ children, ...props}) => {
return (
<Button variant="contained" color="primary" {...props}>
{children}
</Button>
);
};
14 changes: 14 additions & 0 deletions src/components/atoms/form/TextInputComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as React from 'react';
import { FieldProps } from "formik";
import TextField from '@material-ui/core/TextField';

export const TextInputComponent: React.FC<FieldProps> = ({ field, form, ...props }) => {
return (
<TextField
{...field}
{...props}
label={props.placeholder}
style={{ width: '100%' }}
/>
);
};
36 changes: 36 additions & 0 deletions src/components/organisms/TodoFormComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as React from "react";
import Box from '@material-ui/core/Box';
import { Formik, Field, Form } from "formik";
import { TodoFormProps } from "../../containers/TodoFormContainer";
import { TextInputComponent } from "../atoms/form/TextInputComponent";
import { SubmitButtonComponent as SubmitButton } from "../atoms/form/SubmitButtonComponent";

type TodoFormValues = {
description: string;
};

export const TodoFormComponent: React.FC<TodoFormProps> = props => {
return (
<Formik
initialValues={{ description: "" }}
onSubmit={(value: TodoFormValues, actions) => {
props.actions.addTodo({
id: props.newId,
description: value.description
});
actions.setSubmitting(false);
actions.resetForm();
}}
render={({ isSubmitting }) => (
<Form>
<Box display="flex">
<Field type="text" name="description" component={TextInputComponent} placeholder="Todo Description"/>
<SubmitButton type="submit" disabled={isSubmitting}>
Add
</SubmitButton>
</Box>
</Form>
)}
/>
);
};
20 changes: 20 additions & 0 deletions src/components/organisms/TodoListComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as React from "react";
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemText from '@material-ui/core/ListItemText';
import { TodoListProps } from "../../containers/TodoListContainer";
import { Todo } from "../../store/todos/entity";

export const TodoListComponent: React.FC<TodoListProps> = props => {
const todos = props.todos;
const listItems = todos.map((todo: Todo) =>
<ListItem component="div" disableGutters={true}>
<ListItemText primary={todo.description}/>
</ListItem>
);
return (
<List component="div">
{listItems}
</List>
);
};
23 changes: 23 additions & 0 deletions src/containers/TodoFormContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { bindActionCreators, Dispatch } from "redux";
import { connect } from "react-redux";
import { InitialState } from "../store";
import { addTodo } from "../store/todos/actions";
import { TodoFormComponent } from "../components/organisms/TodoFormComponent";

const mapStateToProps = (state: InitialState) => {
return {
todos: state.todos,
newId: state.todos.length + 1
}
};

const mapDispatchToProps = (dispatch: Dispatch) => {
return { actions: bindActionCreators({ addTodo }, dispatch) }
};

export type TodoFormProps = ReturnType<typeof mapStateToProps> & ReturnType<typeof mapDispatchToProps>;

export const TodoForm = connect(
mapStateToProps,
mapDispatchToProps
)(TodoFormComponent);
18 changes: 18 additions & 0 deletions src/containers/TodoListContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { connect } from "react-redux";
import { InitialState } from "../store";
import { TodoListComponent } from "../components/organisms/TodoListComponent";

const mapStateToProps = (state: InitialState) => {
return {
todos: state.todos
}
};

const mapDispatchToProps = () => ({});

export type TodoListProps = ReturnType<typeof mapStateToProps> & ReturnType<typeof mapDispatchToProps>;

export const TodoList = connect(
mapStateToProps,
mapDispatchToProps
)(TodoListComponent);
12 changes: 12 additions & 0 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as React from "react";
import { TodoList } from "../containers/TodoListContainer";
import { TodoForm } from "../containers/TodoFormContainer";

export const Index: React.FC = () => {
return (
<div>
<TodoList/>
<TodoForm/>
</div>
)
};
1 change: 1 addition & 0 deletions src/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<link rel="manifest" href="manifest.webmanifest" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" />
<title>React App</title>
</head>
<body>
Expand Down
Loading