param-item.tsx 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import Tooltip from '@/app/components/base/tooltip'
  6. import Slider from '@/app/components/base/slider'
  7. import TagInput from '@/app/components/base/tag-input'
  8. export const getFitPrecisionValue = (num: number, precision: number | null) => {
  9. if (!precision || !(`${num}`).includes('.'))
  10. return num
  11. const currNumPrecision = (`${num}`).split('.')[1].length
  12. if (currNumPrecision > precision)
  13. return parseFloat(num.toFixed(precision))
  14. return num
  15. }
  16. export type IParamIteProps = {
  17. id: string
  18. name: string
  19. tip: string
  20. value: number | string[]
  21. step?: number
  22. min?: number
  23. max: number
  24. precision: number | null
  25. onChange: (key: string, value: number | string[]) => void
  26. inputType?: 'inputTag' | 'slider'
  27. }
  28. const TIMES_TEMPLATE = '1000000000000'
  29. const ParamItem: FC<IParamIteProps> = ({ id, name, tip, step = 0.1, min = 0, max, precision, value, inputType, onChange }) => {
  30. const { t } = useTranslation()
  31. const getToIntTimes = (num: number) => {
  32. if (precision)
  33. return parseInt(TIMES_TEMPLATE.slice(0, precision + 1), 10)
  34. if (num < 5)
  35. return 10
  36. return 1
  37. }
  38. const times = getToIntTimes(max)
  39. useEffect(() => {
  40. if (precision)
  41. onChange(id, getFitPrecisionValue(value, precision))
  42. }, [value, precision])
  43. return (
  44. <div className="flex items-center justify-between flex-wrap gap-y-2">
  45. <div className="flex flex-col flex-shrink-0">
  46. <div className="flex items-center">
  47. <span className="mr-[6px] text-gray-500 text-[13px] font-medium">{name}</span>
  48. {/* Give tooltip different tip to avoiding hide bug */}
  49. <Tooltip htmlContent={<div className="w-[200px] whitespace-pre-wrap">{tip}</div>} position='top' selector={`param-name-tooltip-${id}`}>
  50. <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
  51. <path d="M8.66667 10.6667H8V8H7.33333M8 5.33333H8.00667M14 8C14 8.78793 13.8448 9.56815 13.5433 10.2961C13.2417 11.0241 12.7998 11.6855 12.2426 12.2426C11.6855 12.7998 11.0241 13.2417 10.2961 13.5433C9.56815 13.8448 8.78793 14 8 14C7.21207 14 6.43185 13.8448 5.7039 13.5433C4.97595 13.2417 4.31451 12.7998 3.75736 12.2426C3.20021 11.6855 2.75825 11.0241 2.45672 10.2961C2.15519 9.56815 2 8.78793 2 8C2 6.4087 2.63214 4.88258 3.75736 3.75736C4.88258 2.63214 6.4087 2 8 2C9.5913 2 11.1174 2.63214 12.2426 3.75736C13.3679 4.88258 14 6.4087 14 8Z" stroke="#9CA3AF" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
  52. </svg>
  53. </Tooltip>
  54. </div>
  55. {inputType === 'inputTag' && <div className="text-gray-400 text-xs font-normal">{t('common.model.params.stop_sequencesPlaceholder')}</div>}
  56. </div>
  57. <div className="flex items-center">
  58. {inputType === 'inputTag'
  59. ? <TagInput
  60. items={(value ?? []) as string[]}
  61. onChange={newSequences => onChange(id, newSequences)}
  62. customizedConfirmKey='Tab'
  63. />
  64. : (
  65. <>
  66. <div className="mr-4 w-[120px]">
  67. <Slider value={value * times} min={min * times} max={max * times} onChange={(value) => {
  68. onChange(id, value / times)
  69. }} />
  70. </div>
  71. <input type="number" min={min} max={max} step={step} className="block w-[64px] h-9 leading-9 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" value={value} onChange={(e) => {
  72. let value = getFitPrecisionValue(isNaN(parseFloat(e.target.value)) ? min : parseFloat(e.target.value), precision)
  73. if (value < min)
  74. value = min
  75. if (value > max)
  76. value = max
  77. onChange(id, value)
  78. }} />
  79. </>
  80. )
  81. }
  82. </div>
  83. </div>
  84. )
  85. }
  86. export default React.memo(ParamItem)