Skip to content

Commit 4274f69

Browse files
committed
HasItemsWithIdentifier and ComponentContext is now implemented by FormFormGroup
1 parent 49e7299 commit 4274f69

File tree

9 files changed

+191
-92
lines changed

9 files changed

+191
-92
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1212
- Add `TreeViewItem.reload()`
1313
- Add `TreeViewItem.parent()`
1414
- Add `HasItems.contains(String identifier)`
15+
- `HasItems``WithIdentifier` and `ComponentContext` is now implemented by `Form``FormGroup`
1516

1617
### Changed
1718

19+
- Replaced `FormGroup.fieldId(String id)` with `FormGroup.formGroup(String identifier)`
1820
- Replace yarn with npm
1921

2022
### Fixed

components/src/main/java/org/patternfly/component/form/Form.java

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,20 @@
1515
*/
1616
package org.patternfly.component.form;
1717

18+
import java.util.Iterator;
19+
import java.util.LinkedHashMap;
20+
import java.util.Map;
21+
import java.util.function.Function;
22+
1823
import org.jboss.elemento.Elements;
1924
import org.patternfly.component.BaseComponent;
2025
import org.patternfly.component.ComponentType;
21-
import org.patternfly.style.Modifiers;
26+
import org.patternfly.component.HasItems;
27+
import org.patternfly.style.Modifiers.Horizontal;
2228

2329
import elemental2.dom.HTMLFormElement;
2430

