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:taojiangcb #1421

Open
wants to merge 3 commits 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/taojiangcb/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?
50 changes: 50 additions & 0 deletions members/taojiangcb/task1/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/taojiangcb/task1/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/taojiangcb/task1/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/taojiangcb/task1/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
1 change: 1 addition & 0 deletions members/taojiangcb/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.
51 changes: 51 additions & 0 deletions members/taojiangcb/task1/src/AddToDo.tsx
Original file line number Diff line number Diff line change
@@ -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: Props) => {
const { store, ...reset } = props;
const [relValue, setRelValue] = useState('');

const onTextChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
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 <React.Fragment>
<Input
sx={{ ml: 1, flex: 1 }}
placeholder="请输入待办事项"
inputProps={{ 'aria-label': 'search google maps' }}
onChange={onTextChange}
value={relValue}
onKeyDown={handleKeyPress} // 监听键盘按下事件
{...reset}
/>
<IconButton onClick={onAddTodo} type="button" sx={{ p: '10px' }} aria-label="search">
<AddCircleIcon />
</IconButton>
</React.Fragment>
}

export default withStore(AddToDo);
42 changes: 42 additions & 0 deletions members/taojiangcb/task1/src/App.css
Original file line number Diff line number Diff line change
@@ -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;
}
27 changes: 27 additions & 0 deletions members/taojiangcb/task1/src/App.tsx
Original file line number Diff line number Diff line change
@@ -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 <div>
<TodoStore.Provider value={store}>
<Card sx={{ minWidth: 275 }}>
<CardHeader title={<React.Fragment>
<Header title="TODO List" />
<AddToDo />
</React.Fragment>} />
<CardContent>
<ToDoList />
</CardContent>
</Card>
</TodoStore.Provider>
</div>
}

export default App;
10 changes: 10 additions & 0 deletions members/taojiangcb/task1/src/Header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

interface Props {
title: string;
}

const Header = ({ title }: Props) => {
return <h4>{title}</h4>
}

export default Header;
43 changes: 43 additions & 0 deletions members/taojiangcb/task1/src/ToDoItem.tsx
Original file line number Diff line number Diff line change
@@ -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 = <IconButton edge="end" onClick={() => props.onDelete(data)} aria-label="comments">
<DeleteOutlineOutlined />
</IconButton>

return <div style={{ textDecoration: data.done ? 'line-through' : 'none', backgroundColor: data.done ? '#eee' : 'white' }}>
<ListItem
key={data.id}
secondaryAction={ui_del_btn}
disablePadding={true}
dense={true}
>
<ListItemButton role={undefined} onClick={handleToggle} dense>
<ListItemIcon>
<Checkbox
edge="start"
checked={data.done}
tabIndex={-1}
disableRipple
inputProps={{ 'aria-labelledby': data.id }} />
</ListItemIcon>
<ListItemText primary={data.text} />
</ListItemButton>
</ListItem>
</div>
}

export default TodoItem
54 changes: 54 additions & 0 deletions members/taojiangcb/task1/src/ToDoList.tsx
Original file line number Diff line number Diff line change
@@ -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: 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(<TodoItem data={store.data[i]} onCheckChange={onCheckChange} onDelete={onDelete} />)
}
return ans;
}, [store.data])

return <div className={styles.scrollContent}>
<List sx={{ width: '100%', maxWidth: 360, height: 300, bgcolor: 'background.paper' }}>
{ui_item_controller}
</List>
</div>
}

export default withStore(ToDoList);
1 change: 1 addition & 0 deletions members/taojiangcb/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.
Loading