Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task1: NeilYeTAT #1416

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions members/NeilYeTAT/task1/React To-Do-List/.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?
50 changes: 50 additions & 0 deletions members/NeilYeTAT/task1/React To-Do-List/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# React + TypeScript + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:

- Configure the top-level `parserOptions` property like this:

```js
export default tseslint.config({
languageOptions: {
// other options...
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
},
})
```

- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked`
- Optionally add `...tseslint.configs.stylisticTypeChecked`
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and update the config:

```js
// eslint.config.js
import react from 'eslint-plugin-react'

export default tseslint.config({
// Set the react version
settings: { react: { version: '18.3' } },
plugins: {
// Add the react plugin
react,
},
rules: {
// other rules...
// Enable its recommended rules
...react.configs.recommended.rules,
...react.configs['jsx-runtime'].rules,
},
})
```
28 changes: 28 additions & 0 deletions members/NeilYeTAT/task1/React To-Do-List/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'

export default tseslint.config(
{ ignores: ['dist'] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
)
13 changes: 13 additions & 0 deletions members/NeilYeTAT/task1/React To-Do-List/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!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>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
34 changes: 34 additions & 0 deletions members/NeilYeTAT/task1/React To-Do-List/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "react-to-do-list",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"clsx": "^2.1.1",
"nanoid": "^5.0.7",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@eslint/js": "^9.11.1",
"@types/react": "^18.3.10",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.2",
"autoprefixer": "^10.4.20",
"eslint": "^9.11.1",
"eslint-plugin-react-hooks": "^5.1.0-rc.0",
"eslint-plugin-react-refresh": "^0.4.12",
"globals": "^15.9.0",
"postcss": "^8.4.47",
"tailwindcss": "^3.4.14",
"typescript": "^5.5.3",
"typescript-eslint": "^8.7.0",
"vite": "^5.4.8"
}
}
6 changes: 6 additions & 0 deletions members/NeilYeTAT/task1/React To-Do-List/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/NeilYeTAT/task1/React To-Do-List/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.
30 changes: 30 additions & 0 deletions members/NeilYeTAT/task1/React To-Do-List/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// import { useEffect, useState } from "react";
import { useState } from "react";
import Header from "./components/Header";
import ToDoList from "./components/ToDoList";

function App() {
const [refresh, setRefresh] = useState(0);

const handleClearStorage = () => {
localStorage.clear();
setRefresh((refresh) => refresh + 1);
};

return (
<div className="h-screen flex flex-col w-screen bg-pink-200 items-center justify-center py-32 gap-4">
<button
onClick={handleClearStorage}
className="w-2/3 h-10 bg-pink-600 text-3xl text-white font-mono rounded-lg hover:bg-pink-500 duration-150"
>
点我清除localStorage缓存喵~
</button>
<div className="h-full m-auto flex flex-col w-2/3 bg-blue-300 items-center gap-4 rounded-lg py-4 overflow-y-scroll">
<Header></Header>
<ToDoList key={refresh}></ToDoList>
</div>
</div>
);
}

export default App;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { nanoid } from "nanoid";
import { Dispatch, SetStateAction, useRef } from "react";
import { ITodoType } from "../type/ITodoType";

export default function AddToDo({
todoList,
setTodoList,
}: {
todoList: ITodoType[];
setTodoList: Dispatch<SetStateAction<ITodoType[]>>;
}) {
const inputRef = useRef<HTMLInputElement>(null);

const handleAddTodo = () => {
const todo = inputRef.current?.value.trim();
if (todo) {
const newTodoList: ITodoType[] = [
...todoList,
{ todo, isFinish: false, id: nanoid() },
];
setTodoList(newTodoList);
localStorage.setItem("todo", JSON.stringify(newTodoList));
} else {
inputRef.current?.focus();
}
if (inputRef.current) {
inputRef.current.value = "";
}
};

const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter") {
handleAddTodo();
}
};

return (
<>
<label className="w-full h-12 flex gap-4">
<input
type="text"
className="w-2/3 h-full text-2xl font-mono px-4 rounded-lg"
placeholder="回车就可以添加喵~"
ref={inputRef}
onKeyDown={handleKeyDown}
/>
<button
className="w-1/3 text-2xl bg-green-500 hover:bg-green-400 duration-150 text-white rounded-lg"
onClick={handleAddTodo}
>
Add a Todo
</button>
</label>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Header() {
return <h1 className="text-5xl font-mono font-bold">Todo List Header</h1>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import clsx from "clsx";
import type { ITodoType } from "../type/ITodoType";

export default function ToDoItem({
todoItem,
toggleFinishState,
removeTodo,
}: {
todoItem: ITodoType;
toggleFinishState: (id: string) => void;
removeTodo: (id: string) => void;
}) {
return (
<div className="bg-indigo-100 flex items-center py-2 text-2xl font-mono justify-between px-4 rounded-lg">
<label
htmlFor={todoItem.id}
className={clsx("w-full cursor-pointer flex items-center gap-2")}
>
<input
id={todoItem.id}
type="checkbox"
className="h-5 w-5"
defaultChecked={todoItem.isFinish}
onClick={() => toggleFinishState(todoItem.id)}
/>
<span
className={clsx(todoItem.isFinish && "line-through text-red-500")}
>
{todoItem.todo}
</span>
</label>
<button
className="w-fit px-4 bg-pink-600 rounded-lg"
onClick={() => removeTodo(todoItem.id)}
>
Remove
</button>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useEffect, useState } from "react";
import AddToDo from "./AddToDo";
import TodoItem from "./ToDoItem";
import type { ITodoType } from "../type/ITodoType";

export default function ToDoList() {
const [todoList, setTodoList] = useState<ITodoType[]>(
JSON.parse(localStorage.getItem("todo")!) || []
);

useEffect(() => {
localStorage.setItem("todo", JSON.stringify(todoList));
}, [todoList]);

// 完成 和 未完成
const toggleFinishState = (id: string) => {
setTodoList(
todoList.map((todoItem) =>
todoItem.id === id
? { ...todoItem, isFinish: !todoItem.isFinish }
: todoItem
)
);
};
// 移出 todo
const removeTodo = (id: string) => {
setTodoList(todoList.filter((todoItem) => todoItem.id !== id));
};

return (
<div className="flex flex-col w-2/3 gap-4">
<AddToDo todoList={todoList} setTodoList={setTodoList}></AddToDo>

{todoList.map((todoItem) => (
<TodoItem
todoItem={todoItem}
key={todoItem.id}
toggleFinishState={toggleFinishState}
removeTodo={removeTodo}
/>
))}
</div>
);
}
3 changes: 3 additions & 0 deletions members/NeilYeTAT/task1/React To-Do-List/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
10 changes: 10 additions & 0 deletions members/NeilYeTAT/task1/React To-Do-List/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import "./index.css";
import App from "./App.tsx";

createRoot(document.getElementById("root")!).render(
// <StrictMode>
<App />
// </StrictMode>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface ITodoType {
id: string;
todo: string;
isFinish: true | false;
}
1 change: 1 addition & 0 deletions members/NeilYeTAT/task1/React To-Do-List/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
8 changes: 8 additions & 0 deletions members/NeilYeTAT/task1/React To-Do-List/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('tailwindcss').Config} */
export default {
content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
theme: {
extend: {},
},
plugins: [],
};
Loading