feat(data-vi): support for adding chart blocks in popups/drawers/sub-pages (#5248)
Some checks are pending
Build Docker Image / build-and-push (push) Waiting to run
Build Pro Image / build-and-push (push) Waiting to run
E2E / Build (push) Waiting to run
E2E / Core and plugins (push) Blocked by required conditions
E2E / plugin-workflow (push) Blocked by required conditions
E2E / plugin-workflow-approval (push) Blocked by required conditions
E2E / plugin-data-source-main (push) Blocked by required conditions
E2E / Comment on PR (push) Blocked by required conditions
NocoBase FrontEnd Test / frontend-test (18) (push) Waiting to run

This commit is contained in:
YANG QIA 2024-09-11 10:17:01 +08:00 committed by GitHub
parent 189f31b145
commit a04aeb17e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 52 additions and 16 deletions

View File

@ -73,5 +73,7 @@ export * from './modules/blocks/useParentRecordCommon';
export { OpenModeProvider, useOpenModeContext } from './modules/popup/OpenModeProvider'; export { OpenModeProvider, useOpenModeContext } from './modules/popup/OpenModeProvider';
export { PopupContextProvider } from './modules/popup/PopupContextProvider'; export { PopupContextProvider } from './modules/popup/PopupContextProvider';
export { usePopupUtils } from './modules/popup/usePopupUtils'; export { usePopupUtils } from './modules/popup/usePopupUtils';
export {
export { VariablePopupRecordProvider } from './modules/variable/variablesProvider/VariablePopupRecordProvider'; VariablePopupRecordProvider,
useCurrentPopupRecord,
} from './modules/variable/variablesProvider/VariablePopupRecordProvider';

View File

@ -14,3 +14,4 @@ export * from './useRoleVariable';
export * from './useURLSearchParamsVariable'; export * from './useURLSearchParamsVariable';
export * from './useUserVariable'; export * from './useUserVariable';
export * from './useVariableOptions'; export * from './useVariableOptions';
export * from './usePopupVariable';

View File

@ -16,3 +16,4 @@ export * from './utils/isVariable';
export * from './utils/transformVariableValue'; export * from './utils/transformVariableValue';
export * from './constants'; export * from './constants';
export type { VariablesContextType } from './types';

View File

@ -10,15 +10,22 @@
import React, { memo, useContext, useEffect, useMemo, useRef } from 'react'; import React, { memo, useContext, useEffect, useMemo, useRef } from 'react';
import { createForm, onFieldInit, onFieldMount, onFieldUnmount } from '@formily/core'; import { createForm, onFieldInit, onFieldMount, onFieldUnmount } from '@formily/core';
import { ChartFilterContext } from './FilterProvider'; import { ChartFilterContext } from './FilterProvider';
import { DEFAULT_DATA_SOURCE_KEY, FormV2, VariablesContext } from '@nocobase/client'; import {
DEFAULT_DATA_SOURCE_KEY,
FormV2,
VariablesContext,
VariablesContextType,
useLocalVariables,
} from '@nocobase/client';
import { setDefaultValue } from './utils'; import { setDefaultValue } from './utils';
import { useChartFilter } from '../hooks'; import { useChartFilter } from '../hooks';
export const ChartFilterForm: React.FC = memo((props) => { export const ChartFilterForm: React.FC = memo((props) => {
const { setField, removeField, setForm } = useContext(ChartFilterContext); const { setField, removeField, setForm } = useContext(ChartFilterContext);
const { getTranslatedTitle } = useChartFilter(); const { getTranslatedTitle } = useChartFilter();
const variables = useRef<any>(null); const variables = useRef<VariablesContextType>(null);
variables.current = useContext(VariablesContext); variables.current = useContext(VariablesContext);
const localVariables = useLocalVariables();
const form = useMemo( const form = useMemo(
() => () =>
createForm({ createForm({
@ -53,7 +60,7 @@ export const ChartFilterForm: React.FC = memo((props) => {
} }
// parse default value // parse default value
setDefaultValue(field, variables.current); setDefaultValue(field, variables.current, localVariables);
}); });
onFieldUnmount('*', (field: any) => { onFieldUnmount('*', (field: any) => {
const name = getField(field); const name = getField(field);
@ -64,7 +71,7 @@ export const ChartFilterForm: React.FC = memo((props) => {
}); });
}, },
}), }),
[setField, getTranslatedTitle, removeField, variables], [setField, getTranslatedTitle, removeField, variables, localVariables],
); );
useEffect(() => setForm(form), [form, setForm]); useEffect(() => setForm(form), [form, setForm]);

View File

@ -27,6 +27,7 @@ import {
SchemaSettingsDataScope, SchemaSettingsDataScope,
removeNullCondition, removeNullCondition,
useFormBlockContext, useFormBlockContext,
useLocalVariables,
} from '@nocobase/client'; } from '@nocobase/client';
import { useChartsTranslation } from '../locale'; import { useChartsTranslation } from '../locale';
import { Schema, useField, useFieldSchema } from '@formily/react'; import { Schema, useField, useFieldSchema } from '@formily/react';
@ -222,6 +223,7 @@ const EditDefaultValue = () => {
const { t } = useChartsTranslation(); const { t } = useChartsTranslation();
const { dn } = useDesignable(); const { dn } = useDesignable();
const variables = useContext(VariablesContext); const variables = useContext(VariablesContext);
const localVariables = useLocalVariables();
const field = useField<Field>(); const field = useField<Field>();
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const { getTranslatedTitle } = useChartFilter(); const { getTranslatedTitle } = useChartFilter();
@ -257,7 +259,7 @@ const EditDefaultValue = () => {
}, },
}); });
dn.refresh(); dn.refresh();
setDefaultValue(field, variables); setDefaultValue(field, variables, localVariables);
}} }}
/> />
); );

