mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-09 15:39:24 +08:00
* feat: recordSet field * fix: record set field * test: add tests * fix: tests * fix: build * feat: front end * refactor: belongs to array field * fix: tests * fix: version * fix: version * fix: build * chore: update * chore: add error * chore: remove only * feat: add locales * fix: version * fix: e2e * fix: fix T-4661 * fix: fix T-4663 * fix: fix T-4665 * fix: fix T-4670 * fix: fix T-4666 * fix: fix T-4664 * fix: fix T-4668 * fix: test * fix: fix T-4669 * fix: fix T-4667 * fix: bug * fix: fix T-4670 * chore: add transaction * feat: beforeAddDataSource hook * feat: support external database sources, fix T-4717 * fix: bug * fix: fix T-4671 * fix: fix T-4769 * fix: version * fix: fix T-4762 * fix: array type interface * fix: fix T-4742 * fix: fix T-4661 * fix: bug * fix: bug * feat: check association keys in backend * fix: bug * fix: bug * fix: bug * fix: test * fix: bug * fix: e2e --------- Co-authored-by: Chareice <chareice@live.com>
74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
/**
|
|
* This file is part of the NocoBase (R) project.
|
|
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
* Authors: NocoBase Team.
|
|
*
|
|
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
*/
|
|
|
|
import { observer, useField } from '@formily/react';
|
|
import { AutoComplete, Select } from 'antd';
|
|
import React, { useState, useEffect } from 'react';
|
|
import { useRecord, useCompile } from '@nocobase/client';
|
|
import { useMBMFields } from './hooks';
|
|
|
|
export const ForeignKey = observer(
|
|
(props: any) => {
|
|
const { disabled } = props;
|
|
const [options, setOptions] = useState([]);
|
|
const record = useRecord();
|
|
const field: any = useField();
|
|
const { type, template } = record;
|
|
const value = record[field.props.name];
|
|
const compile = useCompile();
|
|
const [initialValue, setInitialValue] = useState(value || (template === 'view' ? null : field.initialValue));
|
|
const { foreignKeys } = useMBMFields();
|
|
useEffect(() => {
|
|
const fields = foreignKeys;
|
|
if (fields) {
|
|
const sourceOptions = fields.map((k) => {
|
|
return {
|
|
value: k.name,
|
|
label: compile(k.uiSchema?.title || k.name),
|
|
};
|
|
});
|
|
setOptions(sourceOptions);
|
|
if (value) {
|
|
const option = sourceOptions.find((v) => v.value === value);
|
|
setInitialValue(option?.label || value);
|
|
}
|
|
}
|
|
}, [type]);
|
|
const Component = template === 'view' ? Select : AutoComplete;
|
|
return (
|
|
<div>
|
|
<Component
|
|
disabled={disabled}
|
|
value={initialValue}
|
|
options={options}
|
|
showSearch
|
|
onDropdownVisibleChange={async (open) => {
|
|
const fields = foreignKeys;
|
|
if (fields && open) {
|
|
setOptions(
|
|
fields.map((k) => {
|
|
return {
|
|
value: k.name,
|
|
label: compile(k.uiSchema?.title || k.name),
|
|
};
|
|
}),
|
|
);
|
|
}
|
|
}}
|
|
onChange={(value, option) => {
|
|
props?.onChange?.(value);
|
|
setInitialValue(option.label || value);
|
|
}}
|
|
/>
|
|
</div>
|
|
);
|
|
},
|
|
{ displayName: 'MBMForeignKey' },
|
|
);
|