nocobase/packages/plugins/workflow/src/client/components/DynamicExpression.tsx
Junyi c9b726916c
refactor(client): refactor variable components and variables in workflow (#2157)
* refactor(plugin-workflow): change collection variables to lazy load

* fix(plugin-workflow): avoid to-many reverse loading for association field

* fix(client): fix variable components

* chore(client): fix type

* fix(client): fix current user lazy load options

* refactor(client): remove compile from variable components which potencially causing bug

* fix(plugin-workflow): fix scope argument for new api

* fix(client): fix constant type options

* fix(client): fix infinity rerendering

* fix: avoid closure problem

* fix(client): should use no children when lazy load

* refactor(client): refactor AssignedField to use Variable component

* fix(client): fix type

* fix(plugin-workflow): fix variable options in some node not changes

* fix(plugin-workflow): fix select variable for operand crash (T-815)

* fix(plugin-workflow): variable types detect

* fix(plugin-workflow): detect association to match types

* fix(plugin-workflow): fix variable type filter logic

* fix(plugin-workflow): fix optional types

* fix(plugin-workflow): make changeOnSelect configurable in TextArea and JSONInput

---------

Co-authored-by: Rairn <958414905@qq.com>
2023-07-05 07:01:41 -07:00

54 lines
1.8 KiB
TypeScript

import { onFieldInputValueChange, onFormInitialValuesChange } from '@formily/core';
import { connect, mapReadPretty, observer, 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';
import { NAMESPACE } from '../locale';
import { getCollectionFieldOptions } from '../variable';
const InternalExpression = observer(
(props: any) => {
const { onChange } = props;
const { values } = useForm();
const [collection, setCollection] = useState(values?.sourceCollection);
const compile = useCompile();
const { getCollectionFields } = useCollectionManager();
useFormEffects(() => {
onFormInitialValuesChange((form) => {
setCollection(form.values.sourceCollection);
});
onFieldInputValueChange('sourceCollection', (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));