10
0

demo.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <!-- jsx 语法浏览器不识别 jsx => js 浏览器可识别的语言 -->
  8. <script src="../babel.min.js"></script>
  9. <!-- react的核心库 -->
  10. <script src="../react.development.js"></script>
  11. <!-- 解析react中的dom库 -->
  12. <script src="../react-dom.development.js"></script>
  13. <link rel="stylesheet" href="./index.css" />
  14. </head>
  15. <body>
  16. <div id="root"></div>
  17. <script type="text/babel">
  18. let root = ReactDOM.createRoot(document.getElementById("root"));
  19. function App() {
  20. return (
  21. <div>
  22. <Box />
  23. </div>
  24. );
  25. }
  26. class Box extends React.Component {
  27. constructor() {
  28. super();
  29. this.state = {
  30. todos: [
  31. {
  32. id: 1,
  33. title: "吃饭",
  34. completed: true,
  35. },
  36. {
  37. id: 2,
  38. title: "睡觉",
  39. completed: false,
  40. },
  41. ],
  42. filters: "all", //complete active all
  43. };
  44. this.unChooseMain = this.unChooseMain.bind(this);
  45. }
  46. // 渲染列表
  47. renderList() {
  48. // console.log(this.state.todos, "this.state.todos");
  49. const { todos, filters } = this.state;
  50. // 如果filters是all 显示所有的todos
  51. // 如果filters是active 显示complete是false
  52. // 如果filters是complete 显示complete是true
  53. if (filters === "all") return todos;
  54. return todos.filter((ele) =>
  55. filters === "active" ? !ele.completed : ele.completed
  56. );
  57. }
  58. // 添加数据
  59. addMain(val) {
  60. console.log(val, "值");
  61. this.setState((prevState) => ({
  62. todos: [
  63. ...prevState.todos,
  64. {
  65. id: Date.now(),
  66. title: val,
  67. completed: false,
  68. },
  69. ],
  70. }));
  71. }
  72. // 选中项
  73. chooseMain(val) {
  74. const { todos } = this.state;
  75. this.setState(() => {
  76. for (var i = 0; i < todos.length; i++) {
  77. if (todos[i].id === val) {
  78. todos[i].completed = !todos[i].completed;
  79. }
  80. }
  81. return {todos}
  82. });
  83. }
  84. // 删除项
  85. removeMain(val) {
  86. if(!confirm("您确定删除该条数据么")) return;
  87. const {todos} = this.state;
  88. this.setState((prevState)=>({
  89. todos: prevState.todos.filter((ele)=>{
  90. return ele.id !== val;
  91. })
  92. }))
  93. }
  94. // 未选择项
  95. unChooseMain() {
  96. const {todos} = this.state;
  97. return todos.filter((ele) => !ele.completed).length;
  98. }
  99. // 切换过滤项
  100. setFilters(val) {
  101. console.log(this.state,'3333',val)
  102. this.setState({
  103. filters: val
  104. })
  105. }
  106. // 全选
  107. allChoose(val) {
  108. console.log(val,'val')
  109. const {todos} = this.state;
  110. this.setState((prevState)=>({
  111. todos:prevState.todos.map((ele)=>{
  112. ele.completed = val;
  113. return ele;
  114. })
  115. }))
  116. }
  117. render() {
  118. // console.log(this)
  119. // const {todos} = this.state
  120. return (
  121. <div>
  122. <section className="todoapp">
  123. {/*头部*/}
  124. <Header addList={this.addMain.bind(this)} />
  125. {/*主体*/}
  126. <Main
  127. toggle={this.allChoose.bind(this)}
  128. todos={this.renderList()}
  129. choose={this.chooseMain.bind(this)}
  130. remove = {this.removeMain.bind(this)}
  131. />
  132. {/*尾部*/}
  133. <Footer unChoose= {this.unChooseMain()} filter={this.state.filters} setMain={this.setFilters.bind(this)} />
  134. </section>
  135. </div>
  136. );
  137. }
  138. }
  139. function Header({ addList }) {
  140. return (
  141. <header className="header">
  142. <h1>todos</h1>
  143. <input
  144. autoFocus="autofocus"
  145. autoComplete="off"
  146. placeholder="输入您要完成的任务?"
  147. className="new-todo"
  148. onKeyDown={(e) => {
  149. if (e.keyCode == 13) {
  150. addList(e.target.value);
  151. e.target.value = "";
  152. }
  153. }}
  154. />
  155. </header>
  156. );
  157. }
  158. function Main({ todos, choose,remove,toggle }) {
  159. // console.log(todos,'todos')
  160. return (
  161. <section className="main">
  162. <input id="toggle-all" type="checkbox" className="toggle-all" onChange={(e)=>{toggle(e.target.checked)}} />
  163. <label htmlFor="toggle-all"></label>
  164. <ul className="todo-list">
  165. {todos &&
  166. todos.map((ele) => (
  167. <Item key={ele.id} todo={ele} choice={choose} del={remove} />
  168. ))}
  169. </ul>
  170. </section>
  171. );
  172. }
  173. function Footer({unChoose, filter, setMain}) {
  174. return (
  175. <footer className="footer">
  176. <span className="todo-count">
  177. <strong>{unChoose}</strong> items left{" "}
  178. </span>
  179. <ul className="filters">
  180. <li>
  181. <a href="#/all" className={filter == 'all' ? 'selected' : ''} onClick={()=>{
  182. setMain("all")
  183. }}>
  184. All
  185. </a>
  186. </li>
  187. <li>
  188. <a href="#/active" className={filter == 'active' ? 'selected' : ''} onClick={()=>{
  189. setMain("active")
  190. }}>
  191. Active
  192. </a>
  193. </li>
  194. <li>
  195. <a href="#/completed" className={filter == 'completed' ? 'selected' : ''} onClick={()=>{
  196. setMain("completed")
  197. }}>
  198. Completed
  199. </a>
  200. </li>
  201. </ul>
  202. <button className="clear-completed">Clear completed</button>
  203. </footer>
  204. );
  205. }
  206. function Item({ todo, choice,del }) {
  207. // console.log(todo,{})
  208. return (
  209. <li className="todo">
  210. <div className="view">
  211. <input
  212. type="checkbox"
  213. checked={todo.completed}
  214. onChange={() => choice(todo.id)}
  215. className="toggle"
  216. />
  217. <label>{todo.title}</label>
  218. <button className="destroy" onClick={()=> del(todo.id)}></button>
  219. </div>
  220. <input type="text" className="edit" />
  221. </li>
  222. );
  223. }
  224. root.render(<App />);
  225. </script>
  226. </body>
  227. </html>