All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.patternfly.component.list.DescriptionList Maven / Gradle / Ivy

There is a newer version: 0.2.11
Show newest version
/*
 *  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.list;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import org.patternfly.component.BaseComponent;
import org.patternfly.component.ComponentType;
import org.patternfly.component.HasItems;
import org.patternfly.style.Breakpoints;
import org.patternfly.style.Classes;
import org.patternfly.style.Modifiers.Compact;
import org.patternfly.style.Modifiers.Horizontal;
import org.patternfly.style.Orientation;
import org.patternfly.style.Size;

import elemental2.dom.HTMLElement;

import static org.jboss.elemento.Elements.dl;
import static org.jboss.elemento.Elements.removeChildrenFrom;
import static org.patternfly.core.Validation.verifyEnum;
import static org.patternfly.core.Validation.verifyRange;
import static org.patternfly.style.Breakpoint.default_;
import static org.patternfly.style.BreakpointCollector.modifiers;
import static org.patternfly.style.Breakpoints.breakpoints;
import static org.patternfly.style.Classes.component;
import static org.patternfly.style.Classes.descriptionList;
import static org.patternfly.style.Classes.display;
import static org.patternfly.style.Classes.modifier;
import static org.patternfly.style.Modifiers.toggleModifier;
import static org.patternfly.style.Size._2xl;
import static org.patternfly.style.Size.lg;
import static org.patternfly.style.Variable.componentVar;
import static org.patternfly.style.Variables.GridTemplateColumns;

/**
 * A description list contains terms and their corresponding descriptions.
 *
 * @see https://www.patternfly.org/components/description-list
 */
public class DescriptionList extends BaseComponent implements
        Compact,
        Horizontal,
        HasItems {

    // ------------------------------------------------------ factory

    public static DescriptionList descriptionList() {
        return new DescriptionList();
    }

    // ------------------------------------------------------ instance

    private final Map items;

    DescriptionList() {
        super(ComponentType.DescriptionList, dl().css(component(descriptionList)).element());
        this.items = new LinkedHashMap<>();

    }

    // ------------------------------------------------------ add

    @Override
    public DescriptionList add(DescriptionListGroup item) {
        items.put(item.identifier(), item);
        return add(item.element());
    }

    // ------------------------------------------------------ builder

    /** Same as {@linkplain #autoColumnWidths(boolean) autoColumnWidths(true)} */
    public DescriptionList autoColumnWidths() {
        return autoColumnWidths(true);
    }

    /** Sets the description list to format automatically. */
    public DescriptionList autoColumnWidths(boolean autoColumnWidths) {
        return toggleModifier(that(), element(), Classes.autoColumnWidths, autoColumnWidths);
    }

    /** Same as {@linkplain #autoFit(boolean) autoFit(true)} */
    public DescriptionList autoFit() {
        return autoFit(true);
    }

    /** Sets the description list to auto fit. */
    public DescriptionList autoFit(boolean autoFit) {
        return toggleModifier(that(), element(), Classes.autoFit, autoFit);
    }

    /**
     * Same as {@code autoFitMin(breakpoints(default_, autoFitMin))}
     */
    public DescriptionList autoFitMin(String autoFitMin) {
        return autoFitMin(breakpoints(default_, autoFitMin));
    }

    /**
     * Sets the minimum column size for the auto-fit layout at various breakpoints.
     */
    public DescriptionList autoFitMin(Breakpoints autoFitMin) {
        // Variable examples:
        // --pf-v5-c-description-list--GridTemplateColumns--min: 500px;
        // --pf-v5-c-description-list--GridTemplateColumns--min-on-md: 100px;
        // --pf-v5-c-description-list--GridTemplateColumns--min-on-lg: 150px;
        // --pf-v5-c-description-list--GridTemplateColumns--min-on-xl: 200px;
        return componentVar(component(descriptionList), GridTemplateColumns, "min").applyTo(this).set(autoFitMin);
    }

    /**
     * Same as {@code columns(breakpoints(default_, columns))}
     */
    public DescriptionList columns(Integer columns) {
        return columns(breakpoints(default_, columns));
    }

    /**
     * Sets the number of columns on the description list at various breakpoints.
     */
    public DescriptionList columns(Breakpoints columns) {
        if (columns != null) {
            String modifiers = columns.stream()
                    .filter(bp -> verifyRange(element(), "columns", bp.value, 1, 3))
                    .collect(modifiers(col -> col + "-col"));
            css(modifiers);
        }
        return this;
    }

    /**
     * Sets the display size of the descriptions in the description list.
     */
    public DescriptionList displaySize(Size size) {
        if (verifyEnum(element(), "displaySize", size, lg, _2xl)) {
            for (Size s : Size.values()) {
                classList().remove(modifier(display, s));
            }
            css(modifier(display, size));
        }
        return this;
    }

    /** Same as {@linkplain #fillColumns(boolean) fillColumns(true)} */
    public DescriptionList fillColumns() {
        return fillColumns(true);
    }

    /** Sets the default placement of description list groups to fill from top to bottom. */
    public DescriptionList fillColumns(boolean fillColumns) {
        return toggleModifier(that(), element(), Classes.fillColumns, fillColumns);
    }

    /** Same as {@linkplain #fluid(boolean) fluid(true)} */
    public DescriptionList fluid() {
        return fluid(true);
    }

    /** Sets a horizontal description list to have fluid styling. */
    public DescriptionList fluid(boolean fluid) {
        return toggleModifier(that(), element(), Classes.fluid, fluid);
    }

    /**
     * Same as {@code horizontalTermWidth(breakpoints(default_, horizontalTermWidth))}
     */
    public DescriptionList horizontalTermWidth(String horizontalTermWidth) {
        return horizontalTermWidth(breakpoints(default_, horizontalTermWidth));
    }

    /**
     * Sets the horizontal description list's term column width at various breakpoints.
     */
    public DescriptionList horizontalTermWidth(Breakpoints horizontalTermWidth) {
        return componentVar(component(descriptionList), "m-horizontal__term", "width").applyTo(this).set(horizontalTermWidth);
    }

    /** Same as {@linkplain #inlineGrid(boolean) inlineGrid(true)} */
    public DescriptionList inlineGrid() {
        return inlineGrid(true);
    }

    /** Modifies the description list display to inline-grid. */
    public DescriptionList inlineGrid(boolean inlineGrid) {
        return toggleModifier(that(), element(), Classes.inlineGrid, inlineGrid);
    }

    /**
     * Same as {@code orientation(breakpoints(default_, orientation))}
     */
    public DescriptionList orientation(Orientation orientation) {
        return orientation(breakpoints(default_, orientation));
    }

    /** Indicates how the menu will align at various breakpoints. */
    public DescriptionList orientation(Breakpoints orientation) {
        return css(orientation.modifiers());
    }

    public DescriptionList termWidth(String width) {
        // --pf-v5-c-description-list__term--width: 10ch;
        return componentVar(component(descriptionList), "term", "width").applyTo(this).set(width);
    }

    @Override
    public DescriptionList that() {
        return this;
    }

    // ------------------------------------------------------ api

    @Override
    public Iterator iterator() {
        return items.values().iterator();
    }

    @Override
    public int size() {
        return items.size();
    }

    @Override
    public boolean isEmpty() {
        return items.isEmpty();
    }

    @Override
    public void clear() {
        removeChildrenFrom(element());
        items.clear();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy