index.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. popupContent={<div className="w-[200px]">{tip}</div>}
  39. />
  40. )}
  41. </div>
  42. <div className="flex items-center"></div>
  43. </div>
  44. <div className="mt-2 flex items-center">
  45. <div className="mr-4 flex shrink-0 items-center">
  46. <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) => {
  47. const value = parseFloat(e.target.value)
  48. if (value < min || value > max)
  49. return
  50. onChange(id, value)
  51. }} />
  52. </div>
  53. <div className="flex items-center h-7 grow">
  54. <Slider
  55. className='w-full'
  56. disabled={!enable}
  57. value={max < 5 ? value * 100 : value}
  58. min={min < 1 ? min * 100 : min}
  59. max={max < 5 ? max * 100 : max}
  60. onChange={value => onChange(id, value / (max < 5 ? 100 : 1))}
  61. />
  62. </div>
  63. </div>
  64. </div>
  65. )
  66. }
  67. export default ParamItem