Skip to content

Commit c4dbe62

Browse files
committed
Refactor
1 parent 3bfe73d commit c4dbe62

File tree

5 files changed

+80
-61
lines changed

5 files changed

+80
-61
lines changed

solidjs-tutorial/src/AddBook.tsx

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,46 @@
1-
export default function AddBook() {
1+
import { createSignal, JSX } from "solid-js";
2+
3+
import type { Setter } from "solid-js";
4+
import type { Book } from "./App";
5+
export interface IAddBookProps {
6+
setBooks: Setter<Book[]>;
7+
}
8+
9+
const emptyBook: Book = { title: "", author: "" };
10+
11+
export default function AddBook(props: IAddBookProps) {
12+
const [newBook, setNewBook] = createSignal(emptyBook);
13+
const addBook: JSX.EventHandler<HTMLButtonElement, MouseEvent> = (event) => {
14+
event.preventDefault();
15+
props.setBooks((books) => [...books, newBook()]);
16+
setNewBook(emptyBook);
17+
};
218
return (
3-
<form
4-
onsubmit={(e) => {
5-
e.preventDefault();
6-
}}
7-
>
19+
<form>
820
<div>
921
<label for="title">Book name</label>
10-
<input id="title" />
22+
<input
23+
id="title"
24+
value={newBook().title}
25+
onInput={(e) => {
26+
setNewBook({ ...newBook(), title: e.currentTarget.value });
27+
}}
28+
/>
1129
</div>
30+
<br />
1231
<div>
1332
<label for="author">Author</label>
14-
<input id="author" />
33+
<input
34+
id="author"
35+
value={newBook().author}
36+
onInput={(e) => {
37+
setNewBook({ ...newBook(), author: e.currentTarget.value });
38+
}}
39+
/>
1540
</div>
16-
<button type="submit">Add book</button>
41+
<button type="submit" onClick={addBook}>
42+
Add book
43+
</button>
1744
</form>
1845
);
1946
}

solidjs-tutorial/src/App.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import type { Component } from "solid-js";
22
import Bookshelf from "./Bookshelf";
33

4+
export type Book = {
5+
title: string;
6+
author: string;
7+
};
8+
49
const App: Component = () => {
510
return (
611
<>
712
<Bookshelf name={`Solid`} />
8-
{/* <Counter /> */}
913
</>
1014
);
1115
};

solidjs-tutorial/src/BookList.tsx

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,28 @@
1-
import { createSignal, For } from "solid-js";
1+
import { For } from "solid-js";
22

3-
type Book = {
4-
title: string;
5-
author: string;
6-
};
3+
import type { Book } from "./App";
74

8-
const initialBooks: Array<Book> = [
9-
{ title: "Code Complete", author: "Steve McConnell" },
10-
{ title: "The Hobbit", author: "J.R.R. Tolkien" },
11-
{ title: "Living a Feminist Life", author: "Sarah Ahmed" },
12-
];
13-
14-
export default function () {
15-
const [books, setBooks] = createSignal(initialBooks);
5+
interface IBookListProps {
6+
books: Book[];
7+
}
168

17-
const totalBooks = books().length;
9+
export default function BookList(props: IBookListProps) {
10+
const totalBooks = () => props.books.length;
1811

1912
return (
2013
<>
21-
<h2>My Books ({totalBooks})</h2>
14+
<h2>My books ({totalBooks()})</h2>
2215
<ul>
23-
{/* SolidJS Way */}
24-
<For each={books()}>
25-
{(book) => (
26-
<li>
27-
{`${book.title} `}
28-
<span style={{ "font-style": "italic" }}>{book.author}</span>
29-
</li>
30-
)}
16+
<For each={props.books}>
17+
{(book) => {
18+
return (
19+
<li>
20+
{book.title}{" "}
21+
<span style={{ "font-style": "italic" }}>({book.author})</span>
22+
</li>
23+
);
24+
}}
3125
</For>
32-
33-
{/* React Way */}
34-
{/* {books().map((book) => (
35-
<li>
36-
{`${book.title} `}
37-
<span style={{ "font-style": "italic" }}>{book.author}</span>
38-
</li>
39-
))} */}
4026
</ul>
4127
</>
4228
);

solidjs-tutorial/src/Bookshelf.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
1+
import { createSignal, Show } from "solid-js";
2+
13
import AddBook from "./AddBook";
24
import BookList from "./BookList";
35

6+
import type { Book } from "./App";
7+
48
interface IBookshelfProps {
59
name: string;
610
}
711

12+
const initialBooks: Book[] = [
13+
{ title: "Code Complete", author: "Steve McConnell" },
14+
{ title: "The Hobbit", author: "J.R.R. Tolkien" },
15+
{ title: "Living a Feminist Life", author: "Sarah Ahmed" },
16+
];
17+
818
export default function Bookshelf(props: IBookshelfProps) {
19+
const [books, setBooks] = createSignal(initialBooks);
20+
const [showForm, setShowForm] = createSignal(false);
21+
const toggleForm = () => setShowForm(!showForm());
22+
923
return (
1024
<div>
1125
<h1>{props.name}'s Bookshelf</h1>
12-
<BookList />
13-
<AddBook />
26+
<BookList books={books()} />
27+
<Show
28+
when={!showForm()}
29+
fallback={<button onClick={toggleForm}>Finished adding books</button>}
30+
>
31+
<AddBook setBooks={setBooks} />
32+
<button onClick={toggleForm}>Add a book</button>
33+
</Show>
1434
</div>
1535
);
1636
}

solidjs-tutorial/src/index.css

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,6 @@ body {
77
-moz-osx-font-smoothing: grayscale;
88
}
99

10-
div {
11-
margin: 0;
12-
padding: 0;
13-
14-
align-items: left;
15-
justify-content: left;
16-
17-
height: 100vh;
18-
width: 100vw;
19-
20-
background-color: #fafafa;
21-
22-
font-size: 1.1rem;
23-
color: #333;
24-
25-
overflow: hidden;
26-
}
27-
2810
span {
2911
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
3012
monospace;

0 commit comments

Comments
 (0)