Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/client/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@
"value-name": "Value Name",
"missing-value-name": "Missing value name",
"check-only-number": "Only numbers can be entered",
"invalid-json": "Invalid JSON format",
"json-value-placeholder": "Enter JSON value, e.g. {\"key\": \"value\"}",
"add-form-list-btn": "Add keyword argument"
}
},
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/i18n/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@
"value-name": "值名称",
"missing-value-name": "缺少值名称",
"check-only-number": "只能输入数字",
"invalid-json": "无效的 JSON 格式",
"json-value-placeholder": "输入 JSON 格式的值,例如 {\"key\": \"value\"}",
"add-form-list-btn": "添加关键字参数"
}
},
Expand Down
233 changes: 169 additions & 64 deletions packages/client/src/pages/FridayPage/SettingPage/KwargsFormList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { MinusCircleIcon, PlusCircleIcon } from 'lucide-react';
import { Button } from '@/components/ui/button.tsx';
import { inputTypeOptions, booleanOptions } from '../config';

const { TextArea } = Input;

const KwargsFormList = ({ name }: { name: string }) => {
const { t } = useTranslation();
const form = Form.useFormInstance();
Expand All @@ -19,7 +21,7 @@ const KwargsFormList = ({ name }: { name: string }) => {

return (
<Form.Item label={label} shouldUpdate help={help}>
{({ getFieldValue, setFieldValue }) => (
{({ setFieldValue }) => (
<Form.List name={name}>
{(fields, { add, remove }) => (
<>
Expand Down Expand Up @@ -112,72 +114,175 @@ const KwargsFormList = ({ name }: { name: string }) => {
/>
</Form.Item>
<Form.Item
{...restField}
name={[fieldName, 'value']}
className="flex-1 min-w-0"
validateTrigger={
isNewlyAdded
? ['onSubmit']
: ['onBlur', 'onChange']
}
rules={
getFieldValue([
name,
fieldName,
'type',
]) === 'number'
? [
{
pattern:
/^-?\d+(\.\d+)?$/,
required: true,
whitespace: true,
message: t(
'help.friday.check-only-number',
),
},
]
: [
{
required: true,
whitespace: true,
message: t(
'help.friday.missing-value-name',
),
},
]
}
noStyle
shouldUpdate={(
prevValues,
currentValues,
) => {
const prevType =
prevValues[name]?.[
fieldName
]?.type;
const currentType =
currentValues[name]?.[
fieldName
]?.type;
return (
prevType !== currentType
);
}}
>
{getFieldValue([
name,
fieldName,
'type',
]) === 'boolean' ? (
<Select
placeholder={t(
'help.friday.value-name',
)}
className="w-full"
options={booleanOptions}
onFocus={() => {
newlyAddedRef.current.delete(
fieldName,
{({ getFieldValue }) => {
const fieldType =
getFieldValue([
name,
fieldName,
'type',
]);

// Define rules based on field type
const rules = (() => {
if (
fieldType ===
'number'
) {
return [
{
pattern:
/^-?\d+(\.\d+)?$/,
required: true,
whitespace: true,
message: t(
'help.friday.check-only-number',
),
},
];
} else if (
fieldType === 'json'
) {
return [
{
required: true,
whitespace: true,
message: t(
'help.friday.missing-value-name',
),
},
{
validator:
async (
_: unknown,
value: string,
) => {
if (
!value
)
return;
try {
JSON.parse(
value,
);
} catch {
throw new Error(
t(
'help.friday.invalid-json',
),
);
}
},
},
];
} else {
return [
{
required: true,
whitespace: true,
message: t(
'help.friday.missing-value-name',
),
},
];
}
})();

// Render input component based on field type
const renderInput = () => {
if (
fieldType ===
'boolean'
) {
return (
<Select
placeholder={t(
'help.friday.value-name',
)}
className="w-full"
options={
booleanOptions
}
onFocus={() => {
newlyAddedRef.current.delete(
fieldName,
);
}}
/>
);
}}
/>
) : (
<Input
placeholder={t(
'help.friday.value-name',
)}
className="w-full"
onFocus={() => {
newlyAddedRef.current.delete(
fieldName,
} else if (
fieldType === 'json'
) {
return (
<TextArea
placeholder={t(
'help.friday.json-value-placeholder',
)}
className="w-full font-mono text-xs"
rows={4}
autoSize={{
minRows: 4,
maxRows: 8,
}}
onFocus={() => {
newlyAddedRef.current.delete(
fieldName,
);
}}
/>
);
}}
/>
)}
} else {
return (
<Input
placeholder={t(
'help.friday.value-name',
)}
className="w-full"
onFocus={() => {
newlyAddedRef.current.delete(
fieldName,
);
}}
/>
);
}
};

return (
<Form.Item
{...restField}
name={[
fieldName,
'value',
]}
className="flex-1 min-w-0"
validateTrigger={[
'onBlur',
'onChange',
]}
rules={rules}
>
{renderInput()}
</Form.Item>
);
}}
</Form.Item>

<Button
Expand Down
Loading
Loading