index.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use client'
  2. import type { FC } from 'react'
  3. import Tooltip from '@/app/components/base/tooltip'
  4. import Slider from '@/app/components/base/slider'
  5. import Switch from '@/app/components/base/switch'
  6. type Props = {
  7. className?: string
  8. id: string
  9. name: string
  10. noTooltip?: boolean
  11. tip?: string
  12. value: number
  13. enable: boolean
  14. step?: number
  15. min?: number
  16. max: number
  17. onChange: (key: string, value: number) => void
  18. hasSwitch?: boolean
  19. onSwitchChange?: (key: string, enable: boolean) => void
  20. }
  21. const ParamItem: FC<Props> = ({ className, id, name, noTooltip, tip, step = 0.1, min = 0, max, value, enable, onChange, hasSwitch, onSwitchChange }) => {
  22. return (
  23. <div className={className}>
  24. <div className="flex items-center h-8 justify-between">
  25. <div className="flex items-center">
  26. {hasSwitch && (
  27. <Switch
  28. size='md'
  29. defaultValue={enable}
  30. onChange={async (val) => {
  31. onSwitchChange?.(id, val)
  32. }}
  33. />
  34. )}
  35. <span className="mx-1 text-gray-900 text-[13px] leading-[18px] font-medium">{name}</span>
  36. {!noTooltip && (
  37. <Tooltip
  38. triggerClassName='w-4 h-4 shrink-0'
  39. popupContent={<div className="w-[200px]">{tip}</div>}
  40. />
  41. )}
  42. </div>
  43. <div className="flex items-center"></div>
  44. </div>
  45. <div className="mt-2 flex items-center">
  46. <div className="mr-4 flex shrink-0 items-center">
  47. <input disabled={!enable} type="number" min={min} max={max} step={step} className="block w-[48px] h-7 text-xs leading-[18px] rounded-lg border-0 pl-1 pl py-1.5 bg-gray-50 text-gray-900 placeholder:text-gray-400 focus:ring-1 focus:ring-inset focus:ring-primary-600 disabled:opacity-60" value={(value === null || value === undefined) ? '' : value} onChange={(e) => {
  48. const value = parseFloat(e.target.value)
  49. if (value < min || value > max)
  50. return
  51. onChange(id, value)
  52. }} />
  53. </div>
  54. <div className="flex items-center h-7 grow">
  55. <Slider
  56. className='w-full'
  57. disabled={!enable}
  58. value={max < 5 ? value * 100 : value}
  59. min={min < 1 ? min * 100 : min}
  60. max={max < 5 ? max * 100 : max}
  61. onChange={value => onChange(id, value / (max < 5 ? 100 : 1))}
  62. />
  63. </div>
  64. </div>
  65. </div>
  66. )
  67. }
  68. export default ParamItem