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.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. // eslint-disable-next-line react-hooks/exhaustive-deps
  19. }, [])
  20. return (
  21. <li
  22. ref={listItemRef}
  23. className={cn(
  24. 'system-xs-medium flex cursor-pointer items-center justify-center rounded-md px-1.5 py-1 text-components-button-ghost-text',
  25. isSelected ? 'bg-components-button-ghost-bg-hover' : 'hover:bg-components-button-ghost-bg-hover',
  26. )}
  27. onClick={() => {
  28. listItemRef.current?.scrollIntoView({ behavior: 'smooth' })
  29. onClick()
  30. }}
  31. >
  32. {children}
  33. </li>
  34. )
  35. }
  36. export default React.memo(OptionListItem)