option-list-item.tsx 987 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import React, { type FC, useEffect, useRef } from 'react'
  2. import cn from '@/utils/classnames'
  3. type OptionListItemProps = {
  4. isSelected: boolean
  5. onClick: () => void
  6. } & React.LiHTMLAttributes<HTMLLIElement>
  7. const OptionListItem: FC<OptionListItemProps> = ({
  8. isSelected,
  9. onClick,
  10. children,
  11. }) => {
  12. const listItemRef = useRef<HTMLLIElement>(null)
  13. useEffect(() => {
  14. if (isSelected)
  15. listItemRef.current?.scrollIntoView({ behavior: 'instant' })
  16. }, [])
  17. return (
  18. <li
  19. ref={listItemRef}
  20. className={cn(
  21. 'px-1.5 py-1 rounded-md flex items-center justify-center text-components-button-ghost-text system-xs-medium cursor-pointer',
  22. isSelected ? 'bg-components-button-ghost-bg-hover' : 'hover:bg-components-button-ghost-bg-hover',
  23. )}
  24. onClick={() => {
  25. listItemRef.current?.scrollIntoView({ behavior: 'smooth' })
  26. onClick()
  27. }}
  28. >
  29. {children}
  30. </li>
  31. )
  32. }
  33. export default React.memo(OptionListItem)