diff --git a/members/taojiangcb/task1/.gitignore b/members/taojiangcb/task1/.gitignore new file mode 100644 index 000000000..a547bf36d --- /dev/null +++ b/members/taojiangcb/task1/.gitignore @@ -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? diff --git a/members/taojiangcb/task1/README.md b/members/taojiangcb/task1/README.md new file mode 100644 index 000000000..74872fd4a --- /dev/null +++ b/members/taojiangcb/task1/README.md @@ -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, + }, +}) +``` diff --git a/members/taojiangcb/task1/eslint.config.js b/members/taojiangcb/task1/eslint.config.js new file mode 100644 index 000000000..092408a9f --- /dev/null +++ b/members/taojiangcb/task1/eslint.config.js @@ -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 }, + ], + }, + }, +) diff --git a/members/taojiangcb/task1/index.html b/members/taojiangcb/task1/index.html new file mode 100644 index 000000000..e4b78eae1 --- /dev/null +++ b/members/taojiangcb/task1/index.html @@ -0,0 +1,13 @@ + + + + + + + Vite + React + TS + + +
+ + + diff --git a/members/taojiangcb/task1/package.json b/members/taojiangcb/task1/package.json new file mode 100644 index 000000000..7892a14c4 --- /dev/null +++ b/members/taojiangcb/task1/package.json @@ -0,0 +1,34 @@ +{ + "name": "task1", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "dev": "vite", + "build": "tsc -b && vite build", + "lint": "eslint .", + "preview": "vite preview" + }, + "dependencies": { + "@emotion/react": "^11.14.0", + "@emotion/styled": "^11.14.0", + "@mui/icons-material": "^5.15.19", + "@mui/material": "^5.15.19", + "@mui/styles": "^6.2.1", + "react": "^18.3.1", + "react-dom": "^18.3.1" + }, + "devDependencies": { + "@eslint/js": "^9.15.0", + "@types/react": "^18.3.12", + "@types/react-dom": "^18.3.1", + "@vitejs/plugin-react": "^4.3.4", + "eslint": "^9.15.0", + "eslint-plugin-react-hooks": "^5.0.0", + "eslint-plugin-react-refresh": "^0.4.14", + "globals": "^15.12.0", + "typescript": "~5.6.2", + "typescript-eslint": "^8.15.0", + "vite": "^6.0.1" + } +} diff --git a/members/taojiangcb/task1/public/vite.svg b/members/taojiangcb/task1/public/vite.svg new file mode 100644 index 000000000..e7b8dfb1b --- /dev/null +++ b/members/taojiangcb/task1/public/vite.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/members/taojiangcb/task1/src/AddToDo.tsx b/members/taojiangcb/task1/src/AddToDo.tsx new file mode 100644 index 000000000..02f284306 --- /dev/null +++ b/members/taojiangcb/task1/src/AddToDo.tsx @@ -0,0 +1,51 @@ +import React, { useCallback, useState } from 'react'; +import { withStore, WithStoreChildProps } from './store'; +import { IconButton, Input } from '@mui/material'; +import AddCircleIcon from '@mui/icons-material/AddCircle'; + +interface Props extends WithStoreChildProps { + defaultValue?: string; +} +const AddToDo: React.FC = (props: Props) => { + const { store, ...reset } = props; + const [relValue, setRelValue] = useState(''); + + const onTextChange = useCallback((e: React.ChangeEvent) => { + setRelValue(e.target.value); + }, []); + + const onAddTodo = useCallback(() => { + if (!relValue) return alert('请输入待办事项'); + const newData = { + id: Date.now().toString(), + text: relValue, + done: false + } + setRelValue(''); + store.setData([newData, ...store.data]); + }, [relValue]); + + const handleKeyPress = (event: React.KeyboardEvent) => { + if (event.key === 'Enter') { + // 处理 Enter 键提交表单 + onAddTodo(); + } + }; + + return + + + + + +} + +export default withStore(AddToDo); \ No newline at end of file diff --git a/members/taojiangcb/task1/src/App.css b/members/taojiangcb/task1/src/App.css new file mode 100644 index 000000000..b9d355df2 --- /dev/null +++ b/members/taojiangcb/task1/src/App.css @@ -0,0 +1,42 @@ +#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; +} diff --git a/members/taojiangcb/task1/src/App.tsx b/members/taojiangcb/task1/src/App.tsx new file mode 100644 index 000000000..992b46647 --- /dev/null +++ b/members/taojiangcb/task1/src/App.tsx @@ -0,0 +1,27 @@ +import React from 'react' +import './App.css' +import Header from './Header' +import TodoStore, { useCreateStore } from './store'; +import AddToDo from './AddToDo'; +import ToDoList from './ToDoList'; +import { Card, CardContent, CardHeader } from '@mui/material'; + +function App() { + // 轻 store 初始化 + const store = useCreateStore(); + return
+ + + +
+ + } /> + + + + + +
+} + +export default App; diff --git a/members/taojiangcb/task1/src/Header.tsx b/members/taojiangcb/task1/src/Header.tsx new file mode 100644 index 000000000..58ea970cf --- /dev/null +++ b/members/taojiangcb/task1/src/Header.tsx @@ -0,0 +1,10 @@ + +interface Props { + title: string; +} + +const Header = ({ title }: Props) => { + return

{title}

+} + +export default Header; \ No newline at end of file diff --git a/members/taojiangcb/task1/src/ToDoItem.tsx b/members/taojiangcb/task1/src/ToDoItem.tsx new file mode 100644 index 000000000..42216b92e --- /dev/null +++ b/members/taojiangcb/task1/src/ToDoItem.tsx @@ -0,0 +1,43 @@ +import { Todo } from "./store" +import { Checkbox, IconButton, ListItem, ListItemButton, ListItemIcon, ListItemText } from '@mui/material'; +import { DeleteOutlineOutlined } from '@mui/icons-material'; + +interface Props { + data: Todo, + onCheckChange: (todo: Todo, check: boolean) => void, + onDelete: (todo: Todo) => void +} + +const TodoItem = (props: Props) => { + const { data, onCheckChange } = props; + const handleToggle = () => { + onCheckChange && onCheckChange(data, !data.done) + }; + + const ui_del_btn = props.onDelete(data)} aria-label="comments"> + + + + return
+ + + + + + + + +
+} + +export default TodoItem \ No newline at end of file diff --git a/members/taojiangcb/task1/src/ToDoList.tsx b/members/taojiangcb/task1/src/ToDoList.tsx new file mode 100644 index 000000000..b0ab0280c --- /dev/null +++ b/members/taojiangcb/task1/src/ToDoList.tsx @@ -0,0 +1,54 @@ +import { FC, useMemo } from 'react'; +import { Todo, withStore, WithStoreChildProps } from './store'; +import TodoItem from './ToDoItem'; +import { List, Theme } from '@mui/material'; +import { makeStyles } from '@mui/styles'; + +interface Props extends WithStoreChildProps { } + +const useStyles = makeStyles((theme: Theme) => { + return { + scrollContent: { + overflow: 'hidden', + overflowY: 'auto', + maxHeight: '300px', + backgroundColor: '#fafafa', + marginBottom: 10, + } + } +}); + +const ToDoList: FC = (props: Props) => { + const styles = useStyles(); + const { store } = props; + + const ui_item_controller = useMemo(() => { + const onCheckChange = (todo: Todo, check: boolean) => { + /** 懒的写替换方法了,先直接修改对象 */ + todo.done = check; + store.setData([...store.data]) + } + + const onDelete = (todo: Todo) => { + const res = confirm('确定删除?'); + if (res) { + const datas = store.data.filter(item => item.id !== todo.id); + store.setData(datas); + } + } + + const ans = []; + for (let i = 0; i < store.data.length; i++) { + ans.push() + } + return ans; + }, [store.data]) + + return
+ + {ui_item_controller} + +
+} + +export default withStore(ToDoList); \ No newline at end of file diff --git a/members/taojiangcb/task1/src/assets/react.svg b/members/taojiangcb/task1/src/assets/react.svg new file mode 100644 index 000000000..6c87de9bb --- /dev/null +++ b/members/taojiangcb/task1/src/assets/react.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/members/taojiangcb/task1/src/index.css b/members/taojiangcb/task1/src/index.css new file mode 100644 index 000000000..3135cd52b --- /dev/null +++ b/members/taojiangcb/task1/src/index.css @@ -0,0 +1,68 @@ +:root { + font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; + line-height: 1.5; + font-weight: 400; + + color-scheme: light dark; + color: rgba(255, 255, 255, 0.87); + background-color: #242424; + + font-synthesis: none; + text-rendering: optimizeLegibility; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +a { + font-weight: 500; + color: #646cff; + text-decoration: inherit; +} +a:hover { + color: #535bf2; +} + +body { + margin: 0; + display: flex; + place-items: center; + min-width: 320px; + min-height: 100vh; +} + +h1 { + font-size: 3.2em; + line-height: 1.1; +} + +button { + border-radius: 8px; + border: 1px solid transparent; + padding: 0.6em 1.2em; + font-size: 1em; + font-weight: 500; + font-family: inherit; + background-color: #1a1a1a; + cursor: pointer; + transition: border-color 0.25s; +} +button:hover { + border-color: #646cff; +} +button:focus, +button:focus-visible { + outline: 4px auto -webkit-focus-ring-color; +} + +@media (prefers-color-scheme: dark) { + :root { + color: #213547; + background-color: #ffffff; + } + a:hover { + color: #747bff; + } + button { + background-color: #f9f9f9; + } +} diff --git a/members/taojiangcb/task1/src/main.tsx b/members/taojiangcb/task1/src/main.tsx new file mode 100644 index 000000000..bef5202a3 --- /dev/null +++ b/members/taojiangcb/task1/src/main.tsx @@ -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( + + + , +) diff --git a/members/taojiangcb/task1/src/store.ts b/members/taojiangcb/task1/src/store.ts new file mode 100644 index 000000000..0151498db --- /dev/null +++ b/members/taojiangcb/task1/src/store.ts @@ -0,0 +1,60 @@ +import React, { PropsWithChildren, useContext, useMemo } from "react"; +const LOCAL_STORE_KEY = "todo-list"; + +export interface Todo { + id: string; + text: string; + done: boolean; +} + +export type WithStoreChildProps = PropsWithChildren & { + store: ITodoStore +} + +export interface ITodoStore { + data: readonly Todo[], + setData: (data: Todo[]) => void +} + +/** + * 构建一个基本的 store 包括 data 和 setData 能力 + * @returns a store object with data and setData + */ +export const useCreateStore = (/** initialData */) => { + const [todos, setTodos] = React.useState(() => { + const json_str = localStorage.getItem(LOCAL_STORE_KEY); + const todos = JSON.parse(json_str || '[]'); + return todos; + }); + + const store = useMemo(() => { + function toSaveLocal(todos: Todo[]) { + localStorage.setItem(LOCAL_STORE_KEY, JSON.stringify(todos || [])); + } + + function setData(todos: Todo[]) { + setTodos(todos) // 更新数据 + toSaveLocal(todos); // 保存到本地 + } + + return { + data: Object.freeze(todos), // 冻结数据,只能通过 setData 更新数据 + setData + } + }, [todos]); + return store; +} + +export function withStore(Component: React.ComponentType) { + return function WithStore(props: any) { + const store = useContext(TodoStore); + return React.createElement(Component, { ...props, store }); + } +} + +const TodoStore = React.createContext({ + data: [], + setData: () => { } +}) + +export default TodoStore \ No newline at end of file diff --git a/members/taojiangcb/task1/src/vite-env.d.ts b/members/taojiangcb/task1/src/vite-env.d.ts new file mode 100644 index 000000000..11f02fe2a --- /dev/null +++ b/members/taojiangcb/task1/src/vite-env.d.ts @@ -0,0 +1 @@ +/// diff --git a/members/taojiangcb/task1/tsconfig.app.json b/members/taojiangcb/task1/tsconfig.app.json new file mode 100644 index 000000000..358ca9ba9 --- /dev/null +++ b/members/taojiangcb/task1/tsconfig.app.json @@ -0,0 +1,26 @@ +{ + "compilerOptions": { + "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo", + "target": "ES2020", + "useDefineForClassFields": true, + "lib": ["ES2020", "DOM", "DOM.Iterable"], + "module": "ESNext", + "skipLibCheck": true, + + /* Bundler mode */ + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "isolatedModules": true, + "moduleDetection": "force", + "noEmit": true, + "jsx": "react-jsx", + + /* Linting */ + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true, + "noUncheckedSideEffectImports": true + }, + "include": ["src"] +} diff --git a/members/taojiangcb/task1/tsconfig.json b/members/taojiangcb/task1/tsconfig.json new file mode 100644 index 000000000..1ffef600d --- /dev/null +++ b/members/taojiangcb/task1/tsconfig.json @@ -0,0 +1,7 @@ +{ + "files": [], + "references": [ + { "path": "./tsconfig.app.json" }, + { "path": "./tsconfig.node.json" } + ] +} diff --git a/members/taojiangcb/task1/tsconfig.node.json b/members/taojiangcb/task1/tsconfig.node.json new file mode 100644 index 000000000..db0becc8b --- /dev/null +++ b/members/taojiangcb/task1/tsconfig.node.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo", + "target": "ES2022", + "lib": ["ES2023"], + "module": "ESNext", + "skipLibCheck": true, + + /* Bundler mode */ + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "isolatedModules": true, + "moduleDetection": "force", + "noEmit": true, + + /* Linting */ + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true, + "noUncheckedSideEffectImports": true + }, + "include": ["vite.config.ts"] +} diff --git a/members/taojiangcb/task1/vite.config.ts b/members/taojiangcb/task1/vite.config.ts new file mode 100644 index 000000000..8b0f57b91 --- /dev/null +++ b/members/taojiangcb/task1/vite.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from 'vite' +import react from '@vitejs/plugin-react' + +// https://vite.dev/config/ +export default defineConfig({ + plugins: [react()], +})