Kbone基础——React + Kbone 实现 Todos App_kbone+react

npm start


#### 2、编写React todos代码


#### 2.1 改写index.js


引入组件Todos.js。



// …

import Todos from ‘./Todos’;
// …

ReactDOM.render(, document.getElementById(‘root’));
// …


#### 2.2 编写 Todos.css


在项目目录 `react-todos/src` 下创建 `Todos.css`:



body {
background-color: #222222;
min-height: 100vh;
}

.app {
padding-top: 10rem;
}

.header {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: 50px;
color: yellowgreen;
}

.logo {
animation: App-logo-spin infinite 20s linear;
height: 2vmin;
pointer-events: none;
}

.todo-list {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: white;
}

.todo {
display: flex;
align-items: center;
margin: 1rem 0;
}

.todo-is-completed .checkbox {
color: #000;
background: #fff;
}

.todo-is-completed input {
text-decoration: line-through;
}

.checkbox {
width: 18px;
height: 18px;
border-radius: 50%;
margin-right: 10px;
cursor: pointer;
font-size: 10px;
display: flex;
justify-content: center;
align-items: center;
transition: background-color .2s ease-in-out;
border: 1px solid #fff;
}

.checkbox:hover {
background: rgba(255, 255, 255, 0.25);
border: 1px solid rgba(255, 255, 255, 0);
}

.checkbox-checked {
color: #000;
background: #fff;
}

ul {
list-style: none;
padding: 0;
line-height: 2rem;
}

input {
border: none;
background: transparent;
color: white;
font-size: 1.4rem;
outline: none;
width: 100%;
}


#### 2.3 编写 Todos.js 组件


在项目目录 `react-todos/src` 下创建 `Todos.js`:



import React, { useState } from ‘react’
import ‘./Todos.css’

function Todos() {
const [todos, setTodos] = useState([
{
content: ‘干洗衣服’,
isCompleted: true,
},
{
content: ‘理发’,
isCompleted: false,
},
{
content: ‘学习Kbone’,
isCompleted: false,
}
])

function handleKeyDown(e, i) {
if (e.key === ‘Enter’) {
createTodoAtIndex(e, i)
}
if (e.key === ‘Backspace’ && todos[i].content === ‘’) {
e.preventDefault();
return removeTodoAtIndex(i)
}
}

function createTodoAtIndex(e, i) {
const newTodos = […todos];
newTodos.splice(i + 1, 0, {
content: ‘’,
isCompleted: false,
})
setTodos(newTodos);
setTimeout(() => {
document.forms[0].elements[i + 1].focus();
}, 0)
}

function updateTodoAtIndex(e, i) {
const newTodos = […todos]
newTodos[i].content = e.target.value
setTodos(newTodos)
}

function removeTodoAtIndex(i) {
if (i === 0 && todos.length === 1) return
setTodos(todos => todos.slice(0, i).concat(todos.slice(i + 1, todos.length)))
setTimeout(() => {
document.forms[0].elements[i - 1].focus()
}, 0)
}

function toggleTodoCompleteAtIndex(index) {
const temporaryTodos = […todos]
temporaryTodos[index].isCompleted = !temporaryTodos[index].isCompleted
setTodos(temporaryTodos)
}

return (



React Todos



  • {todos.map((todo, i) => (
    <div key={todo+i} className={ todo ${todo.isCompleted && 'todo-is-completed'}}>
    <div className={‘checkbox’} onClick={() => toggleTodoCompleteAtIndex(i)}>
    {todo.isCompleted && (

    )}

<input
type=“text”
value={todo.content}
onKeyDown={e => handleKeyDown(e, i)}
onChange={e => updateTodoAtIndex(e, i)}
/>

))}



)
}

export default Todos


#### 2.4 项目预览效果


在控制台执行 `npm start` 命令,浏览器效果如下:


![](https://img-blog.csdnimg.cn/94f33e6d17a44f7a8f3057bb5083f3f1.png)


 


#### 3、将 React 项目编入Kbone


#### 3.1 搭建Kbone的React环境



kbone init kbone-react
模板选择 react

cd kbone-react
npm run web
npm run mp


效果如下图:


![](https://img-blog.csdnimg.cn/ed8da0c6b02e4ce48698bce4b2dff033.png)


 


![](https://img-blog.csdnimg.cn/8cab8854726648f088bc021f7ec9369c.png)


#### 3.2 修改 index.js


修改 `kbone-react/src/index.js`:



// …
// import Counter from ‘./components/counter’
import Todos from ‘./components/todos/Todos’

export default function createApp() {
// …

ReactDOM.render(, container)
}

// …


#### 3.3 创建 Todos.js 组件


在 kbone-react/src/components 下创建 todos文件夹,创建Todos.js,内容如下:



import React, { useState } from ‘react’
import ‘./Todos.css’

function Todos() {
const [todos, setTodos] = useState([
{
content: ‘干洗衣服’,
isCompleted: true,
},
{
content: ‘理发’,
isCompleted: false,
},
{
content: ‘学习Kbone’,
isCompleted: false,
}
])

function handleKeyDown(e, i) {
if (e.key === ‘Enter’) {
createTodoAtIndex(e, i)
}
if (e.key === ‘Backspace’ && todos[i].content === ‘’) {
e.preventDefault();
return removeTodoAtIndex(i)
}
}

function createTodoAtIndex(e, i) {
const newTodos = […todos];
newTodos.splice(i + 1, 0, {
content: ‘’,
isCompleted: false,
})
setTodos(newTodos);
setTimeout(() => {
document.forms[0].elements[i + 1].focus();
}, 0)
}

function updateTodoAtIndex(e, i) {
const newTodos = […todos]
newTodos[i].content = e.target.value
setTodos(newTodos)
}

function removeTodoAtIndex(i) {
if (i === 0 && todos.length === 1) return
setTodos(todos => todos.slice(0, i).concat(todos.slice(i + 1, todos.length)))
setTimeout(() => {
document.forms[0].elements[i - 1].focus()
}, 0)
}

function toggleTodoCompleteAtIndex(index) {
const temporaryTodos = […todos]
temporaryTodos[index].isCompleted = !temporaryTodos[index].isCompleted
setTodos(temporaryTodos)
}

return (



React Todos



  • {todos.map((todo, i) => (
    <div key={todo+i} className={ todo ${todo.isCompleted && 'todo-is-completed'}}>
    <div className={‘checkbox’} onClick={() => toggleTodoCompleteAtIndex(i)}>
    {todo.isCompleted && (

    )}

<input
type=“text”
value={todo.content}
onKeyDown={e => handleKeyDown(e, i)}
onChange={e => updateTodoAtIndex(e, i)}
/>

))}



)
}

export default Todos


#### 3.4 创建 Todos.css 样式



body {
background-color: #222222;
min-height: 100vh;
}

.app {
padding-top: 10rem;
}

.header {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: 50px;
color: yellowgreen;
}

.logo {
animation: App-logo-spin infinite 20s linear;
height: 2vmin;
pointer-events: none;
}

.todo-list {
display: flex;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值