2023-04-08 06:40:18 +00:00
|
|
|
import { onFieldInputValueChange, onFormInitialValuesChange } from '@formily/core';
|
2023-04-26 01:56:25 +00:00
|
|
|
import { connect, mapReadPretty, observer, useForm, useFormEffects } from '@formily/react';
|
2023-04-08 02:52:31 +00:00
|
|
|
import { Tag } from 'antd';
|
2023-04-26 01:56:25 +00:00
|
|
|
import React, { useMemo, useState } from 'react';
|
2023-04-08 02:52:31 +00:00
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
2023-07-05 14:01:41 +00:00
|
|
|
import { useCollectionManager, useCompile, useRecord, Variable } from '@nocobase/client';
|
2023-04-08 02:52:31 +00:00
|
|
|
|
2023-04-25 05:12:14 +00:00
|
|
|
import { NAMESPACE } from '../locale';
|
2023-07-05 14:01:41 +00:00
|
|
|
import { getCollectionFieldOptions } from '../variable';
|
2023-04-08 02:52:31 +00:00
|
|
|
|
2023-06-06 11:33:04 +00:00
|
|
|
const InternalExpression = observer(
|
|
|
|
(props: any) => {
|
|
|
|
const { onChange } = props;
|
|
|
|
const { values } = useForm();
|
|
|
|
const [collection, setCollection] = useState(values?.sourceCollection);
|
2023-07-05 14:01:41 +00:00
|
|
|
const compile = useCompile();
|
|
|
|
const { getCollectionFields } = useCollectionManager();
|
2023-06-06 11:33:04 +00:00
|
|
|
|
|
|
|
useFormEffects(() => {
|
|
|
|
onFormInitialValuesChange((form) => {
|
|
|
|
setCollection(form.values.sourceCollection);
|
|
|
|
});
|
|
|
|
onFieldInputValueChange('sourceCollection', (f) => {
|
|
|
|
setCollection(f.value);
|
|
|
|
onChange(null);
|
|
|
|
});
|
2023-04-08 06:40:18 +00:00
|
|
|
});
|
2023-04-08 02:52:31 +00:00
|
|
|
|
2023-07-05 14:01:41 +00:00
|
|
|
const options = getCollectionFieldOptions({ collection, compile, getCollectionFields });
|
2023-04-08 02:52:31 +00:00
|
|
|
|
2023-06-06 11:33:04 +00:00
|
|
|
return <Variable.TextArea {...props} scope={options} />;
|
|
|
|
},
|
|
|
|
{ displayName: 'InternalExpression' },
|
|
|
|
);
|
2023-04-08 02:52:31 +00:00
|
|
|
|
2023-04-08 06:40:18 +00:00
|
|
|
function Result(props) {
|
2023-04-08 02:52:31 +00:00
|
|
|
const { t } = useTranslation();
|
2023-04-08 06:40:18 +00:00
|
|
|
const values = useRecord();
|
2023-07-05 14:01:41 +00:00
|
|
|
const compile = useCompile();
|
|
|
|
const { getCollectionFields } = useCollectionManager();
|
2023-04-25 05:12:14 +00:00
|
|
|
const options = useMemo(
|
2023-07-05 14:01:41 +00:00
|
|
|
() => getCollectionFieldOptions({ collection: values.sourceCollection, compile, getCollectionFields }),
|
2023-04-26 07:23:31 +00:00
|
|
|
[values.sourceCollection, values.sourceCollection],
|
2023-04-25 05:12:14 +00:00
|
|
|
);
|
|
|
|
return props.value ? (
|
|
|
|
<Variable.TextArea {...props} scope={options} />
|
|
|
|
) : (
|
|
|
|
<Tag>{t('Unconfigured', { ns: NAMESPACE })}</Tag>
|
|
|
|
);
|
2023-04-08 02:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const DynamicExpression = connect(InternalExpression, mapReadPretty(Result));
|