-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Make group fileds in the inspector accordions
- Loading branch information
1 parent
86ac604
commit 3d22e5d
Showing
4 changed files
with
55 additions
and
6 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,60 @@ | ||
import { useCallback, useRef } from "react"; | ||
import { useLocalStorageAdopter } from "../../hooks/localStorage"; | ||
import iconDropdown from "../../assets/icons/dropdown.svg"; | ||
|
||
interface Props { | ||
label: string; | ||
children?: React.ReactNode; | ||
accordionKey?: string; | ||
} | ||
|
||
export const BlockGroupField: React.FC<Props> = ({ label, children }) => { | ||
export const BlockGroupField: React.FC<Props> = ({ label, children, accordionKey }) => { | ||
const { state: accordionState, setState: setAccordingState } = useLocalStorageAdopter({ | ||
key: accordionKey, | ||
initialValue: false, | ||
version: "1", | ||
}); | ||
|
||
const contentRef = useRef<HTMLDivElement>(null); | ||
|
||
const handleAccordionClick = useCallback(() => { | ||
setAccordingState((v) => { | ||
const next = !v; | ||
if (next) { | ||
setTimeout(() => { | ||
contentRef.current?.scrollIntoView({ behavior: "smooth", block: "nearest", inline: "nearest" }); | ||
}, 0); | ||
} | ||
return next; | ||
}); | ||
}, [setAccordingState]); | ||
|
||
const content = ( | ||
<div ref={contentRef} className="ml-2 pl-2 border-l border-b border-gray-400 flex flex-col gap-1"> | ||
{children} | ||
</div> | ||
); | ||
|
||
if (!accordionKey) { | ||
return ( | ||
<div className="flex flex-col gap-1"> | ||
<span>{label}:</span> | ||
{content} | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<div className="flex flex-col gap-1"> | ||
<span>{label}:</span> | ||
<div className="ml-2 pl-2 border-l border-gray-400 flex flex-col gap-1">{children}</div> | ||
<div className={"flex flex-col gap-1 " + (accordionState ? "" : "border-b border-gray-400")}> | ||
<button | ||
type="button" | ||
className="flex items-center justify-between hover:bg-gray-200" | ||
onClick={handleAccordionClick} | ||
> | ||
{label}: | ||
<img src={iconDropdown} alt="" className={"ml-auto w-4 h-4 " + (accordionState ? "rotate-180" : "")} /> | ||
</button> | ||
{accordionState ? content : undefined} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters