|
1 | | -import React from 'react' |
| 1 | +import React, { useState } from 'react' |
2 | 2 | import { FrIconClassName, RiIconClassName } from '@codegouvfr/react-dsfr' |
3 | 3 | import Button from '@codegouvfr/react-dsfr/Button' |
4 | 4 | import Tabs from '@codegouvfr/react-dsfr/Tabs' |
@@ -30,59 +30,69 @@ export default function ({ |
30 | 30 | items, |
31 | 31 | canAdd, |
32 | 32 | onAddClick, |
| 33 | + registry, |
33 | 34 | }: ArrayFieldTemplateProps & { uiSchema?: UiSchemaDSFR }) { |
| 35 | + const [selectedTabId, setSelectedTabId] = useState('tab0') |
34 | 36 | const tabLabel = uiSchema?.['ui:tabLabel'] ?? 'Element' |
35 | 37 | const removeIcon = uiSchema?.['ui:removeIcon'] ?? 'fr-icon-delete-line' |
36 | 38 | const addIcon = uiSchema?.['ui:addIcon'] ?? 'fr-icon-add-circle-line' |
37 | | - console.log('items', items) |
| 39 | + |
| 40 | + // ensure no exception when last selected tab has been destroyed |
| 41 | + const selectedIndex = Math.min( |
| 42 | + items.length - 1, |
| 43 | + parseInt(selectedTabId.replace(/^tab(\d+)$/, '$1')), |
| 44 | + ) |
| 45 | + |
| 46 | + const tabContent = |
| 47 | + (items.length && ( |
| 48 | + <> |
| 49 | + <Button |
| 50 | + type="button" |
| 51 | + iconId={removeIcon} |
| 52 | + onClick={(e) => { |
| 53 | + items[selectedIndex].onDropIndexClick(selectedIndex)() |
| 54 | + }} |
| 55 | + size="small" |
| 56 | + priority="secondary" |
| 57 | + > |
| 58 | + Supprimer |
| 59 | + </Button> |
| 60 | + {items[selectedIndex].children} |
| 61 | + </> |
| 62 | + )) || |
| 63 | + null |
| 64 | + |
| 65 | + const onTabChange = (id: string) => { |
| 66 | + if (id === 'add') { |
| 67 | + onAddClick() |
| 68 | + setSelectedTabId(`tab${items.length}`) |
| 69 | + return |
| 70 | + } |
| 71 | + setSelectedTabId(id) |
| 72 | + } |
| 73 | + |
38 | 74 | return ( |
39 | 75 | <div className="form-group field"> |
40 | 76 | <div className="fr-input-group"> |
41 | 77 | <label className="fr-label">{title}</label> |
42 | 78 | <div className="fr-input-wrap"> |
43 | 79 | <Tabs |
| 80 | + onTabChange={onTabChange} |
| 81 | + selectedTabId={selectedTabId} |
44 | 82 | tabs={items |
45 | 83 | .map((element) => ({ |
46 | 84 | label: `${tabLabel} ${element.index + 1}`, |
47 | | - content: ( |
48 | | - <> |
49 | | - <Button |
50 | | - type="button" |
51 | | - iconId={removeIcon} |
52 | | - onClick={(e) => { |
53 | | - console.log('io', element) |
54 | | - element.onDropIndexClick(element.index) |
55 | | - }} |
56 | | - size="small" |
57 | | - priority="secondary" |
58 | | - > |
59 | | - Supprimer |
60 | | - </Button> |
61 | | - {element.children} |
62 | | - </> |
63 | | - ), |
| 85 | + tabId: `tab${element.index}`, |
64 | 86 | })) |
65 | 87 | .concat([ |
66 | 88 | { |
67 | 89 | label: `Ajouter`, |
68 | | - content: ( |
69 | | - <> |
70 | | - {canAdd && ( |
71 | | - <Button |
72 | | - type="button" |
73 | | - iconId={addIcon} |
74 | | - onClick={onAddClick} |
75 | | - size="small" |
76 | | - priority="secondary" |
77 | | - > |
78 | | - Ajouter |
79 | | - </Button> |
80 | | - )} |
81 | | - </> |
82 | | - ), |
| 90 | + tabId: 'add', |
83 | 91 | }, |
84 | 92 | ])} |
85 | | - /> |
| 93 | + > |
| 94 | + {selectedTabId !== 'add' && tabContent} |
| 95 | + </Tabs> |
86 | 96 | </div> |
87 | 97 | </div> |
88 | 98 | </div> |
|
0 commit comments