Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. import React, { useContext, useState } from 'react'
  2. import PropTypes from 'prop-types'
  3. import style from './SwitcherEditDeck.scss'
  4. import { MobileContext } from '../container/App'
  5.  
  6. const SwitcherEditDeck = ({ btns, onClick, value }) => {
  7. const isMobile = useContext(MobileContext)
  8. const [tooltipVisible, setTooltipVisible] = useState(btns.reduce((acc, btn) => ({ ...acc, [btn.id]: false }), {}))
  9. const toogleVisible = (key, action) => {
  10. const res = Object.keys(tooltipVisible).reduce((acc, item) => {
  11. switch (action) {
  12. case 'show': {
  13. return item === key ? { ...acc, [item]: true } : { ...acc, [item]: false }
  14. }
  15. case 'hide': {
  16. return { ...acc, [item]: false }
  17. }
  18. default:
  19. break
  20. }
  21. }, {})
  22. return res
  23. }
  24. return <div className={style.wrap_all}>
  25. {btns.map(btn => {
  26. const isValue = btn.id === 'off' ? value === 'none' : btn.id === value
  27. return <div
  28. key={btn.id}
  29. className={style.wrap}
  30. >
  31. <div
  32. aria-checked={isValue}
  33. className={isValue ? style[`select_${btn.id}`] : style[`main_${btn.id}`]}
  34. role={'checkbox'}
  35. tabIndex={0}
  36. onClick={() => onClick(btn.id)}
  37. onKeyDown={(e) => e.key === 'Enter' && onClick(btn.id)}
  38. onMouseEnter={() => setTooltipVisible(toogleVisible(btn.id, 'show'))}
  39. onMouseLeave={() => setTooltipVisible(toogleVisible(btn.id, 'hide'))}
  40. />
  41. {(!isMobile && tooltipVisible[btn.id]) && (<span
  42. className={style.tooltip}
  43. style={{ 'width': `${btn.width}px` }}
  44. >{btn.label}</span>)}
  45. </div>
  46. })}
  47. </div>
  48. }
  49.  
  50. SwitcherEditDeck.propTypes = {
  51. btns: PropTypes.arrayOf(
  52. PropTypes.shape({
  53. id: PropTypes.string,
  54. label: PropTypes.string,
  55. width: PropTypes.string
  56. })
  57. ),
  58. value: PropTypes.string,
  59. onClick: PropTypes.func()
  60. }
  61.  
  62. export default React.memo(SwitcherEditDeck)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement