AddItemButton.jsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import React from "react";
  2. import { useTranslation } from "react-i18next";
  3. import ItemLibrary from "./ItemLibrary";
  4. import Touch from "./ui/Touch";
  5. import SidePanel from "./ui/SidePanel";
  6. const AddItemPanel = ({ itemLibraries, open, onClose }) => {
  7. const [tab, setTab] = React.useState(itemLibraries[0]?.key || "standard");
  8. React.useEffect(() => {
  9. setTab(itemLibraries[0]?.key || "standard");
  10. }, [itemLibraries]);
  11. return (
  12. <SidePanel open={open} onClose={onClose} position="right" width="33%">
  13. <nav className="tabs">
  14. {itemLibraries.map(({ name, key }) => (
  15. <a
  16. onClick={() => setTab(key)}
  17. className={tab === key ? "active" : ""}
  18. style={{ cursor: "pointer" }}
  19. key={key}
  20. >
  21. {name}
  22. </a>
  23. ))}
  24. </nav>
  25. <section className="content">
  26. {itemLibraries.map(({ key, items }) =>
  27. tab === key ? <ItemLibrary items={items} key={key} /> : null
  28. )}
  29. </section>
  30. </SidePanel>
  31. );
  32. };
  33. const AddItemButton = ({ itemLibraries }) => {
  34. const { t } = useTranslation();
  35. const [showAddPanel, setShowAddPanel] = React.useState(false);
  36. return (
  37. <>
  38. <Touch
  39. onClick={() => setShowAddPanel((prev) => !prev)}
  40. alt={t("Add item")}
  41. title={t("Add item")}
  42. label={t("Add")}
  43. icon={showAddPanel ? "cross" : "plus"}
  44. />
  45. <AddItemPanel
  46. itemLibraries={itemLibraries}
  47. open={showAddPanel}
  48. onClose={() => setShowAddPanel(false)}
  49. />
  50. </>
  51. );
  52. };
  53. export default AddItemButton;