Skip to content

Commit

Permalink
Merge pull request #1382 from WenHaoHub/task1
Browse files Browse the repository at this point in the history
task1: WenHaoHub
  • Loading branch information
ourai authored Sep 10, 2024
2 parents 9604fe7 + 1360628 commit 9b54ff9
Show file tree
Hide file tree
Showing 22 changed files with 567 additions and 0 deletions.
18 changes: 18 additions & 0 deletions members/WenHaoHub/task1/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
}
24 changes: 24 additions & 0 deletions members/WenHaoHub/task1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
3 changes: 3 additions & 0 deletions members/WenHaoHub/task1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Task1
todilist

16 changes: 16 additions & 0 deletions members/WenHaoHub/task1/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Todo</title>
</head>

<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>

</html>
32 changes: 32 additions & 0 deletions members/WenHaoHub/task1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "task1",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"clsx": "^2.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"@vitejs/plugin-react-swc": "^3.5.0",
"autoprefixer": "^10.4.19",
"eslint": "^8.57.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.6",
"postcss": "^8.4.38",
"tailwindcss": "^3.4.3",
"typescript": "^5.2.2",
"vite": "^5.2.0"
}
}
6 changes: 6 additions & 0 deletions members/WenHaoHub/task1/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
1 change: 1 addition & 0 deletions members/WenHaoHub/task1/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions members/WenHaoHub/task1/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}

.todo {
display: flex;
height: 65px;
}
34 changes: 34 additions & 0 deletions members/WenHaoHub/task1/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useEffect, useState } from 'react'
import './App.css'
import Header from './componets/Header'
import AddToDo from './componets/AddToDo'
import ToDoList from './componets/ToDoList'

export interface Todo {
id: string
text: string
completed: boolean
}

const STORAGE_KEY = 'todo'

function App() {
const [data, setData] = useState<Todo[]>(JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]'))

useEffect(() => {
localStorage.setItem(STORAGE_KEY, JSON.stringify(data))
}, [data])

return (
<div className="w-[600px]">
<Header />
<AddToDo addItem={setData} />
<ToDoList
data={data}
updateData={setData}
/>
</div>
)
}

export default App
1 change: 1 addition & 0 deletions members/WenHaoHub/task1/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions members/WenHaoHub/task1/src/componets/AddToDo.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.toggle-all {
width: 1px;
height: 1px;
border: none;
opacity: 0;
}

.toggle-all + label {
display: flex;
align-items: center;
justify-content: center;
width: 45px;
height: 65px;
font-size: 0;
}

.toggle-all + label:before {
content: '❯';
display: inline-block;
font-size: 22px;
color: #949494;
padding: 10px 27px 10px 27px;
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
}
69 changes: 69 additions & 0 deletions members/WenHaoHub/task1/src/componets/AddToDo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { useCallback, useState } from 'react'
import { Todo } from '../App'
import './AddToDo.css'

function AddToDo({ addItem }: { addItem: React.Dispatch<React.SetStateAction<Todo[]>> }) {
const [text, setText] = useState('')
const [isSelect, setSelect] = useState(false);

const handleChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
setText(e.target.value)
}, [])

const handleAdd =
(e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key !== 'Enter') return
if (text.trim() === '') return //原生方法
//写函数 表示前一次的state,
addItem((prev) => {
console.log('>>>prev', prev);
return [...prev, { id: Date.now().toString(), text, completed: false }]
})
setText('')
}



const selectAll = (e: React.ChangeEvent<HTMLInputElement>) => {
addItem((prev) => {
const isAllCompleted = prev.every((todo) => todo.completed)
if (e.target.checked === false && isAllCompleted) {
setSelect(e.target.checked)
return prev.map((todo) => ({ ...todo, completed: !isAllCompleted }))
} else if (e.target.checked === false && !isAllCompleted) {
return prev.map((todo) => ({ ...todo, completed: !isAllCompleted }))
} else if (e.target.checked === true && isAllCompleted) {
setSelect(e.target.checked)
return prev.map((todo) => ({ ...todo, completed: true }))

} else {
setSelect(e.target.checked)
return prev.map((todo) => ({ ...todo, completed: !isAllCompleted }))
}
})
}

return (
<div className="todo rounded border-2 flex items-center">
<label className="ml-4 mr-2">
<input
type="checkbox"
checked={isSelect}
onChange={selectAll}
/>
{/* Dark mode */}
</label>

<input
className="px-4 grow"
placeholder="添加代办事项"
type="text"
value={text}
onChange={handleChange}
onKeyUp={handleAdd}
/>
</div>
)
}

export default AddToDo
9 changes: 9 additions & 0 deletions members/WenHaoHub/task1/src/componets/Header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function Header() {
return (
<header className="m-6">
<h1 >TodoList</h1>
</header>
)
}

export default Header
29 changes: 29 additions & 0 deletions members/WenHaoHub/task1/src/componets/ToDoItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { clsx } from 'clsx'
import { Todo } from '../App'

type ToDoItemProps = {
todo: Todo
onToggle: (id: string) => void
removeItem: (id: string) => void
}

function ToDoItem({ todo, onToggle, removeItem }: ToDoItemProps) {
console.log('>>>ToDoItem',);
return (
<div className="todo text-2xl items-center border-b-2 todo-item">
<input
className="w-[45px] h-[20px]"
type="checkbox"
checked={todo.completed}
onChange={() => onToggle(todo.id)}
/>
<span className={clsx('grow text-left px-4', todo.completed && 'completed')}>{todo.text}</span>
<button
className="destroy mr-5"
onClick={() => removeItem(todo.id)}
></button>
</div>
)
}

export default ToDoItem
Loading

0 comments on commit 9b54ff9

Please sign in to comment.