2023-04-08 06:40:18 +00:00
|
|
|
import { onFieldInputValueChange, onFormInitialValuesChange } from '@formily/core';
|
2023-07-26 07:38:51 +00:00
|
|
|
import { connect, mapReadPretty, observer, useField, 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-12-07 13:46:58 +00:00
|
|
|
import { getCollectionFieldOptions } from '@nocobase/plugin-workflow/client';
|
2023-04-08 02:52:31 +00:00
|
|
|
|
2023-04-25 05:12:14 +00:00
|
|
|
import { NAMESPACE } from '../locale';
|
2023-04-08 02:52:31 +00:00
|
|
|
|
2023-06-06 11:33:04 +00:00
|
|
|
const InternalExpression = observer(
|
|
|
|
(props: any) => {
|
|
|
|
const { onChange } = props;
|
2023-07-26 07:38:51 +00:00
|
|
|
const field = useField<any>();
|
|
|
|
// TODO(refactor): better to provide another context like useFieldset()
|
|
|
|
const form = useForm();
|
|
|
|
const basePath = field.path.segments.slice(0, -1);
|
|
|
|
const collectionPath = [...basePath, 'sourceCollection'].join('.');
|
|
|
|
const [collection, setCollection] = useState(form.getValuesIn(collectionPath));
|
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) => {
|
2023-07-26 07:38:51 +00:00
|
|
|
setCollection(form.getValuesIn(collectionPath));
|
2023-06-06 11:33:04 +00:00
|
|
|
});
|
2023-07-26 07:38:51 +00:00
|
|
|
onFieldInputValueChange(collectionPath, (f) => {
|
2023-06-06 11:33:04 +00:00
|
|
|
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));
|