Skip to content

Commit

Permalink
feat: grid empty state content
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivirkki committed May 24, 2024
1 parent 5b937e8 commit 0f3fca3
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import com.vaadin.flow.component.grid.editor.Editor;
import com.vaadin.flow.component.grid.editor.EditorImpl;
import com.vaadin.flow.component.grid.editor.EditorRenderer;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.page.PendingJavaScriptResult;
import com.vaadin.flow.component.shared.SelectionPreservationHandler;
import com.vaadin.flow.component.shared.SelectionPreservationMode;
Expand Down Expand Up @@ -1457,6 +1458,10 @@ public UpdateQueueData getUpdateQueueData() {

private PendingJavaScriptResult pendingSorterUpdate;

private static final String EMPTY_STATE_SLOT = "empty-state";
private Component emptyStateComponent;
private String emptyStateText;

/**
* Creates a new instance, with page size of 50.
*/
Expand Down Expand Up @@ -5056,4 +5061,56 @@ private String getUniqueKey(T item) {
.map(provider -> provider.apply(item))
.orElse(getDataCommunicator().getKeyMapper().key(item));
}

/**
* Sets the component to be displayed when the grid is empty.
*
* @param emptyStateComponent
* the component to be displayed when the grid is empty
*/
public void setEmptyStateComponent(Component emptyStateComponent) {
this.emptyStateText = null;
this.emptyStateComponent = emptyStateComponent;
updateEmptyStateContent();
}

/**
* Sets the text to be displayed when the grid is empty.
*
* @param emptyStateText
* the text to be displayed when the grid is empty
*/
public void setEmptyStateText(String emptyStateText) {
this.emptyStateComponent = null;
this.emptyStateText = emptyStateText;
updateEmptyStateContent();
}

/**
* Returns the component that is displayed when the grid is empty.
*
* @return the component that is displayed when the grid is empty
*/
public Component getEmptyStateComponent() {
return emptyStateComponent;
}

/**
* Returns the text that is displayed when the grid is empty.
*
* @return the text that is displayed when the grid is empty
*/
public String getEmptyStateText() {
return emptyStateText;
}

private void updateEmptyStateContent() {
if (emptyStateComponent != null) {
SlotUtils.setSlot(this, EMPTY_STATE_SLOT, emptyStateComponent);
} else if (emptyStateText != null) {
SlotUtils.setSlot(this, EMPTY_STATE_SLOT, new Span(emptyStateText));
} else {
SlotUtils.clearSlot(this, EMPTY_STATE_SLOT);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* 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
*
* http://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 com.vaadin.flow.component.grid;

import java.util.Optional;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.dom.Element;

public class GridEmptyStateTest {

private Grid<String> grid;

@Before
public void setup() {
grid = new Grid<>();
}

@Test
public void setEmptyStateComponent_hasEmptyStateComponent() {
var content = new Div();
grid.setEmptyStateComponent(content);
Assert.assertEquals(content.getElement(), getEmptyStateElement());
}

@Test
public void setEmptyStateText_hasEmptyStateText() {
var content = "empty";
grid.setEmptyStateText(content);
var emptyStateElement = getEmptyStateElement();
Assert.assertEquals(content, emptyStateElement.getText());
Assert.assertEquals("span", emptyStateElement.getTag());
}

@Test
public void setEmptyStateComponent_overridesEmptyStateText() {
grid.setEmptyStateText("empty");
var content = new Div();
grid.setEmptyStateComponent(content);
Assert.assertEquals(content.getElement(), getEmptyStateElement());
}

@Test
public void setEmptyStateText_overridesEmptyStateComponent() {
var content = "empty";
grid.setEmptyStateComponent(new Div());
grid.setEmptyStateText(content);
var emptyStateElement = getEmptyStateElement();
Assert.assertEquals(content, emptyStateElement.getText());
Assert.assertEquals("span", emptyStateElement.getTag());
}

@Test
public void setEmptyStateComponentNull_noEmptyStateComponent() {
grid.setEmptyStateText("empty");
grid.setEmptyStateComponent(null);
Assert.assertTrue(getEmptyStateElementOptional().isEmpty());
}

@Test
public void setEmptyStateTextNull_noEmptyStateComponent() {
grid.setEmptyStateText("empty");
grid.setEmptyStateText(null);
Assert.assertTrue(getEmptyStateElementOptional().isEmpty());
}

@Test
public void setEmptyStateComponent_getEmptyStateComponent() {
var content = new Div();
grid.setEmptyStateComponent(content);
Assert.assertEquals(content, grid.getEmptyStateComponent());
}

@Test
public void setEmptyStateText_getEmptyStateText() {
var content = "empty";
grid.setEmptyStateText(content);
Assert.assertEquals(content, grid.getEmptyStateText());
}

@Test
public void setEmptyStateComponent_setEmptyStateText_getEmptyStateComponent() {
grid.setEmptyStateComponent(new Div());
grid.setEmptyStateText("empty");
Assert.assertNull(grid.getEmptyStateComponent());
}

@Test
public void setEmptyStateText_setEmptyStateComponent_getEmptyStateText() {
grid.setEmptyStateText("empty");
grid.setEmptyStateComponent(new Div());
Assert.assertNull(grid.getEmptyStateText());
}

@Test
public void setEmptyStateComponent_setEmptyStateTextNull_getEmptyStateComponent() {
grid.setEmptyStateComponent(new Div());
grid.setEmptyStateText(null);
Assert.assertNull(grid.getEmptyStateComponent());
Assert.assertNull(grid.getEmptyStateText());
}

@Test
public void setEmptyStateText_setEmptyStateComponentNull_getEmptyStateText() {
grid.setEmptyStateText("empty");
grid.setEmptyStateComponent(null);
Assert.assertNull(grid.getEmptyStateComponent());
Assert.assertNull(grid.getEmptyStateText());
}

private Element getEmptyStateElement() {
return getEmptyStateElementOptional().orElse(null);
}

private Optional<Element> getEmptyStateElementOptional() {
return grid.getElement().getChildren().filter(
child -> child.getAttribute("slot").equals("empty-state"))
.findFirst();
}
}

0 comments on commit 0f3fca3

Please sign in to comment.