UserConfig.jsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import React from "react";
  2. import { SketchPicker } from "react-color";
  3. import styled from "styled-components";
  4. import Modal from "../../ui/Modal";
  5. const UserColor = styled.div`
  6. background-color: ${({ color }) => color};
  7. width: 30px;
  8. height: 30px;
  9. margin: 2px;
  10. margin-right: 0.5em;
  11. border-radius: 100%;
  12. text-align: center;
  13. line-height: 30px;
  14. cursor: ${({ editable }) => (editable ? "pointer" : "default")};
  15. @media screen and (max-width: 640px) {
  16. & {
  17. font-size: 0.5em;
  18. width: 20px;
  19. height: 20px;
  20. line-height: 20px;
  21. }
  22. }
  23. `;
  24. const StyledInputName = styled.input`
  25. &:not([type="checkbox"]):not([type="radio"]):not([type="submit"]):not([type="color"]):not([type="button"]):not([type="reset"]) {
  26. width: 10em;
  27. }
  28. `;
  29. const emptyStyle = {};
  30. const emptyColors = [];
  31. const UserConfig = ({ user, setUser, editable, index }) => {
  32. const [name, setName] = React.useState(user.name);
  33. const [color, setColor] = React.useState(user.color);
  34. const [showDetails, setShowDetails] = React.useState(false);
  35. const handleChange = React.useCallback(
  36. (e) => {
  37. setName(e.target.value);
  38. setUser((prevUser) => ({ ...prevUser, name: e.target.value }));
  39. },
  40. [setUser]
  41. );
  42. const handleChangecolor = React.useCallback((newColor) => {
  43. setColor(newColor.hex);
  44. }, []);
  45. const handleChangecolorComplete = React.useCallback(
  46. (newColor) => {
  47. setColor(newColor.hex);
  48. setUser((prevUser) => ({ ...prevUser, color: newColor.hex }));
  49. },
  50. [setUser]
  51. );
  52. return (
  53. <>
  54. <UserColor
  55. color={user.color}
  56. editable={editable}
  57. onClick={() => editable && setShowDetails(true)}
  58. title={user.name}
  59. >
  60. {user.name ? user.name[0] : index}
  61. {editable ? "*" : ""}
  62. </UserColor>
  63. <Modal title={"User details"} show={showDetails} setShow={setShowDetails}>
  64. <label>Username</label>
  65. <StyledInputName value={name} onChange={handleChange} />
  66. <label>Color</label>
  67. <SketchPicker
  68. disableAlpha
  69. presetColors={emptyColors}
  70. color={color}
  71. onChange={handleChangecolor}
  72. onChangeComplete={handleChangecolorComplete}
  73. styles={emptyStyle}
  74. width={160}
  75. />
  76. </Modal>
  77. </>
  78. );
  79. };
  80. export default UserConfig;