31+
import static org.jboss.elemento.Elements.failSafeRemoveFromParent;
2532
import static org.patternfly.style.Classes.component;
2633
import static org.patternfly.style.Classes.form;
2734
import static org.patternfly.style.Classes.limitWidth;
@@ -34,7 +41,9 @@
3441
*
3542
* @see <a href= "https://www.patternfly.org/components/forms/form">https://www.patternfly.org/components/forms/form</a>
3643
*/
37-
public class Form extends BaseComponent<HTMLFormElement, Form> implements Modifiers.Horizontal<HTMLFormElement, Form> {
44+
public class Form extends BaseComponent<HTMLFormElement, Form> implements
45+
HasItems<HTMLFormElement, Form, FormGroup>,
46+
Horizontal<HTMLFormElement, Form> {
3847

3948
// ------------------------------------------------------ factory
4049

@@ -44,13 +53,38 @@ public static Form form() {
4453

4554
// ------------------------------------------------------ instance
4655

56+
private final Map<String, FormGroup> items;
57+
4758
Form() {
4859
super(ComponentType.Form, Elements.form().css(component(form)).apply(f -> f.noValidate = true).element());
60+
this.items = new LinkedHashMap<>();
4961
storeComponent();
5062
}
5163

5264
// ------------------------------------------------------ add
5365

66+
public <T> Form addItems(Iterable<T> items, Function<T, FormGroup> display) {
67+
for (T item : items) {
68+
FormGroup group = display.apply(item);
69+
addItem(group);
70+
}
71+
return this;
72+
}
73+
74+
public Form addItem(FormGroup item) {
75+
return add(item);
76+
}
77+
78+
public Form addGroup(FormGroup group) {
79+
return add(group);
80+
}
81+
82+
@Override
83+
public Form add(FormGroup item) {
84+
items.put(item.identifier(), item);
85+
return add(item.element());
86+
}
87+
5488
public Form addAlert(FormAlert alert) {
5589
return add(alert);
5690
}
@@ -63,10 +97,6 @@ public Form addFieldGroup(FormFieldGroup fieldGroup) {
6397
return add(fieldGroup);
6498
}
6599

66-
public Form addGroup(FormGroup group) {
67-
return add(group);
68-
}
69-
70100
public Form addActionGroup(FormActionGroup actionGroup) {
71101
return add(actionGroup);
72102
}
@@ -81,4 +111,34 @@ public Form limitWidth() {
81111
public Form that() {
82112
return this;
83113
}
114+
115+
// ------------------------------------------------------ api
116+
117+
@Override
118+
public Iterator<FormGroup> iterator() {
119+
return items.values().iterator();
120+
}
121+
122+
@Override
123+
public int size() {
124+
return items.size();
125+
}
126+
127+
@Override
128+
public boolean isEmpty() {
129+
return items.isEmpty();
130+
}
131+
132+
@Override
133+
public boolean contains(String identifier) {
134+
return items.containsKey(identifier);
135+
}
136+
137+
@Override
138+
public void clear() {
139+
for (FormGroup group : items.values()) {
140+
failSafeRemoveFromParent(group);
141+
}
142+
items.clear();
143+
}
84144
}

components/src/main/java/org/patternfly/component/form/FormGroup.java

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,56 @@
1515
*/
1616
package org.patternfly.component.form;
1717

18-
import org.jboss.elemento.Attachable;
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
import org.jboss.elemento.Id;
1922
import org.jboss.elemento.logger.Logger;
23+
import org.patternfly.component.ComponentType;
24+
import org.patternfly.component.WithIdentifier;
2025
import org.patternfly.core.Attributes;
26+
import org.patternfly.core.ComponentContext;
27+
import org.patternfly.core.Dataset;
2128
import org.patternfly.style.Classes;
2229

2330
import elemental2.dom.HTMLElement;
24-
import elemental2.dom.MutationRecord;
2531

2632
import static org.jboss.elemento.Elements.div;
2733
import static org.patternfly.style.Classes.component;
2834
import static org.patternfly.style.Classes.group;
2935

3036
public class FormGroup extends FormSubComponent<HTMLElement, FormGroup> implements
31-
Attachable {
37+
WithIdentifier<HTMLElement, FormGroup>,
38+
ComponentContext<HTMLElement, FormGroup> {
3239

3340
// ------------------------------------------------------ factory
3441

3542
public static FormGroup formGroup() {
36-
return new FormGroup();
43+
return new FormGroup(Id.unique(ComponentType.Form.id, SUB_COMPONENT_NAME));
44+
}
45+
46+
public static FormGroup formGroup(String identifier) {
47+
return new FormGroup(identifier);
3748
}
3849

3950
// ------------------------------------------------------ instance
4051

4152
private static final Logger logger = Logger.getLogger(FormGroup.class.getName());
4253
static final String SUB_COMPONENT_NAME = "fg";
4354

44-
String fieldId;
4555
boolean required;
4656
FormGroupRole role;
47-
48-
FormGroup() {
49-
super(SUB_COMPONENT_NAME, div().css(component(Classes.form, group)).element());
50-
this.fieldId = null;
57+
private final String identifier;
58+
private final Map<String, Object> data;
59+
60+
FormGroup(String identifier) {
61+
super(SUB_COMPONENT_NAME, div().css(component(Classes.form, group))
62+
.data(Dataset.identifier, identifier)
63+
.element());
64+
this.identifier = identifier;
65+
this.data = new HashMap<>();
5166
this.required = false;
5267
storeSubComponent();
53-
Attachable.register(this, this);
54-
}
55-
56-
@Override
57-
public void attach(MutationRecord mutationRecord) {
58-
if ((role == FormGroupRole.radiogroup || role == FormGroupRole.group) && fieldId == null) {
59-
logger.error("Missing field ID for form group %o with role '%s'.", element(), role.name());
60-
}
6168
}
6269

6370
// ------------------------------------------------------ add
@@ -72,13 +79,12 @@ public FormGroup addControl(FormGroupControl control) {
7279

7380
// ------------------------------------------------------ builder
7481

75-
public FormGroup fieldId(String id) {
76-
this.fieldId = id;
77-
return this;
82+
public FormGroup required() {
83+
return required(true);
7884
}
7985

80-
public FormGroup required() {
81-
this.required = true;
86+
public FormGroup required(boolean required) {
87+
this.required = required;
8288
return this;
8389
}
8490

@@ -97,8 +103,35 @@ public FormGroup role(FormGroupRole role) {
97103
return this;
98104
}
99105

106+
@Override
107+
public <T> FormGroup store(String key, T value) {
108+
data.put(key, value);
109+
return this;
110+
}
111+
100112
@Override
101113
public FormGroup that() {
102114
return this;
103115
}
116+
117+
// ------------------------------------------------------ api
118+
119+
@Override
120+
public String identifier() {
121+
return identifier;
122+
}
123+
124+
@Override
125+
public boolean has(String key) {
126+
return data.containsKey(key);
127+
}
128+
129+
@Override
130+
@SuppressWarnings("unchecked")
131+
public <T> T get(String key) {
132+
if (data.containsKey(key)) {
133+
return (T) data.get(key);
134+
}
135+
return null;
136+
}
104137
}

components/src/main/java/org/patternfly/component/form/FormGroupControl.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,16 @@ public static FormGroupControl formGroupControl() {
6767
public void attach(MutationRecord mutationRecord) {
6868
FormGroup formGroup = lookupSubComponent(FormGroup.SUB_COMPONENT_NAME);
6969

70-
if (formGroup.fieldId != null && control != null && !formGroup.fieldId.equals(control.id)) {
71-
logger.error("The field id of the form group %o is different from the id of its control %o: '%s' != '%s'",
72-
formGroup.element(), element(), formGroup.fieldId, control.id);
70+
if (control != null && !formGroup.identifier().equals(control.id)) {
71+
logger.error("The identifier of the form group %o is different from the id of its control %o: '%s' != '%s'",
72+
formGroup.element(), element(), formGroup.identifier(), control.id);
7373
}
7474

75-
if (formGroup.fieldId != null) {
76-
for (Checkbox checkbox : checkboxes) {
77-
checkbox.inputElement().name(formGroup.fieldId);
78-
}
79-
for (Radio radio : radios) {
80-
radio.inputElement().name(formGroup.fieldId);
81-
}
75+
for (Checkbox checkbox : checkboxes) {
76+
checkbox.inputElement().name(formGroup.identifier());
77+
}
78+
for (Radio radio : radios) {
79+
radio.inputElement().name(formGroup.identifier());
8280
}
8381
}
8482

components/src/main/java/org/patternfly/component/form/FormGroupLabel.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.jboss.elemento.ButtonType;
2222
import org.jboss.elemento.Elements;
2323
import org.jboss.elemento.Id;
24-
import org.patternfly.component.ComponentType;
2524
import org.patternfly.component.popover.Popover;
2625
import org.patternfly.core.Aria;
2726
import org.patternfly.icon.IconSets;
@@ -85,12 +84,12 @@ public void attach(MutationRecord mutationRecord) {
8584
if (element().id != null && !element().id.isEmpty()) {
8685
formGroup.aria(Aria.labelledBy, element().id);
8786
} else {
88-
String labelId = Id.build(formGroup.fieldId != null ? formGroup.fieldId : ComponentType.Form.id, "label");
87+
String labelId = Id.build(formGroup.identifier(), "label");
8988
id(labelId);
9089
formGroup.aria(Aria.labelledBy, labelId);
9190
}
92-
} else if (formGroup.fieldId != null) {
93-
((HTMLLabelElement) labelElement).htmlFor = formGroup.fieldId;
91+
} else {
92+
((HTMLLabelElement) labelElement).htmlFor = formGroup.identifier();
9493
}
9594
if (formGroup.required) {
9695
labelElement.appendChild(span().css(component(Classes.form, Classes.label, Classes.required))

components/src/main/java/org/patternfly/component/list/List.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
import static org.jboss.elemento.Elements.removeChildrenFrom;
3636
import static org.jboss.elemento.Elements.ul;
37+
import static org.patternfly.component.divider.Divider.divider;
38+
import static org.patternfly.component.divider.DividerType.li;
3739
import static org.patternfly.core.Attributes.role;
3840
import static org.patternfly.style.Classes.icon;
3941
import static org.patternfly.style.Classes.list;
@@ -93,6 +95,10 @@ public List add(ListItem item) {
9395
return add(item.element());
9496
}
9597

98+
public List addDivider() {
99+
return add(divider(li));
100+
}
101+
96102
// ------------------------------------------------------ builder
97103

98104
public List largeIcons() {

core/src/main/java/org/patternfly/filter/Filter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ public Filter(FilterOperator operator) {
6060

6161
// ------------------------------------------------------ api
6262

63-
public <V> void add(FilterAttribute<T, V> attribute) {
63+
public <V> Filter<T> add(FilterAttribute<T, V> attribute) {
6464
attributes.put(attribute.name, attribute);
65+
return this;
6566
}
6667

6768
@Override

0 commit comments

Comments
 (0)