forked from patternfly/react-component-groups
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldBuilderExample.tsx
More file actions
104 lines (94 loc) · 3.56 KB
/
FieldBuilderExample.tsx
File metadata and controls
104 lines (94 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import React, { useState } from 'react';
import {
Form,
TextInput,
} from '@patternfly/react-core';
import { FieldBuilder } from '@patternfly/react-component-groups/dist/dynamic/FieldBuilder';
interface Contact {
name: string;
email: string;
}
export const FieldBuilderExample: React.FunctionComponent = () => {
const [ contacts, setContacts ] = useState<Contact[]>([
{ name: '', email: '' }
]);
// Handle adding a new contact row
const handleAddContact = (event: React.MouseEvent) => {
// eslint-disable-next-line no-console
console.log('Add button clicked:', event.currentTarget);
const newContacts = [ ...contacts, { name: '', email: '' } ];
setContacts(newContacts);
};
// Handle removing a contact row
const handleRemoveContact = (event: React.MouseEvent, index: number) => {
// eslint-disable-next-line no-console
console.log('Remove button clicked:', event.currentTarget, 'for index:', index);
const newContacts = contacts.filter((_, i) => i !== index);
setContacts(newContacts);
};
// Handle updating contact data
const handleContactChange = (index: number, field: keyof Contact, value: string) => {
const updatedContacts = [ ...contacts ];
updatedContacts[index] = { ...updatedContacts[index], [field]: value };
setContacts(updatedContacts);
};
// Custom announcement for adding rows
const customAddAnnouncement = (rowNumber: number, rowGroupLabelPrefix: string) => `New ${rowGroupLabelPrefix.toLowerCase()} ${rowNumber} added.`;
// Custom announcement for removing rows
const customRemoveAnnouncement = (rowNumber: number, rowGroupLabelPrefix: string) => {
const removedIndex = rowNumber - 1;
const removedContact = contacts[removedIndex];
if (removedContact?.name) {
return `Removed ${rowGroupLabelPrefix.toLowerCase()} ${removedContact.name}.`;
}
return `${rowGroupLabelPrefix} ${rowNumber} removed.`;
};
// Custom aria-label for remove buttons
const customRemoveAriaLabel = (rowNumber: number, rowGroupLabelPrefix: string) => {
const contactIndex = rowNumber - 1;
const contact = contacts[contactIndex];
if (contact?.name) {
return `Remove ${rowGroupLabelPrefix.toLowerCase()} ${contact.name}`;
}
return `Remove ${rowGroupLabelPrefix.toLowerCase()} in row ${rowNumber}`;
};
return (
<Form>
<FieldBuilder
isRequired
firstColumnLabel="Name"
secondColumnLabel="Email"
rowCount={contacts.length}
onAddRow={handleAddContact}
onRemoveRow={handleRemoveContact}
onAddRowAnnouncement={customAddAnnouncement}
onRemoveRowAnnouncement={customRemoveAnnouncement}
removeButtonAriaLabel={customRemoveAriaLabel}
addButtonContent="Add contact"
>
{({ focusRef, firstColumnAriaLabel, secondColumnAriaLabel }, index) => [
<TextInput
key="name"
ref={focusRef}
type="text"
value={contacts[index]?.name || ''}
placeholder="Enter full name"
onChange={(_event, value) => handleContactChange(index, 'name', value)}
aria-label={firstColumnAriaLabel}
isRequired
/>,
<TextInput
key="email"
type="email"
value={contacts[index]?.email || ''}
placeholder="name@example.com"
onChange={(_event, value) => handleContactChange(index, 'email', value)}
aria-label={secondColumnAriaLabel}
isRequired
/>
]}
</FieldBuilder>
</Form>
);
};
export default FieldBuilderExample;