mirror of
https://github.com/langgenius/dify.git
synced 2026-04-29 04:26:30 +08:00
feat: update checkbox component in the panel and refactor form types for checkbox and boolean
This commit is contained in:
parent
dc4801c014
commit
2793ede875
@ -20,7 +20,7 @@ const textareaVariants = cva(
|
|||||||
)
|
)
|
||||||
|
|
||||||
export type TextareaProps = {
|
export type TextareaProps = {
|
||||||
value: string
|
value: string | number
|
||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
destructive?: boolean
|
destructive?: boolean
|
||||||
styleCss?: CSSProperties
|
styleCss?: CSSProperties
|
||||||
|
|||||||
@ -14,7 +14,8 @@ export enum FormTypeEnum {
|
|||||||
secretInput = 'secret-input',
|
secretInput = 'secret-input',
|
||||||
select = 'select',
|
select = 'select',
|
||||||
radio = 'radio',
|
radio = 'radio',
|
||||||
boolean = 'checkbox',
|
checkbox = 'checkbox',
|
||||||
|
boolean = 'boolean',
|
||||||
files = 'files',
|
files = 'files',
|
||||||
file = 'file',
|
file = 'file',
|
||||||
modelSelector = 'model-selector',
|
modelSelector = 'model-selector',
|
||||||
|
|||||||
@ -264,7 +264,7 @@ function Form<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (formSchema.type === FormTypeEnum.boolean) {
|
if (formSchema.type === FormTypeEnum.checkbox) {
|
||||||
const {
|
const {
|
||||||
variable, label, show_on, required,
|
variable, label, show_on, required,
|
||||||
} = formSchema as CredentialFormSchemaRadio
|
} = formSchema as CredentialFormSchemaRadio
|
||||||
|
|||||||
@ -54,7 +54,7 @@ const ReasoningConfigForm: React.FC<Props> = ({
|
|||||||
const getVarKindType = (type: FormTypeEnum) => {
|
const getVarKindType = (type: FormTypeEnum) => {
|
||||||
if (type === FormTypeEnum.file || type === FormTypeEnum.files)
|
if (type === FormTypeEnum.file || type === FormTypeEnum.files)
|
||||||
return VarKindType.variable
|
return VarKindType.variable
|
||||||
if (type === FormTypeEnum.select || type === FormTypeEnum.boolean || type === FormTypeEnum.textNumber || type === FormTypeEnum.array || type === FormTypeEnum.object)
|
if (type === FormTypeEnum.select || type === FormTypeEnum.checkbox || type === FormTypeEnum.textNumber || type === FormTypeEnum.array || type === FormTypeEnum.object)
|
||||||
return VarKindType.constant
|
return VarKindType.constant
|
||||||
if (type === FormTypeEnum.textInput || type === FormTypeEnum.secretInput)
|
if (type === FormTypeEnum.textInput || type === FormTypeEnum.secretInput)
|
||||||
return VarKindType.mixed
|
return VarKindType.mixed
|
||||||
@ -164,7 +164,7 @@ const ReasoningConfigForm: React.FC<Props> = ({
|
|||||||
const isArray = type === FormTypeEnum.array
|
const isArray = type === FormTypeEnum.array
|
||||||
const isShowJSONEditor = isObject || isArray
|
const isShowJSONEditor = isObject || isArray
|
||||||
const isFile = type === FormTypeEnum.file || type === FormTypeEnum.files
|
const isFile = type === FormTypeEnum.file || type === FormTypeEnum.files
|
||||||
const isBoolean = type === FormTypeEnum.boolean
|
const isBoolean = type === FormTypeEnum.checkbox
|
||||||
const isSelect = type === FormTypeEnum.select
|
const isSelect = type === FormTypeEnum.select
|
||||||
const isAppSelector = type === FormTypeEnum.appSelector
|
const isAppSelector = type === FormTypeEnum.appSelector
|
||||||
const isModelSelector = type === FormTypeEnum.modelSelector
|
const isModelSelector = type === FormTypeEnum.modelSelector
|
||||||
|
|||||||
@ -181,7 +181,7 @@ export const getConfiguredValue = (value: Record<string, any>, formSchemas: { va
|
|||||||
const getVarKindType = (type: FormTypeEnum) => {
|
const getVarKindType = (type: FormTypeEnum) => {
|
||||||
if (type === FormTypeEnum.file || type === FormTypeEnum.files)
|
if (type === FormTypeEnum.file || type === FormTypeEnum.files)
|
||||||
return VarKindType.variable
|
return VarKindType.variable
|
||||||
if (type === FormTypeEnum.select || type === FormTypeEnum.boolean || type === FormTypeEnum.textNumber)
|
if (type === FormTypeEnum.select || type === FormTypeEnum.checkbox || type === FormTypeEnum.textNumber)
|
||||||
return VarKindType.constant
|
return VarKindType.constant
|
||||||
if (type === FormTypeEnum.textInput || type === FormTypeEnum.secretInput)
|
if (type === FormTypeEnum.textInput || type === FormTypeEnum.secretInput)
|
||||||
return VarKindType.mixed
|
return VarKindType.mixed
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
'use client'
|
'use client'
|
||||||
import type { FC } from 'react'
|
import type { FC } from 'react'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useMemo, useState } from 'react'
|
||||||
import { type ResourceVarInputs, VarKindType } from '../types'
|
import { type ResourceVarInputs, VarKindType } from '../types'
|
||||||
import type { CredentialFormSchema, FormOption } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
import type { CredentialFormSchema, FormOption } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||||
import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks'
|
import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks'
|
||||||
@ -17,7 +17,6 @@ import useAvailableVarList from '@/app/components/workflow/nodes/_base/hooks/use
|
|||||||
import Input from '@/app/components/base/input'
|
import Input from '@/app/components/base/input'
|
||||||
import { SimpleSelect } from '@/app/components/base/select'
|
import { SimpleSelect } from '@/app/components/base/select'
|
||||||
import MixedVariableTextInput from '@/app/components/workflow/nodes/tool/components/mixed-variable-text-input'
|
import MixedVariableTextInput from '@/app/components/workflow/nodes/tool/components/mixed-variable-text-input'
|
||||||
import FormInputBoolean from './form-input-boolean'
|
|
||||||
import AppSelector from '@/app/components/plugins/plugin-detail-panel/app-selector'
|
import AppSelector from '@/app/components/plugins/plugin-detail-panel/app-selector'
|
||||||
import ModelParameterModal from '@/app/components/plugins/plugin-detail-panel/model-selector'
|
import ModelParameterModal from '@/app/components/plugins/plugin-detail-panel/model-selector'
|
||||||
import VarReferencePicker from '@/app/components/workflow/nodes/_base/components/variable/var-reference-picker'
|
import VarReferencePicker from '@/app/components/workflow/nodes/_base/components/variable/var-reference-picker'
|
||||||
@ -29,6 +28,8 @@ import { ChevronDownIcon } from '@heroicons/react/20/solid'
|
|||||||
import { RiCheckLine, RiLoader4Line } from '@remixicon/react'
|
import { RiCheckLine, RiLoader4Line } from '@remixicon/react'
|
||||||
import type { Event } from '@/app/components/tools/types'
|
import type { Event } from '@/app/components/tools/types'
|
||||||
import { PluginCategoryEnum } from '@/app/components/plugins/types'
|
import { PluginCategoryEnum } from '@/app/components/plugins/types'
|
||||||
|
import CheckboxList from '@/app/components/base/checkbox-list'
|
||||||
|
import FormInputBoolean from './form-input-boolean'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
readOnly: boolean
|
readOnly: boolean
|
||||||
@ -69,6 +70,7 @@ const FormInputItem: FC<Props> = ({
|
|||||||
placeholder,
|
placeholder,
|
||||||
variable,
|
variable,
|
||||||
type,
|
type,
|
||||||
|
_type,
|
||||||
default: defaultValue,
|
default: defaultValue,
|
||||||
options,
|
options,
|
||||||
multiple,
|
multiple,
|
||||||
@ -81,7 +83,8 @@ const FormInputItem: FC<Props> = ({
|
|||||||
const isArray = type === FormTypeEnum.array
|
const isArray = type === FormTypeEnum.array
|
||||||
const isShowJSONEditor = isObject || isArray
|
const isShowJSONEditor = isObject || isArray
|
||||||
const isFile = type === FormTypeEnum.file || type === FormTypeEnum.files
|
const isFile = type === FormTypeEnum.file || type === FormTypeEnum.files
|
||||||
const isBoolean = type === FormTypeEnum.boolean
|
const isBoolean = _type === FormTypeEnum.boolean
|
||||||
|
const isCheckbox = _type === FormTypeEnum.checkbox
|
||||||
const isSelect = type === FormTypeEnum.select
|
const isSelect = type === FormTypeEnum.select
|
||||||
const isDynamicSelect = type === FormTypeEnum.dynamicSelect
|
const isDynamicSelect = type === FormTypeEnum.dynamicSelect
|
||||||
const isAppSelector = type === FormTypeEnum.appSelector
|
const isAppSelector = type === FormTypeEnum.appSelector
|
||||||
@ -280,6 +283,45 @@ const FormInputItem: FC<Props> = ({
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const availableCheckboxOptions = useMemo(() => (
|
||||||
|
(options || []).filter((option: { show_on?: Array<{ variable: string; value: any }> }) => {
|
||||||
|
if (option.show_on?.length)
|
||||||
|
return option.show_on.every(showOnItem => value[showOnItem.variable]?.value === showOnItem.value || value[showOnItem.variable] === showOnItem.value)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
), [options, value])
|
||||||
|
|
||||||
|
const checkboxListOptions = useMemo(() => (
|
||||||
|
availableCheckboxOptions.map((option: { value: string; label: Record<string, string> }) => ({
|
||||||
|
value: option.value,
|
||||||
|
label: option.label?.[language] || option.label?.en_US || option.value,
|
||||||
|
}))
|
||||||
|
), [availableCheckboxOptions, language])
|
||||||
|
|
||||||
|
const checkboxListValue = useMemo(() => {
|
||||||
|
let current: string[] = []
|
||||||
|
if (Array.isArray(varInput?.value))
|
||||||
|
current = varInput.value as string[]
|
||||||
|
else if (typeof varInput?.value === 'string')
|
||||||
|
current = [varInput.value as string]
|
||||||
|
else if (Array.isArray(defaultValue))
|
||||||
|
current = defaultValue as string[]
|
||||||
|
|
||||||
|
const allowedValues = new Set(availableCheckboxOptions.map((option: { value: string }) => option.value))
|
||||||
|
return current.filter(item => allowedValues.has(item))
|
||||||
|
}, [varInput?.value, defaultValue, availableCheckboxOptions])
|
||||||
|
|
||||||
|
const handleCheckboxListChange = (selected: string[]) => {
|
||||||
|
onChange({
|
||||||
|
...value,
|
||||||
|
[variable]: {
|
||||||
|
...(varInput || {}),
|
||||||
|
type: VarKindType.constant,
|
||||||
|
value: selected,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn('gap-1', !(isShowJSONEditor && isConstant) && 'flex')}>
|
<div className={cn('gap-1', !(isShowJSONEditor && isConstant) && 'flex')}>
|
||||||
{showTypeSwitch && (
|
{showTypeSwitch && (
|
||||||
@ -306,6 +348,16 @@ const FormInputItem: FC<Props> = ({
|
|||||||
placeholder={placeholder?.[language] || placeholder?.en_US}
|
placeholder={placeholder?.[language] || placeholder?.en_US}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
{isCheckbox && isConstant && (
|
||||||
|
<CheckboxList
|
||||||
|
title={schema.label?.[language] || schema.label?.en_US || variable}
|
||||||
|
value={checkboxListValue}
|
||||||
|
onChange={handleCheckboxListChange}
|
||||||
|
options={checkboxListOptions}
|
||||||
|
disabled={readOnly}
|
||||||
|
maxHeight='200px'
|
||||||
|
/>
|
||||||
|
)}
|
||||||
{isBoolean && isConstant && (
|
{isBoolean && isConstant && (
|
||||||
<FormInputBoolean
|
<FormInputBoolean
|
||||||
value={varInput?.value as boolean}
|
value={varInput?.value as boolean}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user