nocobase/packages/plugins/@nocobase/plugin-workflow-dynamic-calculation/src/client/DynamicExpression.tsx

58 lines
2.1 KiB
TypeScript
Raw Normal View History

import { onFieldInputValueChange, onFormInitialValuesChange } from '@formily/core';
import { connect, mapReadPretty, observer, useField, useForm, useFormEffects } from '@formily/react';
import { Tag } from 'antd';
import React, { useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useCollectionManager, useCompile, useRecord, Variable } from '@nocobase/client';
refactor(plugin-workflow): split workflow features into plugins (#3115) * refactor(plugin-workflow): split manual and dynamic calculation into plugins * refactor(plugin-workflow): move loop to plugin * refactor(plugin-workflow): move parallel to plugin * fix(plugin-dynamic-calculation): fix package title * fix(plugin-workflow): fix plugin name * refactor(plugin-workflow): move delay to plugin * refactor(plugin-workflow): simplify exporting names * refactor(plugin-workflow): move aggregate to plugin * refactor(plugin-workflow): move sql to plugin * refactor(plugin-workflow): move reqeust to plugin * refactor(plugin-workflow): move form trigger to plugin * refactor(plugin-workflow): move locale to plugins * fix(plugin-workflow): fix test cases * fix(plugin-workflow-request): package name typo * fix(plugin-workflow): remove clean db from testkit * fix(plugin-workflow-sql): skip independent case * fix(plugin-workflow-sql): skip independent case * fix(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * test(plugin-workflow-delay): fix test cases * fix(plugin-workflow): fix migration version matching * test(plugin-workflow): fix test case * refactor(plugin-workflow): correct exporting of testkit * fix(plugin-workflow): fix testkit and require module * refactor(plugin-workflow): add workflow-test package for testing * test(plugin-workflow): test weird case * fix(plugin-workflow-test): remove workflow dependency to avoid cycling * fix(plugin-workflow): fix migration version * fix(plugin-workflow): fix migration and packages * fix(plugin-workflow): fix package dependencies * fix(preset): fix builtin list in preset * fix(plugin-workflow): add package entry file * fix(plugin-workflow): fix migrations * refactor(plugin-workflow): remove require * fix(plugin-workflow): fix locale namespace * fix(plugin-workflow): fix merged errors * fix(plugin-workflow): fix import cycling references * refactor(plugin-workflow): change instruction and triggers to classes in client * fix(plugin-workflow): fix migration version
2023-12-07 13:46:58 +00:00
import { getCollectionFieldOptions } from '@nocobase/plugin-workflow/client';
import { NAMESPACE } from '../locale';
const InternalExpression = observer(
(props: any) => {
const { onChange } = props;
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));
const compile = useCompile();
const { getCollectionFields } = useCollectionManager();
useFormEffects(() => {
onFormInitialValuesChange((form) => {
setCollection(form.getValuesIn(collectionPath));
});
onFieldInputValueChange(collectionPath, (f) => {
setCollection(f.value);
onChange(null);
});
});
const options = getCollectionFieldOptions({ collection, compile, getCollectionFields });
return <Variable.TextArea {...props} scope={options} />;
},
{ displayName: 'InternalExpression' },
);
function Result(props) {
const { t } = useTranslation();
const values = useRecord();
const compile = useCompile();
const { getCollectionFields } = useCollectionManager();
const options = useMemo(
() => getCollectionFieldOptions({ collection: values.sourceCollection, compile, getCollectionFields }),
[values.sourceCollection, values.sourceCollection],
);
return props.value ? (
<Variable.TextArea {...props} scope={options} />
) : (
<Tag>{t('Unconfigured', { ns: NAMESPACE })}</Tag>
);
}
export const DynamicExpression = connect(InternalExpression, mapReadPretty(Result));