deno.land / x / jotai@v1.8.4 / examples / todos_with_atomFamily / src / App.tsx

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import type { FormEvent } from 'react'import { CloseOutlined } from '@ant-design/icons'import { a, useTransition } from '@react-spring/web'import { Radio } from 'antd'import { Provider, atom, useAtom, useSetAtom } from 'jotai'import { atomFamily } from 'jotai/utils'import { nanoid } from 'nanoid'
type Param = { id: string; title?: string }const todoAtomFamily = atomFamily( (param: Param) => atom({ title: param.title || 'No title', completed: false }), (a: Param, b: Param) => a.id === b.id)
const filterAtom = atom('all')const todosAtom = atom<string[]>([])const filteredAtom = atom((get) => { const filter = get(filterAtom) const todos = get(todosAtom) if (filter === 'all') return todos else if (filter === 'completed') return todos.filter((id) => get(todoAtomFamily({ id })).completed) else return todos.filter((id) => !get(todoAtomFamily({ id })).completed)})
const TodoItem = ({ id, remove,}: { id: string remove: (id: string) => void}) => { const [item, setItem] = useAtom(todoAtomFamily({ id })) const toggleCompleted = () => setItem({ ...item, completed: !item.completed }) return ( <> <input type="checkbox" checked={item.completed} onChange={toggleCompleted} /> <span style={{ textDecoration: item.completed ? 'line-through' : '' }}> {item.title} </span> <CloseOutlined onClick={() => remove(id)} /> </> )}
const Filter = () => { const [filter, set] = useAtom(filterAtom) return ( <Radio.Group onChange={(e) => set(e.target.value)} value={filter}> <Radio value="all">All</Radio> <Radio value="completed">Completed</Radio> <Radio value="incompleted">Incompleted</Radio> </Radio.Group> )}
const Filtered = ({ remove }: { remove: (id: string) => void }) => { const [todos] = useAtom(filteredAtom) const transitions = useTransition(todos, { keys: (id: string) => id, from: { opacity: 0, height: 0 }, enter: { opacity: 1, height: 40 }, leave: { opacity: 0, height: 0 }, }) return transitions((style, id) => ( <a.div className="item" style={style}> <TodoItem id={id} remove={remove} /> </a.div> ))}
const TodoList = () => { // Use `useSetAtom` to avoid re-render // const [, setTodos] = useAtom(todosAtom) const setTodos = useSetAtom(todosAtom) const remove = (id: string) => { setTodos((prev) => prev.filter((item) => item !== id)) todoAtomFamily.remove({ id }) } const add = (e: FormEvent<HTMLFormElement>) => { e.preventDefault() const title = e.currentTarget.inputTitle.value e.currentTarget.inputTitle.value = '' const id = nanoid() todoAtomFamily({ id, title }) setTodos((prev) => [...prev, id]) } return ( <form onSubmit={add}> <Filter /> <input name="inputTitle" placeholder="Type ..." /> <Filtered remove={remove} /> </form> )}
const serializeAtom = atom< null, | { type: 'serialize'; callback: (value: string) => void } | { type: 'deserialize'; value: string }>(null, (get, set, action) => { if (action.type === 'serialize') { const todos = get(todosAtom) const todoMap: Record<string, { title: string; completed: boolean }> = {} todos.forEach((id) => { todoMap[id] = get(todoAtomFamily({ id })) }) const obj = { todos, todoMap, filter: get(filterAtom), } action.callback(JSON.stringify(obj)) } else if (action.type === 'deserialize') { const obj = JSON.parse(action.value) // needs error handling and type checking set(filterAtom, obj.filter) obj.todos.forEach((id: string) => { const todo = obj.todoMap[id] set(todoAtomFamily({ id, ...todo }), todo) }) set(todosAtom, obj.todos) }})
const Persist = () => { const [, dispatch] = useAtom(serializeAtom) const save = () => { dispatch({ type: 'serialize', callback: (value) => { localStorage.setItem('serializedTodos', value) }, }) } const load = () => { const value = localStorage.getItem('serializedTodos') if (value) { dispatch({ type: 'deserialize', value }) } } return ( <div> <button onClick={save}>Save to localStorage</button> <button onClick={load}>Load from localStorage</button> </div> )}
export default function App() { return ( <Provider> <h1>Jōtai</h1> <TodoList /> <h3>Persist</h3> <Persist /> </Provider> )}
jotai

Version Info

Tagged at
2 years ago