Skip to content

Commit

Permalink
完善搜索功能
Browse files Browse the repository at this point in the history
  • Loading branch information
zoujia committed Jun 1, 2024
1 parent 6379afa commit 5c24f9f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 17 deletions.
14 changes: 14 additions & 0 deletions src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import Contact, {
import EditContact, {
action as editAction
} from './routes/edit';
import DestroyContact, {
destroy as destroyAction
} from './routes/destroy';
import Index from './routes';

const router = createBrowserRouter([
{
Expand All @@ -22,6 +26,10 @@ const router = createBrowserRouter([
loader: rootLoader,
action: rootAction,
children: [
{
index: true,
element: <Index />
},
{
path: 'contacts/:contactId',
element: <Contact />,
Expand All @@ -32,6 +40,12 @@ const router = createBrowserRouter([
element: <EditContact />,
loader: contactLoader,
action: editAction
},
{
path: 'contacts/:contactId/destroy',
element: <DestroyContact />,
action: destroyAction,
errorElement: <div>Oops! There was an error.</div>
}
]
}
Expand Down
9 changes: 0 additions & 9 deletions src/routes/contact.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ export async function loader({ params }) {
export default function Contact() {
const { contact } = useLoaderData();

// const contact = {
// first: "Your",
// last: "Name",
// avatar: "https://placekitten.com/g/200/200",
// twitter: "your_handle",
// notes: "Some notes",
// favorite: true,
// };

return (
<div id="contact">
<div>
Expand Down
12 changes: 12 additions & 0 deletions src/routes/destroy.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { redirect } from "react-router-dom";
import { deleteContact } from "../contacts";

export async function destroy({ params }) {
throw Error('Oh dang!');
// await deleteContact(params.contactId);
// return redirect('/');
}

export default function DestroyContact() {
return <></>;
}
8 changes: 6 additions & 2 deletions src/routes/edit.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useLoaderData, Form, redirect } from "react-router-dom";
import { useLoaderData, Form, redirect, useNavigate } from "react-router-dom";
import { updateContact } from "../contacts";

export async function action({ request, params }) {
Expand All @@ -11,6 +11,7 @@ export async function action({ request, params }) {

export default function EditContact() {
const { contact } = useLoaderData();
const navigate = useNavigate();

return (
<Form method="post" id="contact-form">
Expand Down Expand Up @@ -60,7 +61,10 @@ export default function EditContact() {
</label>
<p>
<button type="submit">Save</button>
<button type="cancel">Cancel</button>
<button
type="button"
onClick={() => navigate(-1)}
>Cancel</button>
</p>
</Form>
);
Expand Down
14 changes: 14 additions & 0 deletions src/routes/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

export default function Index() {
return (
<p id="zero-state">
This is a demo for React Router.
<br />
Check out {' '}
<a href="https://reactrouter.com">
the docs at reactrouter.com
</a>
.
</p>
);
}
21 changes: 15 additions & 6 deletions src/routes/root.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Outlet, useLoaderData, Form, redirect, NavLink } from "react-router-dom";
import { Outlet, useLoaderData, Form, redirect, NavLink, useNavigation } from "react-router-dom";
import { createContact, getContacts } from '../contacts';

export async function action() {
Expand All @@ -7,20 +7,24 @@ export async function action() {
return redirect(`/contacts/${contact.id}/edit`);
}

export async function loader () {
const contacts = await getContacts();
export async function loader ({ request }) {
const url = new URL(request.url);
const q = url.searchParams.get('q');

const contacts = await getContacts(q);
return { contacts };
}

export default function Root() {
const { contacts } = useLoaderData();
const navigation = useNavigation();

return (
<>
<div id="sidebar">
<h1>React Rooter Contacts</h1>
<div>
<form id="search-form" role="search">
<Form id="search-form" role="search">
<input
id="q"
aria-label="Search contacts"
Expand All @@ -37,7 +41,7 @@ export default function Root() {
className="sr-only"
aria-live="polite"
></div>
</form>
</Form>
<Form method="post">
<button type="submit">New</button>
</Form>
Expand Down Expand Up @@ -71,7 +75,12 @@ export default function Root() {
)}
</nav>
</div>
<div id="detail">
<div
id="detail"
className={
navigation.state === 'loading' ? 'loading' : ''
}
>
<Outlet />
</div>
</>
Expand Down

0 comments on commit 5c24f9f

Please sign in to comment.