org.patternfly.component.form.FormGroup Maven / Gradle / Ivy
/*
* Copyright 2023 Red Hat
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.patternfly.component.form;
import org.jboss.elemento.Attachable;
import org.jboss.elemento.logger.Logger;
import org.patternfly.core.Attributes;
import org.patternfly.style.Classes;
import elemental2.dom.HTMLElement;
import elemental2.dom.MutationRecord;
import static org.jboss.elemento.Elements.div;
import static org.patternfly.style.Classes.component;
import static org.patternfly.style.Classes.group;
public class FormGroup extends FormSubComponent implements
Attachable {
// ------------------------------------------------------ factory
public static FormGroup formGroup() {
return new FormGroup();
}
// ------------------------------------------------------ instance
private static final Logger logger = Logger.getLogger(FormGroup.class.getName());
static final String SUB_COMPONENT_NAME = "fg";
String fieldId;
boolean required;
FormGroupRole role;
FormGroup() {
super(SUB_COMPONENT_NAME, div().css(component(Classes.form, group)).element());
this.fieldId = null;
this.required = false;
storeSubComponent();
Attachable.register(this, this);
}
@Override
public void attach(MutationRecord mutationRecord) {
if ((role == FormGroupRole.radiogroup || role == FormGroupRole.group) && fieldId == null) {
logger.error("Missing field ID for form group %o with role '%s'.", element(), role.name());
}
}
// ------------------------------------------------------ add
public FormGroup addLabel(FormGroupLabel label) {
return add(label);
}
public FormGroup addControl(FormGroupControl control) {
return add(control);
}
// ------------------------------------------------------ builder
public FormGroup fieldId(String id) {
this.fieldId = id;
return this;
}
public FormGroup required() {
this.required = true;
return this;
}
/**
* Sets the role of the form group. Pass in {@link FormGroupRole#radiogroup} when the form group contains multiple radio
* inputs, or pass in {@link FormGroupRole#group} when the form group contains multiple of any other input type (e.g.
* checkboxes).
*
* Please note that if you set a role, the internal structure of the {@link FormGroupLabel} changes. Without a role the
* label contains a {@code
© 2015 - 2024 Weber Informatics LLC | Privacy Policy