Skip to content

Commit

Permalink
Merge pull request #1396 from BigBear-JS/task1
Browse files Browse the repository at this point in the history
task1:BigBear-JS
  • Loading branch information
ourai authored Oct 2, 2024
2 parents b888682 + 9295e78 commit 9b1ca38
Show file tree
Hide file tree
Showing 25 changed files with 401 additions and 0 deletions.
24 changes: 24 additions & 0 deletions members/BigBear-JS/task1/TodoList/.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/BigBear-JS/task1/TodoList/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/BigBear-JS/task1/TodoList/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/BigBear-JS/task1/TodoList/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>
29 changes: 29 additions & 0 deletions members/BigBear-JS/task1/TodoList/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "todolist",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@eslint/js": "^9.9.0",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.1",
"eslint": "^9.9.0",
"eslint-plugin-react-hooks": "^5.1.0-rc.0",
"eslint-plugin-react-refresh": "^0.4.9",
"globals": "^15.9.0",
"typescript": "^5.5.3",
"typescript-eslint": "^8.0.1",
"vite": "^5.4.1"
}
}
1 change: 1 addition & 0 deletions members/BigBear-JS/task1/TodoList/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.
4 changes: 4 additions & 0 deletions members/BigBear-JS/task1/TodoList/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.todolist-container {
margin: auto;
width: 300px;
}
50 changes: 50 additions & 0 deletions members/BigBear-JS/task1/TodoList/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { useState, useEffect } from 'react'
import { Todo } from './types'
import Header from './components/Header'
import AddTodo from './components/AddTodo'
import TodoList from './components/TodoList'

import './App.css'

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

const addTodo = (text: string) => {
setTodos([
...todos,
{
id: Date.now(),
text: text,
completed: false,
}
])
}
const deleteTodo = (id: number) => {
setTodos(todos.filter(todo => todo.id !== id))
}
const toggleCompleted = (id: number) => {
setTodos(todos.map(todo => {
if (todo.id === id) {
return {
...todo,
completed: !todo.completed
}
}
return todo
}))
}

useEffect(() => {
localStorage.setItem('todos', JSON.stringify(todos))
}, [todos])

return (
<div className='todolist-container'>
<Header title={"Todo List"} />
<AddTodo onAdd={addTodo} />
<TodoList onDelete={deleteTodo} onToggle={toggleCompleted} todos={todos} />
</div>
)
}

export default App
1 change: 1 addition & 0 deletions members/BigBear-JS/task1/TodoList/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.
17 changes: 17 additions & 0 deletions members/BigBear-JS/task1/TodoList/src/components/AddTodo/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.addtodo {
padding: 5px 0;
display: flex;
justify-content: space-between;
height: 40px;
}
.addtodo input {
flex: 1;
padding: 0 5px;
box-sizing: border-box;
}
.addtodo button {
margin-left: 10px;
padding: 0 5px;
color: #fff;
background: #000;
}
29 changes: 29 additions & 0 deletions members/BigBear-JS/task1/TodoList/src/components/AddTodo/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useState } from 'react';
import './index.css'

interface AddTodoProps {
onAdd: (text: string) => void;
}
export default function AddTodo({ onAdd }: AddTodoProps) {
const [text, setText] = useState<string>('')
const addText = () => {
if (text.trim() === '') {
alert('Please enter a todo')
return
}
onAdd(text)
setText('')
}

return (
<div className="addtodo">
<input
type="text"
placeholder="Add Todo"
value={text}
onChange={e => setText(e.target.value)}
/>
<button onClick={addText}>Add</button>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.header {
text-align: center;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import './index.css'

interface HeaderProps {
title: string
}
export default function Header({ title }: HeaderProps) {
return <h1>{title}</h1>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.todoitem {
padding: 5px 0;
margin-bottom: 10px;
display: flex;
justify-content: space-between;
border-bottom: 2px solid #ccc;
}
.todoitem-text {
flex: 1;
margin-right: 10px;
white-space: wrap;
word-break: break-all;
}
.todoitem button {
padding: 5px;
color: #fff;
background: #000;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Todo } from "../../types";
import './index.css'

interface TodoItemProps {
todo: Todo;
onDelete: (id: number) => void;
onToggle: (id: number) => void;
}
export default function TodoItem({ todo, onDelete, onToggle }: TodoItemProps) {
return (
<li className="todoitem">
<div className="todoitem-text" style={{textDecoration: todo.completed ? 'line-through' : 'none'}}>{todo.text}</div>
<div>
<button onClick={() => onToggle(todo.id)}>toggle</button>
<button onClick={() => onDelete(todo.id)}>delete</button>
</div>
</li>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.todolist {
width: 100%;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Todo } from "../../types"
import TodoItem from "../TodoItem"
import './index.css'

interface TodoListProps {
todos: Todo[];
onToggle: (id: number) => void;
onDelete: (id: number) => void;
}
export default function TodoList({todos, onDelete, onToggle}: TodoListProps) {

return (
<ul className="todolist">
{
todos.map(todo => {
return <TodoItem key={todo.id} todo={todo} onToggle={onToggle} onDelete={onDelete} />
})
}
</ul>
)
}
7 changes: 7 additions & 0 deletions members/BigBear-JS/task1/TodoList/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
10 changes: 10 additions & 0 deletions members/BigBear-JS/task1/TodoList/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 App from './App.tsx'
import './index.css'

createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
</StrictMode>,
)
5 changes: 5 additions & 0 deletions members/BigBear-JS/task1/TodoList/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Todo {
id: number;
text: string;
completed: boolean;
}
1 change: 1 addition & 0 deletions members/BigBear-JS/task1/TodoList/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
24 changes: 24 additions & 0 deletions members/BigBear-JS/task1/TodoList/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"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
},
"include": ["src"]
}
Loading

0 comments on commit 9b1ca38

Please sign in to comment.