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-04-25 05:12:14 +00:00
|
|
|
import { 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';
|
|
|
|
import { useCollectionFieldOptions } from '../variable';
|
2023-04-08 02:52:31 +00:00
|
|
|
|
|
|
|
const InternalExpression = observer((props: any) => {
|
2023-04-26 01:56:25 +00:00
|
|
|
const { onChange, sourceCollection } = props;
|
2023-04-08 06:40:18 +00:00
|
|
|
const { values } = useForm();
|
|
|
|
const [collection, setCollection] = useState(values?.sourceCollection);
|
2023-04-08 02:52:31 +00:00
|
|
|
|
|
|
|
useFormEffects(() => {
|
2023-04-25 05:12:14 +00:00
|
|
|
onFormInitialValuesChange((form) => {
|
2023-04-08 06:40:18 +00:00
|
|
|
setCollection(form.values.sourceCollection);
|
|
|
|
});
|
2023-04-08 02:52:31 +00:00
|
|
|
onFieldInputValueChange('sourceCollection', (f) => {
|
|
|
|
setCollection(f.value);
|
|
|
|
onChange(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-04-26 01:56:25 +00:00
|
|
|
const options = useCollectionFieldOptions({ collection: sourceCollection || collection });
|
2023-04-08 02:52:31 +00:00
|
|
|
|
2023-04-25 05:12:14 +00:00
|
|
|
return <Variable.TextArea {...props} scope={options} />;
|
2023-04-08 02:52:31 +00:00
|
|
|
});
|
|
|
|
|
2023-04-08 06:40:18 +00:00
|
|
|
function Result(props) {
|
2023-04-26 01:56:25 +00:00
|
|
|
const { sourceCollection } = props;
|
2023-04-08 02:52:31 +00:00
|
|
|
const { t } = useTranslation();
|
2023-04-08 06:40:18 +00:00
|
|
|
const values = useRecord();
|
2023-04-25 05:12:14 +00:00
|
|
|
const options = useMemo(
|
2023-04-26 01:56:25 +00:00
|
|
|
() => useCollectionFieldOptions({ collection: sourceCollection || values.sourceCollection }),
|
|
|
|
[values.sourceCollection, values.sourceCollection, 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));
|