Skip to content

Instantly share code, notes, and snippets.

View TatuLund's full-sized avatar

Tatu Lund TatuLund

View GitHub Profile
@TatuLund
TatuLund / SpringDemoView.java
Created July 8, 2024 12:09
This is a DemoView and helper class for Spring Boot based (WAR packaging required) projects to show demo code snippets.
package com.example.application.views;
/*
* Copyright 2000-2021 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
@TatuLund
TatuLund / SpinnerButton.java
Created June 11, 2024 05:37
Accessibility makes things less obvious. It is trivial to use Spinner in Button component by just using button.setIcon(new Spinner()). But that is not accessible. Spinner does have aria alert label, but it wont be announced when Spinner is a child of disabled button. Hence Spinner needs to be placed outside button. Here is an example how to do it.
package org.vaadin.addons.componentfactory.spinner;
/*
Add this depenency to get Spinner component
<dependency>
<groupId>org.vaadin.addons.componentfactory</groupId>
<artifactId>spinner</artifactId>
<version>24.1.0</version>
</dependency>
@TatuLund
TatuLund / SecurityConfiguration.java
Created June 10, 2024 08:44
An example how to configure SpringSecurity SessionConcurrency with VaadinWebSecurity. This is a built-in feature in SpringSecurity which can be used for example to allow the same user to login only once at the time in the system. When you try to login on another computer you will be given an error until you logout from the previous one. I.e. one…
package org.vaadin.example;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.session.SessionRegistryImpl;
@TatuLund
TatuLund / GridColumnWidths.java
Last active June 18, 2024 06:48
Vaadin 24 uses only a small fraction of the rows to autocalculate column widths, as it would be too slow otherwise. Your Grid may have long and short data scattered around in the Grid, hence the calculation based on the first rows may not be good for the whole Grid. Then it would be nice to recalculate the widths when Grid is being scrolled to n…
package com.example.application.views;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.Route;
@TatuLund
TatuLund / ComboBoxScrollIntoView.java
Last active May 22, 2024 07:26
In Vaadin 23/24 regular ComboBox does not support scroll selected item into view. But with some JavaScript workaround this is possible to do with ComboBoxLight add-on from Directory as unlike ComboBox, ComboBoxLight does not lazy load and hence the target item is in the DOM always.
package com.example.application.views;
// See: https://github.com/vaadin/flow-components/issues/3470
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.vaadin.addons.componentfactory.ComboBoxLight;
import com.vaadin.flow.component.Html;
@TatuLund
TatuLund / CustomComponent.java
Created April 23, 2024 09:52
A Vaadin code example how to create web component with shadow root in Java.
package org.tatu.components;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Html;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.dom.ShadowRoot;
// Example of slotting of child component:
// component.getElement().setAttribute("slot", "content");
// String html = "<div><slot name='content'></slot></div>";
@TatuLund
TatuLund / ValidatingComboView.java
Created March 21, 2024 09:36
In Vaadin 24 there is a feature of client side validation support for Binder. This is particularly important for some fields like DatePicker. However you can extend the functionality to many other fields if you so wish. For example if you want the ComboBox to show validation error when the input is not matching the suggestions, here is an example.
package com.example.application.views;
import java.util.concurrent.CopyOnWriteArrayList;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.notification.Notification.Position;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.data.binder.Binder;
@TatuLund
TatuLund / CurrencyField.tsx
Created March 13, 2024 12:09
An example how to implement general use currency field component in Hilla/React with Cleave.js library.
import {
TextField,
TextFieldProps,
} from "@hilla/react-components/TextField.js";
import { TextField as TextFieldWebComponent } from "@vaadin/text-field/vaadin-text-field.js";
import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
// Cleave.js is a library for formatting input text content when you are typing.
// It is used to format the input value as a currency in this component.
// Cleave.js is not included in the Hilla components, so it needs to be installed separately.
@TatuLund
TatuLund / MenuBarView.java
Created February 28, 2024 12:17
An example for Vaadin 23/24 how to with JavaScript create automatically closing menu
package com.example.application.views;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.menubar.MenuBar;
import com.vaadin.flow.component.menubar.MenuBarVariant;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.router.Route;
@Route(value = "menu-bar", layout = MainLayout.class)
@TatuLund
TatuLund / LazyComponentView.java
Last active October 2, 2024 11:40
This is an example of LazyComponent for Vaadin 14/23/24. If the content creation of the component takes long. For example data needs to be fetched from the back end and computed, or it is an image whose loading takes time, one may need to produce the component in background process. This example component wraps the process in the Future which cr…
/**
* This is an example of LazyComponent for Vaadin 14/23/24. If the content creation
* of the component takes long. For example data needs to be fetched from the back
* end and computed, or it is an image whose loading takes time, one may need to
* produce the component in background process. This example component wraps the
* process in the Future which creates the component and upon completion adds to
* view. @Push annotation needs to be present in AppShellConfigurator.
*/
package com.example.application.views;