top-k-item.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import ParamItem from '.'
  6. type Props = {
  7. className?: string
  8. value: number
  9. onChange: (key: string, value: number) => void
  10. enable: boolean
  11. }
  12. const VALUE_LIMIT = {
  13. default: 2,
  14. step: 1,
  15. min: 1,
  16. max: 10,
  17. }
  18. const key = 'top_k'
  19. const TopKItem: FC<Props> = ({
  20. className,
  21. value,
  22. enable,
  23. onChange,
  24. }) => {
  25. const { t } = useTranslation()
  26. const handleParamChange = (key: string, value: number) => {
  27. let notOutRangeValue = parseFloat(value.toFixed(2))
  28. notOutRangeValue = Math.max(VALUE_LIMIT.min, notOutRangeValue)
  29. notOutRangeValue = Math.min(VALUE_LIMIT.max, notOutRangeValue)
  30. onChange(key, notOutRangeValue)
  31. }
  32. return (
  33. <ParamItem
  34. className={className}
  35. id={key}
  36. name={t(`appDebug.datasetConfig.${key}`)}
  37. tip={t(`appDebug.datasetConfig.${key}Tip`) as string}
  38. {...VALUE_LIMIT}
  39. value={value}
  40. enable={enable}
  41. onChange={handleParamChange}
  42. />
  43. )
  44. }
  45. export default React.memo(TopKItem)