2023-04-25 05:12:14 +00:00
|
|
|
import React, { useState, useMemo } from 'react';
|
2023-04-08 06:40:18 +00:00
|
|
|
import { onFieldInputValueChange, onFormInitialValuesChange } from '@formily/core';
|
|
|
|
import { useForm, observer, connect, mapReadPretty, useFormEffects } from '@formily/react';
|
2023-04-08 02:52:31 +00:00
|
|
|
import { Tag } from 'antd';
|
|
|
|
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-08 06:40:18 +00:00
|
|
|
const { onChange } = props;
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const options = useCollectionFieldOptions({ collection });
|
|
|
|
|
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-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(
|
|
|
|
() => useCollectionFieldOptions({ collection: values.sourceCollection }),
|
|
|
|
[values.sourceCollection, values.sourceCollection],
|
|
|
|
);
|
|
|
|
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));
|