You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

option-list-item.tsx 1.0KB

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