References: Creating your first app
1. Input Form 만들기
계속 콘솔로 데이터를 넣어줄 순 없으니 인풋 폼을 만들어 줍니다.
/imports/ui/app.jsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import React, { Component } from 'react'; import { withTracker } from 'meteor/react-meteor-data'; import { Tasks } from '../api/tasks.js'; import ReactDOM from 'react-dom'; import Task from './Task'; // App component - represents the whole app class App extends Component { handleSubmit(event) { event.preventDefault(); // Find the text field via the React ref const text = ReactDOM.findDOMNode(this.refs.textInput).value.trim(); Tasks.insert({ text, createdAt: new Date(), // current time }); // Clear form ReactDOM.findDOMNode(this.refs.textInput).value = ''; } renderTasks() { return this.props.tasks.map((task) => ( <Task key={task._id} task={task} /> )); } render() { return ( <div className="container"> <header> <h1>Todo List</h1> <form className="new-task" onSubmit={this.handleSubmit.bind(this)} > <input type="text" ref="textInput" placeholder="Type to add new tasks" /> </form> </header> <ul> {this.renderTasks()} </ul> </div> ); } } export default withTracker(() => { return { tasks: Tasks.find({}).fetch(), }; })(App); | cs |
다음과 같이 인풋 창이 생깁니다. (New task!를 입력한 상태)
이 상태에서 엔터키를 입력하면 데이터가 저장됩니다.
만약 가장 최근에 등록한 글을 맨 위에 보고 싶으면 다음과 같이 수정 해 주시면 됩니다.
/imports/ui/app.jsx Line:49
export default withTracker(() => {
return {
tasks: Tasks.find({}, { sort: { createdAt: -1 } }).fetch(),
};
})(App);
2. 완료 체크박스와 삭제 버튼 만들기
체크 박스와 삭제 버튼을 추가해 봅시다.
/import/ui/Task.jsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import React, { Component } from 'react'; import { Tasks } from '../api/tasks.js'; // Task component - represents a single todo item export default class Task extends Component { toggleChecked() { // Set the checked property to the opposite of its current value Tasks.update(this.props.task._id, { $set: { checked: !this.props.task.checked }, }); } deleteThisTask() { Tasks.remove(this.props.task._id); } render() { // Give tasks a different className when they are checked off, // so that we can style them nicely in CSS const taskClassName = this.props.task.checked ? 'checked' : ''; return ( <li className={taskClassName}> <button className="delete" onClick={this.deleteThisTask.bind(this)}> × </button> <input type="checkbox" readOnly checked={!!this.props.task.checked} onClick={this.toggleChecked.bind(this)} /> <span className="text">{this.props.task.text}</span> </li> ); } } | cs |
다음과 같은 결과를 확인할 수 있습니다.
이번엔 완료 체크된 작업은 숨길 수 있는 기능과 미완료 작업의 갯수를 표시해 보겠습니다.
/imports/ui/app.jsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | import React, { Component } from 'react'; import { withTracker } from 'meteor/react-meteor-data'; import { Tasks } from '../api/tasks.js'; import ReactDOM from 'react-dom'; import Task from './Task'; // App component - represents the whole app class App extends Component { constructor(props) { super(props); this.state = { hideCompleted: false, }; } handleSubmit(event) { event.preventDefault(); // Find the text field via the React ref const text = ReactDOM.findDOMNode(this.refs.textInput).value.trim(); Tasks.insert({ text, createdAt: new Date(), // current time }); // Clear form ReactDOM.findDOMNode(this.refs.textInput).value = ''; } renderTasks() { let filteredTasks = this.props.tasks; if (this.state.hideCompleted) { filteredTasks = filteredTasks.filter(task => !task.checked); } return filteredTasks.map((task) => ( <Task key={task._id} task={task} /> )); } toggleHideCompleted() { this.setState({ hideCompleted: !this.state.hideCompleted, }); } render() { return ( <div className="container"> <header> <h1>Todo List ({this.props.incompleteCount})</h1> <label className="hide-completed"> <input type="checkbox" readOnly checked={this.state.hideCompleted} onClick={this.toggleHideCompleted.bind(this)} /> Hide Completed Tasks </label> <form className="new-task" onSubmit={this.handleSubmit.bind(this)} > <input type="text" ref="textInput" placeholder="Type to add new tasks" /> </form> </header> <ul> {this.renderTasks()} </ul> </div> ); } } export default withTracker(() => { return { tasks: Tasks.find({}, { sort: { createdAt: -1 } }).fetch(), incompleteCount: Tasks.find({ checked: { $ne: true } }).count(), }; })(App); | cs |
완료되지 않은 작업의 갯수를 확인 할 수 있고 Hide Completed Tasks 옵션을 통해 완료한 작업을 숨길 수 있습니다.
반응형
'Programming > JavaScript' 카테고리의 다른 글
NodeJS - Meteor with React Tutorial (4) (0) | 2019.02.19 |
---|---|
NodeJS - Meteor with React Tutorial (3) (0) | 2019.02.18 |
NodeJS - Meteor with React Tutorial (1) (0) | 2019.02.18 |
NodeJS - Meteor 프레임워크 Blaze? React? (0) | 2019.02.15 |
NodeJS - Meteor Autopublish (0) | 2019.02.15 |