parameter-item.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import type { FC } from 'react'
  2. import { useEffect, useRef, useState } from 'react'
  3. import type { ModelParameterRule } from '../declarations'
  4. import { useLanguage } from '../hooks'
  5. import { isNullOrUndefined } from '../utils'
  6. import { HelpCircle } from '@/app/components/base/icons/src/vender/line/general'
  7. import Switch from '@/app/components/base/switch'
  8. import Tooltip from '@/app/components/base/tooltip'
  9. import Slider from '@/app/components/base/slider'
  10. import Radio from '@/app/components/base/radio'
  11. import { SimpleSelect } from '@/app/components/base/select'
  12. import TagInput from '@/app/components/base/tag-input'
  13. export type ParameterValue = number | string | string[] | boolean | undefined
  14. type ParameterItemProps = {
  15. parameterRule: ModelParameterRule
  16. value?: ParameterValue
  17. onChange?: (value: ParameterValue) => void
  18. className?: string
  19. onSwitch?: (checked: boolean, assignValue: ParameterValue) => void
  20. }
  21. const ParameterItem: FC<ParameterItemProps> = ({
  22. parameterRule,
  23. value,
  24. onChange,
  25. className,
  26. onSwitch,
  27. }) => {
  28. const language = useLanguage()
  29. const [localValue, setLocalValue] = useState(value)
  30. const numberInputRef = useRef<HTMLInputElement>(null)
  31. const getDefaultValue = () => {
  32. let defaultValue: ParameterValue
  33. if (parameterRule.type === 'int' || parameterRule.type === 'float')
  34. defaultValue = isNullOrUndefined(parameterRule.default) ? (parameterRule.min || 0) : parameterRule.default
  35. else if (parameterRule.type === 'string')
  36. defaultValue = parameterRule.options?.length ? (parameterRule.default || '') : (parameterRule.default || '')
  37. else if (parameterRule.type === 'boolean')
  38. defaultValue = !isNullOrUndefined(parameterRule.default) ? parameterRule.default : false
  39. else if (parameterRule.type === 'tag')
  40. defaultValue = !isNullOrUndefined(parameterRule.default) ? parameterRule.default : []
  41. return defaultValue
  42. }
  43. const renderValue = value ?? localValue ?? getDefaultValue()
  44. const handleInputChange = (newValue: ParameterValue) => {
  45. setLocalValue(newValue)
  46. if (onChange && (parameterRule.name === 'stop' || !isNullOrUndefined(value)))
  47. onChange(newValue)
  48. }
  49. const handleNumberInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  50. let num = +e.target.value
  51. if (!isNullOrUndefined(parameterRule.max) && num > parameterRule.max!) {
  52. num = parameterRule.max as number
  53. numberInputRef.current!.value = `${num}`
  54. }
  55. if (!isNullOrUndefined(parameterRule.min) && num < parameterRule.min!)
  56. num = parameterRule.min as number
  57. handleInputChange(num)
  58. }
  59. const handleNumberInputBlur = () => {
  60. if (numberInputRef.current)
  61. numberInputRef.current.value = renderValue as string
  62. }
  63. const handleSlideChange = (num: number) => {
  64. if (!isNullOrUndefined(parameterRule.max) && num > parameterRule.max!) {
  65. handleInputChange(parameterRule.max)
  66. numberInputRef.current!.value = `${parameterRule.max}`
  67. return
  68. }
  69. if (!isNullOrUndefined(parameterRule.min) && num < parameterRule.min!) {
  70. handleInputChange(parameterRule.min)
  71. numberInputRef.current!.value = `${parameterRule.min}`
  72. return
  73. }
  74. handleInputChange(num)
  75. numberInputRef.current!.value = `${num}`
  76. }
  77. const handleRadioChange = (v: number) => {
  78. handleInputChange(v === 1)
  79. }
  80. const handleStringInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  81. handleInputChange(e.target.value)
  82. }
  83. const handleSelect = (option: { value: string | number; name: string }) => {
  84. handleInputChange(option.value)
  85. }
  86. const handleTagChange = (newSequences: string[]) => {
  87. handleInputChange(newSequences)
  88. }
  89. const handleSwitch = (checked: boolean) => {
  90. if (onSwitch) {
  91. const assignValue: ParameterValue = localValue || getDefaultValue()
  92. onSwitch(checked, assignValue)
  93. }
  94. }
  95. const renderInput = () => {
  96. const numberInputWithSlide = (parameterRule.type === 'int' || parameterRule.type === 'float')
  97. && !isNullOrUndefined(parameterRule.min)
  98. && !isNullOrUndefined(parameterRule.max)
  99. if (parameterRule.type === 'int' || parameterRule.type === 'float') {
  100. let step = 100
  101. if (parameterRule.max) {
  102. if (parameterRule.max < 10)
  103. step = 0.1
  104. else if (parameterRule.max < 100)
  105. step = 1
  106. else if (parameterRule.max < 1000)
  107. step = 10
  108. else if (parameterRule.max < 10000)
  109. step = 100
  110. }
  111. return (
  112. <>
  113. {numberInputWithSlide && <Slider
  114. className='w-[120px]'
  115. value={renderValue as number}
  116. min={parameterRule.min}
  117. max={parameterRule.max}
  118. step={step}
  119. onChange={handleSlideChange}
  120. />}
  121. <input
  122. ref={numberInputRef}
  123. className='shrink-0 block ml-4 pl-3 w-16 h-8 appearance-none outline-none rounded-lg bg-gray-100 text-[13px] text-gra-900'
  124. type='number'
  125. max={parameterRule.max}
  126. min={parameterRule.min}
  127. step={numberInputWithSlide ? step : +`0.${parameterRule.precision || 0}`}
  128. onChange={handleNumberInputChange}
  129. onBlur={handleNumberInputBlur}
  130. />
  131. </>
  132. )
  133. }
  134. if (parameterRule.type === 'boolean') {
  135. return (
  136. <Radio.Group
  137. className='w-[200px] flex items-center'
  138. value={renderValue ? 1 : 0}
  139. onChange={handleRadioChange}
  140. >
  141. <Radio value={1} className='!mr-1 w-[94px]'>True</Radio>
  142. <Radio value={0} className='w-[94px]'>False</Radio>
  143. </Radio.Group>
  144. )
  145. }
  146. if (parameterRule.type === 'string' && !parameterRule.options?.length) {
  147. return (
  148. <input
  149. className='flex items-center px-3 w-[200px] h-8 appearance-none outline-none rounded-lg bg-gray-100 text-[13px] text-gra-900'
  150. value={renderValue as string}
  151. onChange={handleStringInputChange}
  152. />
  153. )
  154. }
  155. if (parameterRule.type === 'string' && !!parameterRule?.options?.length) {
  156. return (
  157. <SimpleSelect
  158. className='!py-0'
  159. wrapperClassName='!w-[200px] !h-8'
  160. defaultValue={renderValue as string}
  161. onSelect={handleSelect}
  162. items={parameterRule.options.map(option => ({ value: option, name: option }))}
  163. />
  164. )
  165. }
  166. if (parameterRule.type === 'tag') {
  167. return (
  168. <div className='w-[200px]'>
  169. <TagInput
  170. items={renderValue as string[]}
  171. onChange={handleTagChange}
  172. customizedConfirmKey='Tab'
  173. />
  174. </div>
  175. )
  176. }
  177. return null
  178. }
  179. useEffect(() => {
  180. if (numberInputRef.current)
  181. numberInputRef.current.value = `${renderValue}`
  182. }, [])
  183. return (
  184. <div className={`flex items-center justify-between ${className}`}>
  185. <div>
  186. <div className='shrink-0 flex items-center w-[200px]'>
  187. <div
  188. className='mr-0.5 text-[13px] font-medium text-gray-700 truncate'
  189. title={parameterRule.label[language]}
  190. >
  191. {parameterRule.label[language]}
  192. </div>
  193. {
  194. parameterRule.help && (
  195. <Tooltip
  196. selector={`model-parameter-rule-${parameterRule.name}`}
  197. htmlContent={(
  198. <div className='w-[200px] whitespace-pre-wrap'>{parameterRule.help[language]}</div>
  199. )}
  200. >
  201. <HelpCircle className='mr-1.5 w-3.5 h-3.5 text-gray-400' />
  202. </Tooltip>
  203. )
  204. }
  205. {
  206. !parameterRule.required && parameterRule.name !== 'stop' && (
  207. <Switch
  208. defaultValue={!isNullOrUndefined(value)}
  209. onChange={handleSwitch}
  210. size='md'
  211. />
  212. )
  213. }
  214. </div>
  215. {
  216. parameterRule.type === 'tag' && (
  217. <div className='w-[200px] text-gray-400 text-xs font-normal'>
  218. {parameterRule?.tagPlaceholder?.[language]}
  219. </div>
  220. )
  221. }
  222. </div>
  223. {renderInput()}
  224. </div>
  225. )
  226. }
  227. export default ParameterItem