@boarteam/boar-pack-common-frontend 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@boarteam/boar-pack-common-frontend",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Common frontend package for Boar Pack",
5
5
  "repository": "git@github.com:boarteam/boar-pack.git",
6
6
  "author": "Andrew Balakirev <balakirev.andrey@gmail.com>",
@@ -47,5 +47,5 @@
47
47
  "scripts": {
48
48
  "yalc:push": "yalc push"
49
49
  },
50
- "gitHead": "36339bec16a3d1966bab8df4e10f3fa0ad49f4e4"
50
+ "gitHead": "008e1d42d47532005e1af1909d40befdebff5197"
51
51
  }
@@ -1,6 +1,6 @@
1
1
  import { ActionType } from "@ant-design/pro-table";
2
2
  import React, { Key, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
3
- import { Badge, Button, Result, Tabs, TabsProps, Tooltip } from "antd";
3
+ import { Badge, Button, Form, Result, Tabs, TabsProps, Tooltip } from "antd";
4
4
  import { DeleteOutlined, StopOutlined } from "@ant-design/icons";
5
5
  import { FormattedMessage, useIntl } from "react-intl";
6
6
  import { DescriptionsRefType, FieldsEdit, TDescriptionsProps, TGetOneParams } from "./descriptionTypes";
@@ -21,7 +21,7 @@ import { debounce } from "lodash";
21
21
  import { NamePath } from "antd/lib/form/interface";
22
22
  import { FieldData } from "rc-field-form/lib/interface";
23
23
 
24
- const useStyles = createStyles(({css}) => {
24
+ const useStyles = createStyles(({ css }) => {
25
25
  return {
26
26
  antDescriptionsStyles: css`
27
27
  .ant-descriptions-item-content {
@@ -62,6 +62,7 @@ const DescriptionsComponent = <Entity extends Record<string | symbol, any>,
62
62
  columns,
63
63
  params,
64
64
  onEntityChange,
65
+ conditionalFieldsConfig,
65
66
  ...rest
66
67
  }: TDescriptionsProps<Entity,
67
68
  CreateDto,
@@ -80,13 +81,36 @@ const DescriptionsComponent = <Entity extends Record<string | symbol, any>,
80
81
  }
81
82
  form = editable.form;
82
83
 
84
+ const watchedValue = conditionalFieldsConfig
85
+ ? Form.useWatch(conditionalFieldsConfig.field, form)
86
+ : undefined;
87
+
88
+ const filteredColumns = React.useMemo(() => {
89
+ if (!conditionalFieldsConfig) {
90
+ return columns;
91
+ }
92
+
93
+ const { field, deps } = conditionalFieldsConfig;
94
+ const depsList = deps[watchedValue as string];
95
+
96
+ if (!depsList?.length) {
97
+ return columns;
98
+ }
99
+
100
+ // Always include the watched field itself
101
+ return columns.filter(col => {
102
+ const idx = col.dataIndex as string;
103
+ return idx === field || depsList.includes(idx);
104
+ });
105
+ }, [columns, watchedValue, conditionalFieldsConfig]);
106
+
83
107
  const actionRefComponent = useRef<ActionType>();
84
108
  const actionRef = actionRefProp || actionRefComponent;
85
109
  const intl = useIntl();
86
110
  const [data, setData] = useState<Partial<Entity> | undefined>(entity);
87
111
  const [loading, setLoading] = useState(false);
88
112
 
89
- const sections = columnsToDescriptionItemProps(columns, mainTitle);
113
+ const sections = columnsToDescriptionItemProps(filteredColumns, mainTitle);
90
114
 
91
115
  const columnDataIndexToSection = sections.reduce((acc, section) => {
92
116
  section.columns.forEach(column => {
@@ -35,7 +35,15 @@ export enum FieldsEdit {
35
35
  All = 'all'
36
36
  }
37
37
 
38
+ export interface ConditionalFieldsConfig {
39
+ /** Which field to watch */
40
+ field: string;
41
+ /** Map of watched-value → array of dataIndex you want to show */
42
+ deps: Record<string, string[]>;
43
+ }
44
+
38
45
  export type TDescriptionsProps<Entity, CreateDto, UpdateDto, TPathParams = object> = {
46
+ conditionalFieldsConfig?: ConditionalFieldsConfig;
39
47
  mainTitle?: ProColumns<Entity>['title'] | null,
40
48
  entity?: Partial<Entity>,
41
49
  getOne?: ({}: TGetOneParams & TPathParams) => Promise<Entity | null>,
@@ -1,7 +1,7 @@
1
1
  import { ProColumns } from "@ant-design/pro-components";
2
2
  import { Button, Modal } from "antd";
3
3
  import { MutableRefObject, useRef } from "react";
4
- import { Descriptions, DescriptionsRefType } from "../Descriptions";
4
+ import { ConditionalFieldsConfig, Descriptions, DescriptionsRefType } from "../Descriptions";
5
5
  import { buildFieldsFromColumnsForDescriptionsDisplay } from "./tableTools";
6
6
 
7
7
  export interface CreateEntityModalProps<Entity> {
@@ -24,6 +24,8 @@ export interface CreateEntityModalProps<Entity> {
24
24
  * Receives the validated form data.
25
25
  */
26
26
  onSubmit: (data: any, descriptionsRef: MutableRefObject<DescriptionsRefType<Entity>>) => Promise<void>;
27
+ /** Configuration for conditional fields */
28
+ conditionalFieldsConfig?: ConditionalFieldsConfig
27
29
  }
28
30
 
29
31
  export function CreateEntityModal<
@@ -40,6 +42,7 @@ export function CreateEntityModal<
40
42
  idColumnName,
41
43
  onCancel,
42
44
  onSubmit,
45
+ conditionalFieldsConfig,
43
46
  }: CreateEntityModalProps<Entity>) {
44
47
  const descriptionsRef = useRef<DescriptionsRefType<Entity>>(null);
45
48
 
@@ -79,6 +82,7 @@ export function CreateEntityModal<
79
82
  editableKeys,
80
83
  actionRender: () => [],
81
84
  }}
85
+ conditionalFieldsConfig={conditionalFieldsConfig}
82
86
  />
83
87
  </Modal>
84
88
  );
@@ -55,6 +55,7 @@ const Table = <Entity extends Record<string | symbol, any>,
55
55
  columnsState: managedColumnsState,
56
56
  columnsSetSelect: managedColumnsSetSelect,
57
57
  popupCreation = false,
58
+ conditionalFieldsConfig,
58
59
  toolBarRender,
59
60
  params,
60
61
  editPopupTitle,
@@ -120,6 +121,7 @@ const Table = <Entity extends Record<string | symbol, any>,
120
121
  createButtonSize: rest.size,
121
122
  popupCreation,
122
123
  createNewDefaultParams,
124
+ conditionalFieldsConfig,
123
125
  });
124
126
 
125
127
  const { exportButton, importButton, setLastQueryParams } = useImportExport<TPathParams>({
@@ -6,6 +6,7 @@ import { TColumnsSet } from "./useColumnsSets";
6
6
  import { ColumnStateType } from "@ant-design/pro-table/es/typing";
7
7
  import { RowEditableConfig } from "@ant-design/pro-utils";
8
8
  import { ProColumns } from "@ant-design/pro-components";
9
+ import { ConditionalFieldsConfig } from "../Descriptions";
9
10
 
10
11
  export type IWithId = {
11
12
  id: string | number,
@@ -96,6 +97,7 @@ interface BaseProps<Entity,
96
97
  viewOnly?: boolean;
97
98
  columnsSets?: TColumnsSet<Entity>[];
98
99
  popupCreation?: boolean;
100
+ conditionalFieldsConfig?: ConditionalFieldsConfig;
99
101
  columnsState?: ColumnStateType;
100
102
  columnsSetSelect?: () => React.ReactNode;
101
103
  editPopupTitle?: string;
@@ -32,6 +32,7 @@ export function useCreation<Entity, CreateDto, TPathParams = {}>({
32
32
  createButtonSize,
33
33
  popupCreation,
34
34
  createNewDefaultParams,
35
+ conditionalFieldsConfig,
35
36
  }: {
36
37
  actionRef?: MutableRefObject<ActionType | undefined>;
37
38
  pathParams: TPathParams;
@@ -105,6 +106,7 @@ export function useCreation<Entity, CreateDto, TPathParams = {}>({
105
106
  setCreatePopupData(undefined);
106
107
  }}
107
108
  onSubmit={onCreateSubmit}
109
+ conditionalFieldsConfig={conditionalFieldsConfig}
108
110
  />;
109
111
 
110
112
  return {