2022-07-06 10:34:48 +08:00
|
|
|
import React from "react";
|
2022-07-07 10:26:33 +08:00
|
|
|
import { Select, Cascader } from 'antd';
|
2022-07-06 10:34:48 +08:00
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
2022-07-07 10:26:33 +08:00
|
|
|
import { useCollectionManager, useCollectionFilterOptions, useCompile } from "@nocobase/client";
|
2022-07-06 10:34:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default function (props) {
|
2022-07-07 10:26:33 +08:00
|
|
|
const { collection, value, onChange } = props;
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const compile = useCompile();
|
|
|
|
const { getCollectionFields } = useCollectionManager();
|
|
|
|
const fields = getCollectionFields(collection)
|
|
|
|
.filter(field => field.interface && (!field.target || field.type === 'belongsTo'))
|
|
|
|
.map(field => field.type === 'belongsTo'
|
|
|
|
? {
|
|
|
|
title: `${compile(field.uiSchema?.title || field.name)} ID`,
|
|
|
|
name: field.foreignKey
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
title: compile(field.uiSchema?.title || field.name),
|
|
|
|
name: field.name
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
placeholder={t('Fields')}
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
>
|
|
|
|
{fields
|
|
|
|
.map(field => (
|
|
|
|
<Select.Option key={field.name} value={field.name}>{field.title}</Select.Option>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</Select>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function SelectWithAssociations(props) {
|
2022-07-06 10:34:48 +08:00
|
|
|
const { collection, value, onChange } = props;
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const compile = useCompile();
|
|
|
|
const fields = useCollectionFilterOptions(collection);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Cascader
|
|
|
|
fieldNames={{
|
|
|
|
label: 'title',
|
|
|
|
value: 'name',
|
|
|
|
children: 'children',
|
|
|
|
}}
|
|
|
|
changeOnSelect={false}
|
|
|
|
value={Array.isArray(value) ? value : value?.split('.')}
|
|
|
|
options={compile(fields)}
|
|
|
|
onChange={onChange}
|
|
|
|
placeholder={t('Select Field')}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|