Skip to content

Commit

Permalink
🚑 検索条件を変えても、セレクトボックスに値が残る場合がある
Browse files Browse the repository at this point in the history
  • Loading branch information
arrow2nd committed Nov 21, 2024
1 parent 08652cd commit ac19133
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions components/ui/form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { useRouter } from "next/navigation";
import { useRef } from "react";
import { useRouter, useSearchParams } from "next/navigation";
import { useEffect, useRef } from "react";
import { GroupBase, SelectInstance } from "react-select";
import { generateSearchQueryPath } from "libs/url";
import { Query } from "types/query";
Expand All @@ -17,6 +17,7 @@ export type FormProps = {

const Form = ({ query, idolOptions, clotheOptions }: FormProps) => {
const router = useRouter();
const searchParams = useSearchParams();
const idolSelectRef = useRef<SelectInstance>(null);
const clotheSelectRef = useRef<SelectInstance>(null);

Expand All @@ -32,6 +33,10 @@ const Form = ({ query, idolOptions, clotheOptions }: FormProps) => {

const pagePath = generateSearchQueryPath(type, q);
router.replace(pagePath);
};

useEffect(() => {
const type = searchParams.get("type") as Query["type"];

switch (type) {
case "idol":
Expand All @@ -42,8 +47,11 @@ const Form = ({ query, idolOptions, clotheOptions }: FormProps) => {
clotheSelectRef.current?.blur();
idolSelectRef.current?.clearValue();
break;
default:
idolSelectRef.current?.clearValue();
clotheSelectRef.current?.clearValue();
}
};
}, [searchParams]);

return (
<div className="mb-16 flex justify-center">
Expand Down

0 comments on commit ac19133

Please sign in to comment.