NewItems.js 996 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import React from "react";
  2. import { useItems } from "../components/Board/Items";
  3. import { nanoid } from "nanoid";
  4. import { useTranslation } from "react-i18next";
  5. const itemTypes = ["rect", "round", "image", "note", "counter", "dice"];
  6. const itemTemplates = {
  7. round: {},
  8. rect: {},
  9. image: {},
  10. note: {},
  11. counter: {},
  12. dice: {},
  13. };
  14. const NewItem = ({ type }) => {
  15. const { t } = useTranslation();
  16. const { pushItem } = useItems();
  17. const onClickHandler = () => {
  18. const newItem = {
  19. ...itemTemplates[type],
  20. type,
  21. x: 200,
  22. y: 50,
  23. id: nanoid(),
  24. };
  25. pushItem(newItem);
  26. };
  27. return (
  28. <button style={{ cursor: "pointer" }} onClick={onClickHandler}>
  29. {t(type)}
  30. </button>
  31. );
  32. };
  33. const NewItems = () => {
  34. return (
  35. <ul style={{ textAlign: "left", listStyle: "none" }}>
  36. {itemTypes.map((type) => (
  37. <li key={type}>
  38. <NewItem type={type} />
  39. </li>
  40. ))}
  41. </ul>
  42. );
  43. };
  44. export default NewItems;