View File

@ -7,7 +7,7 @@
* For more information, please refer to: https://www.nocobase.com/agreement. * For more information, please refer to: https://www.nocobase.com/agreement.
*/ */
import { DEFAULT_DATA_SOURCE_KEY } from '@nocobase/client'; import { DEFAULT_DATA_SOURCE_KEY, VariablesContextType } from '@nocobase/client';
import { moment2str } from '@nocobase/utils/client'; import { moment2str } from '@nocobase/utils/client';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import { Schema } from '@formily/react'; import { Schema } from '@formily/react';
@ -170,16 +170,16 @@ export const transformValue = (value: any, props: any) => {
return value; return value;
}; };
export const setDefaultValue = async (field: any, variables: any) => { export const setDefaultValue = async (field: any, variablesCtx: VariablesContextType, localVariables?: any) => {
const defaultValue = field.initialValue; const defaultValue = field.initialValue;
const isVariable = const isVariable =
typeof defaultValue === 'string' && defaultValue?.startsWith('{{$') && defaultValue?.endsWith('}}'); typeof defaultValue === 'string' && defaultValue?.startsWith('{{$') && defaultValue?.endsWith('}}');
if (!isVariable || !variables) { if (!isVariable || !variablesCtx) {
field.setValue(defaultValue); field.setValue(defaultValue);
field.setInitialValue(defaultValue); field.setInitialValue(defaultValue);
} else { } else {
field.loading = true; field.loading = true;
const { value } = await variables.parseVariable(defaultValue); const { value } = await variablesCtx.parseVariable(defaultValue, localVariables);
const transformedValue = transformValue(value, field.componentProps); const transformedValue = transformValue(value, field.componentProps);
field.setValue(transformedValue); field.setValue(transformedValue);
field.setInitialValue(transformedValue); field.setInitialValue(transformedValue);

View File

@ -18,6 +18,7 @@ import {
useCollectionManager_deprecated, useCollectionManager_deprecated,
useDataSourceManager, useDataSourceManager,
useVariables, useVariables,
useLocalVariables,
} from '@nocobase/client'; } from '@nocobase/client';
import { flatten, parse, unflatten } from '@nocobase/utils/client'; import { flatten, parse, unflatten } from '@nocobase/utils/client';
import { useMemoizedFn } from 'ahooks'; import { useMemoizedFn } from 'ahooks';
@ -103,6 +104,7 @@ export const useChartFilter = () => {
const action = fieldSchema?.['x-action']; const action = fieldSchema?.['x-action'];
const { fields: fieldProps, form } = useContext(ChartFilterContext); const { fields: fieldProps, form } = useContext(ChartFilterContext);
const variables = useVariables(); const variables = useVariables();
const localVariables = useLocalVariables();
const getChartFilterFields = ({ const getChartFilterFields = ({
dataSource, dataSource,
@ -418,7 +420,7 @@ export const useChartFilter = () => {
if (['$user', '$date', '$nDate', '$nRole', '$nFilter'].some((n) => value.includes(n))) { if (['$user', '$date', '$nDate', '$nRole', '$nFilter'].some((n) => value.includes(n))) {
return value; return value;
} }
const result = variables?.parseVariable(value).then(({ value }) => value); const result = variables?.parseVariable(value, localVariables).then(({ value }) => value);
return result; return result;
}, },
}); });

View File

@ -13,6 +13,7 @@ import {
useCurrentRoleVariable, useCurrentRoleVariable,
useCurrentUserVariable, useCurrentUserVariable,
useDatetimeVariable, useDatetimeVariable,
usePopupVariable,
useURLSearchParamsVariable, useURLSearchParamsVariable,
} from '@nocobase/client'; } from '@nocobase/client';
import { useMemo } from 'react'; import { useMemo } from 'react';
@ -32,13 +33,29 @@ export const useGeneralVariableOptions = (
const { apiTokenSettings } = useAPITokenVariable({ noDisabled: true }); const { apiTokenSettings } = useAPITokenVariable({ noDisabled: true });
const { datetimeSettings } = useDatetimeVariable({ operator, schema, noDisabled: true }); const { datetimeSettings } = useDatetimeVariable({ operator, schema, noDisabled: true });
const { urlSearchParamsSettings } = useURLSearchParamsVariable(); const { urlSearchParamsSettings } = useURLSearchParamsVariable();
const { settings: popupRecordSettings, shouldDisplayPopupRecord } = usePopupVariable({
schema,
});
const result = useMemo( const result = useMemo(
() => () =>
[currentUserSettings, currentRoleSettings, apiTokenSettings, datetimeSettings, urlSearchParamsSettings].filter( [
Boolean, currentUserSettings,
), currentRoleSettings,
[datetimeSettings, currentUserSettings, currentRoleSettings, urlSearchParamsSettings, apiTokenSettings], apiTokenSettings,
datetimeSettings,
urlSearchParamsSettings,
shouldDisplayPopupRecord && popupRecordSettings,
].filter(Boolean),
[
datetimeSettings,
currentUserSettings,
currentRoleSettings,
urlSearchParamsSettings,
apiTokenSettings,
shouldDisplayPopupRecord,
popupRecordSettings,
],
); );
if (!schema) return []; if (!schema) return [];

View File

@ -69,6 +69,10 @@ class PluginDataVisualiztionClient extends Plugin {
title: lang('Charts'), title: lang('Charts'),
Component: 'ChartV2BlockInitializer', Component: 'ChartV2BlockInitializer',
}); });
this.app.schemaInitializerManager.addItem('popup:common:addBlock', 'dataBlocks.charts', {
title: '{{t("Charts")}}',
Component: 'ChartV2BlockInitializer',
});
} }
} }