Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support array element type Enum #150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
35 changes: 32 additions & 3 deletions packages/admin/src/components/Fields/FieldContentEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Rule } from 'antd/es/form'
import { IConnectEditor, IDatePicker, IFileAndImageEditor } from '@/components/Fields'
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons'
import { IObjectEditor } from './Object'
import { FormListFieldData } from 'antd/es/form/FormList'

const MarkdownEditor = React.lazy(() => import('@/components/Fields/Markdown'))
const RichTextEditor = React.lazy(() => import('@/components/Fields/RichText'))
Expand Down Expand Up @@ -154,6 +155,36 @@ export function getFieldEditor(field: SchemaField, key: number) {
)
break
case 'Array':
const schemaField = field as SchemaField & { [key: string]: any };
const renderItem = (formField: FormListFieldData) => {
switch (schemaField.elementType) {
case 'Enum':
return (
<Form.Item {...formField} noStyle validateTrigger={['onChange', 'onBlur']}>
<Select style={{ width: '40%' }} >
{enumElements?.length ? (
enumElements?.map((ele, index) => (
<Option value={ele.value} key={index}>
{ele.label}
</Option>
))
) : (
<Option value="" disabled>
</Option>
)}
</Select>
</Form.Item>
);
default:
return (
<Form.Item {...formField} noStyle validateTrigger={['onChange', 'onBlur']}>
<Input style={{ width: '60%' }} />
</Form.Item>
);
}
}

FieldEditor = (
<Form.List name={name}>
{(fields, { add, remove }) => {
Expand All @@ -162,9 +193,7 @@ export function getFieldEditor(field: SchemaField, key: number) {
{fields?.map((field, index) => {
return (
<Form.Item key={index}>
<Form.Item {...field} noStyle validateTrigger={['onChange', 'onBlur']}>
<Input style={{ width: '60%' }} />
</Form.Item>
{renderItem(field)}
<MinusCircleOutlined
className="dynamic-delete-button"
style={{ margin: '0 8px' }}
Expand Down
17 changes: 14 additions & 3 deletions packages/admin/src/components/Fields/FieldContentRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,22 @@ export function getFieldRender(field: SchemaField) {
return text
}

const f = field as SchemaField & { [key: string]: any };

const renderItem = (val: string, index: number) => {
switch (f.elementType) {
case 'Enum':
const enumOptions = f.enumElements || [];
const displayVal = enumOptions.find(e => e.value === val)?.label;
return <Tag key={index}>{displayVal || val}</Tag>;
default:
return <Tag key={index}>{val}</Tag>;
}
}

return (
<Space direction="vertical">
{record[name]?.map((val: string, index: number) => (
<Tag key={index}>{val}</Tag>
))}
{record[name]?.map(renderItem)}
</Space>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,32 @@ export function getFieldFormItem(
</Form.Item>
</>
)
case 'Array':
const renderElement = (fieldType: string,) => {
switch (fieldType) {
case 'Enum':
return getFieldFormItem(fieldType, { ...options });
default:
return null;
}
}

return (
<>
<Form.Item label="数组元素类型" name="elementType" validateTrigger={['onChange']}>
<Select placeholder="元素值类型">
<Option value="Enum">枚举</Option>
<Option value="any">任意类型</Option>
</Select>
</Form.Item>
<Form.Item label="数组元素配置" dependencies={['elementType']}>
{(ele) => {
const filed = ele.getFieldValue('elementType');
return renderElement(filed);
}}
</Form.Item>
</>
)
default:
return ''
}
Expand Down