|
8 | 8 | import React from 'react'; |
9 | 9 | import { EuiButtonEmpty, EuiFlexGroup, EuiFlexItem, EuiFormRow, EuiSpacer } from '@elastic/eui'; |
10 | 10 | import { UseArray, useFormData } from '../../../../shared_imports'; |
| 11 | +import type { FormHook, ArrayItem } from '../../../../shared_imports'; |
11 | 12 | import { RelatedIntegrationsHelpInfo } from './related_integrations_help_info'; |
12 | 13 | import { RelatedIntegrationFieldRow } from './related_integration_field_row'; |
13 | 14 | import * as i18n from './translations'; |
14 | 15 | import { OptionalFieldLabel } from '../optional_field_label'; |
| 16 | +import { getFlattenedArrayFieldNames } from '../utils'; |
15 | 17 |
|
16 | 18 | interface RelatedIntegrationsProps { |
17 | 19 | path: string; |
18 | 20 | dataTestSubj?: string; |
19 | 21 | } |
20 | 22 |
|
21 | | -export function RelatedIntegrations({ path, dataTestSubj }: RelatedIntegrationsProps): JSX.Element { |
| 23 | +function RelatedIntegrationsComponent({ |
| 24 | + path, |
| 25 | + dataTestSubj, |
| 26 | +}: RelatedIntegrationsProps): JSX.Element { |
| 27 | + return ( |
| 28 | + <UseArray path={path} initialNumberOfItems={0}> |
| 29 | + {({ items, addItem, removeItem, form }) => ( |
| 30 | + <RelatedIntegrationsList |
| 31 | + items={items} |
| 32 | + addItem={addItem} |
| 33 | + removeItem={removeItem} |
| 34 | + path={path} |
| 35 | + form={form} |
| 36 | + dataTestSubj={dataTestSubj} |
| 37 | + /> |
| 38 | + )} |
| 39 | + </UseArray> |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +interface RelatedIntegrationsListProps { |
| 44 | + items: ArrayItem[]; |
| 45 | + addItem: () => void; |
| 46 | + removeItem: (id: number) => void; |
| 47 | + path: string; |
| 48 | + form: FormHook; |
| 49 | + dataTestSubj?: string; |
| 50 | +} |
| 51 | + |
| 52 | +const RelatedIntegrationsList = ({ |
| 53 | + items, |
| 54 | + addItem, |
| 55 | + removeItem, |
| 56 | + path, |
| 57 | + form, |
| 58 | + dataTestSubj, |
| 59 | +}: RelatedIntegrationsListProps) => { |
| 60 | + const flattenedFieldNames = getFlattenedArrayFieldNames(form, path); |
| 61 | + |
| 62 | + /* |
| 63 | + Not using "watch" for the initial render, to let row components render and initialize form fields. |
| 64 | + Then we can use the "watch" feature to track their changes. |
| 65 | + */ |
| 66 | + const hasRenderedInitially = flattenedFieldNames.length > 0; |
| 67 | + const fieldsToWatch = hasRenderedInitially ? flattenedFieldNames : []; |
| 68 | + |
| 69 | + const [formData] = useFormData({ watch: fieldsToWatch }); |
| 70 | + |
22 | 71 | const label = ( |
23 | 72 | <> |
24 | 73 | {i18n.RELATED_INTEGRATIONS_LABEL} |
25 | 74 | <RelatedIntegrationsHelpInfo /> |
26 | 75 | </> |
27 | 76 | ); |
28 | | - const [formData] = useFormData(); |
29 | 77 |
|
30 | 78 | return ( |
31 | | - <UseArray path={path} initialNumberOfItems={0}> |
32 | | - {({ items, addItem, removeItem }) => ( |
33 | | - <EuiFormRow |
34 | | - label={label} |
35 | | - labelAppend={OptionalFieldLabel} |
36 | | - labelType="legend" |
37 | | - fullWidth |
38 | | - data-test-subj={dataTestSubj} |
39 | | - hasChildLabel={false} |
40 | | - > |
41 | | - <> |
42 | | - <EuiFlexGroup direction="column" gutterSize="s"> |
43 | | - {items.map((item) => ( |
44 | | - <EuiFlexItem key={item.id} data-test-subj="relatedIntegrationRow"> |
45 | | - <RelatedIntegrationFieldRow |
46 | | - item={item} |
47 | | - relatedIntegrations={formData[path] ?? []} |
48 | | - removeItem={removeItem} |
49 | | - /> |
50 | | - </EuiFlexItem> |
51 | | - ))} |
52 | | - </EuiFlexGroup> |
53 | | - {items.length > 0 && <EuiSpacer size="s" />} |
54 | | - <EuiButtonEmpty size="xs" iconType="plusInCircle" onClick={addItem}> |
55 | | - {i18n.ADD_INTEGRATION} |
56 | | - </EuiButtonEmpty> |
57 | | - </> |
58 | | - </EuiFormRow> |
59 | | - )} |
60 | | - </UseArray> |
| 79 | + <EuiFormRow |
| 80 | + label={label} |
| 81 | + labelAppend={OptionalFieldLabel} |
| 82 | + labelType="legend" |
| 83 | + fullWidth |
| 84 | + data-test-subj={dataTestSubj} |
| 85 | + hasChildLabel={false} |
| 86 | + > |
| 87 | + <> |
| 88 | + <EuiFlexGroup direction="column" gutterSize="s"> |
| 89 | + {items.map((item) => ( |
| 90 | + <EuiFlexItem key={item.id} data-test-subj="relatedIntegrationRow"> |
| 91 | + <RelatedIntegrationFieldRow |
| 92 | + item={item} |
| 93 | + relatedIntegrations={formData[path] ?? []} |
| 94 | + removeItem={removeItem} |
| 95 | + /> |
| 96 | + </EuiFlexItem> |
| 97 | + ))} |
| 98 | + </EuiFlexGroup> |
| 99 | + {items.length > 0 && <EuiSpacer size="s" />} |
| 100 | + <EuiButtonEmpty size="xs" iconType="plusInCircle" onClick={addItem}> |
| 101 | + {i18n.ADD_INTEGRATION} |
| 102 | + </EuiButtonEmpty> |
| 103 | + </> |
| 104 | + </EuiFormRow> |
61 | 105 | ); |
62 | | -} |
| 106 | +}; |
| 107 | + |
| 108 | +export const RelatedIntegrations = React.memo(RelatedIntegrationsComponent); |
0 commit comments