Skip to content

Commit

Permalink
feat: jsx
Browse files Browse the repository at this point in the history
  • Loading branch information
mohaalak committed Jul 23, 2019
1 parent 5cfd5ed commit 3e98cfb
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 169 deletions.
13 changes: 13 additions & 0 deletions TodoItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as Rahnemaak from './rahnemaak';
// @jsx Rahnemaak.createElement
export class TodoItemComponent extends Rahnemaak.Component {
render() {
const { todo, toggle } = this.props;
const className = todo.checked ? 'checked' : '';
return (
<li className={className} onClick={() => toggle(todo)}>
{todo.text}
</li>
);
}
}
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
</head>
<body>
<div id="root"></div>
<script src="./test.ts"></script>
<script src="./index.tsx"></script>
</body>
</html>
147 changes: 0 additions & 147 deletions index.js

This file was deleted.

132 changes: 132 additions & 0 deletions index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import * as Rahnemaak from './rahnemaak';
import { TodoItemComponent } from './TodoItem';
// @jsx Rahnemaak.createElement
const root = document.getElementById('root');
function removeChildren() {
for (const child of root.children) {
root.removeChild(child);
}
}
removeChildren();

interface TodoItem {
text: string;
checked: boolean;
}
type TodoStatus = 'all' | 'done' | 'todo';
interface TodoState {
todos: TodoItem[];
status: TodoStatus;
}

class TodoApplication {
state: TodoState = {
todos: [{ text: 'Hamid', checked: true }],
status: 'all'
};
constructor() {
this.addTodo = this.addTodo.bind(this);
this.toggleTodo = this.toggleTodo.bind(this);
}

setState(state: Partial<TodoState>) {
this.state = { ...this.state, ...state };

renderApp();
}

private setStatus(status: TodoStatus) {
this.setState({ status });
}

private addTodo(e: any) {
e.preventDefault();
const todo = e.target.children[0].value;

this.setState({
todos: [...this.state.todos, { text: todo, checked: false }]
});
}

getTodos() {
const { status, todos } = this.state;

if (status === 'all') {
return todos;
}
if (status === 'done') {
return todos.filter(x => x.checked);
}

if (status === 'todo') {
return todos.filter(x => !x.checked);
}
}
toggleTodo(todoItem: TodoItem) {
const foundTodo = this.state.todos.find(x => x === todoItem);
foundTodo.checked = !foundTodo.checked;

this.setState({
todos: this.state.todos
});
}

renderForm() {
return (
<form onSubmit={this.addTodo}>
<input />
<button>Submit</button>
</form>
);
}

renderTodoItem(todo: TodoItem) {
console.log('salam');
return <TodoItemComponent todo={todo} toggle={this.toggleTodo} />;
}
renderTodoContainer() {
return <ul>{this.getTodos().map(x => this.renderTodoItem(x))}</ul>;
}

renderFooterButton(text: string, inputStatus: TodoStatus) {
const { status } = this.state;
return (
<button
className={status === inputStatus ? 'active' : ''}
onClick={e => this.setStatus(inputStatus)}
>
{text}
</button>
);
}
renderFooter() {
return (
<div>
{this.renderFooterButton('All', 'all')}
{this.renderFooterButton('Done', 'done')}
{this.renderFooterButton('Todo', 'todo')}
</div>
);
}

render() {
return (
<div>
{this.renderForm()}
{this.renderTodoContainer()}
{this.renderFooter()}
</div>
);
}
}

const todoApp = new TodoApplication();

function renderApp() {
const element = todoApp.render();
removeChildren();

Rahnemaak.render(root, element);
}

renderApp();
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
"license": "ISC",
"devDependencies": {
"typescript": "^3.5.3"
},
"dependencies": {
"@types/lodash": "^4.14.136",
"lodash": "^4.17.15"
}
}
Loading

0 comments on commit 3e98cfb

Please sign in to comment.