Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show radio button selection column #114

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.vaadin.addons.flowingcode</groupId>
<artifactId>grid-helpers</artifactId>
<version>1.3.3-SNAPSHOT</version>
<version>1.4.0-SNAPSHOT</version>
<name>Grid Helpers Add-on</name>
<description>Grid Helpers Add-on for Vaadin Flow</description>

Expand All @@ -16,6 +16,8 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<drivers.dir>${project.basedir}/drivers</drivers.dir>
<jetty.version>9.4.36.v20210114</jetty.version>
<webdrivermanager.version>3.8.1</webdrivermanager.version>
<driver-binary-downloader-maven-plugin.version>1.0.17</driver-binary-downloader-maven-plugin.version>
</properties>

<organization>
Expand Down Expand Up @@ -158,7 +160,7 @@
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.8.1</version>
<version>${webdrivermanager.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand Down Expand Up @@ -330,7 +332,7 @@
<configuration>
<quiet>true</quiet>
<doclint>none</doclint>
<additionalparam>-Xdoclint:none</additionalparam>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -414,9 +416,8 @@

<plugin>
<groupId>com.lazerycode.selenium</groupId>
<artifactId>driver-binary-downloader-maven-plugin
</artifactId>
<version>1.0.17</version>
<artifactId>driver-binary-downloader-maven-plugin</artifactId>
<version>${driver-binary-downloader-maven-plugin.version}</version>
<configuration>
<onlyGetDriversForHostOperatingSystem>
true
Expand Down Expand Up @@ -563,7 +564,18 @@
<vaadin.version>24.2.6</vaadin.version>
<jetty.version>11.0.12</jetty.version>
<frontend.hotdeploy>true</frontend.hotdeploy>
<webdrivermanager.version>5.6.3</webdrivermanager.version>
<driver-binary-downloader-maven-plugin.version>1.0.18</driver-binary-downloader-maven-plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.17.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>

</profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,4 +555,12 @@ public static <T> CheckboxColumn<T> addCheckboxColumn(Grid<T> grid,
public static <T> void toggleSelectAllCheckbox(Grid<T> grid, boolean visible) {
getHelper(grid).lazySelectAllGridHelper.toggleSelectAllCheckbox(visible);
}

private final GridRadioSelectionColumnHelper<T> radioButtonSelectionColumnHelper =
new GridRadioSelectionColumnHelper<>(this);

public static <T> GridRadioSelectionColumn showRadioSelectionColumn(Grid<T> grid) {
return getHelper(grid).radioButtonSelectionColumnHelper.showRadioSelectionColumn();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*-
* #%L
* Grid Helpers Add-on
* %%
* Copyright (C) 2022 - 2024 Flowing Code
* %%
* 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.
* #L%
*/

package com.flowingcode.vaadin.addons.gridhelpers;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasStyle;
import com.vaadin.flow.component.Synchronize;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.dependency.NpmPackage;

/**
* Server side implementation for the flow specific grid radio selection column.
*/
@Tag("grid-flow-radio-selection-column")
@NpmPackage(value = "@vaadin/polymer-legacy-adapter", version = "24.3.2")
@JsModule("@vaadin/polymer-legacy-adapter/style-modules.js")
@JsModule("./fcGridHelper/grid-flow-radio-selection-column.js")
public class GridRadioSelectionColumn extends Component implements HasStyle {

/**
* Sets this column's frozen state.
*
* @param frozen whether to freeze or unfreeze this column
*/
public void setFrozen(boolean frozen) {
getElement().setProperty("frozen", frozen);
}

/**
* Gets the this column's frozen state.
*
* @return whether this column is frozen
*/
@Synchronize("frozen-changed")
public boolean isFrozen() {
return getElement().getProperty("frozen", false);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*-
* #%L
* Grid Helpers Add-on
* %%
* Copyright (C) 2022 - 2024 Flowing Code
* %%
* 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.
* #L%
*/

package com.flowingcode.vaadin.addons.gridhelpers;

import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.Grid.SelectionMode;
import java.io.Serializable;
import lombok.RequiredArgsConstructor;

@SuppressWarnings("serial")
@RequiredArgsConstructor
class GridRadioSelectionColumnHelper<T> implements Serializable {

private final GridHelper<T> helper;

private GridRadioSelectionColumn selectionColumn;

public GridRadioSelectionColumn showRadioSelectionColumn() {
remove();

Grid<T> grid = helper.getGrid();
grid.setSelectionMode(SelectionMode.SINGLE);

selectionColumn = new GridRadioSelectionColumn();
selectionColumn.setClassName("fc-grid-radio-selection-column");

if (grid.getElement().getNode().isAttached()) {
grid.getElement().insertChild(0, selectionColumn.getElement());
} else {
grid.getElement().getNode().runWhenAttached(ui -> {
grid.getElement().insertChild(0, selectionColumn.getElement());
});
}

return selectionColumn;
}

private void remove() {
if (selectionColumn != null && selectionColumn.getElement().getNode().isAttached()) {
helper.getGrid().getElement().removeChild(selectionColumn.getElement());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*-
* #%L
* Grid Helpers Add-on
* %%
* Copyright (C) 2022 - 2024 Flowing Code
* %%
* 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.
* #L%
*/

import '@vaadin/grid/vaadin-grid-column.js';
import { GridColumn } from '@vaadin/grid/src/vaadin-grid-column.js';
import { GridRadioSelectionColumnBaseMixin } from './grid-radio-selection-column-base-mixin.js';

class GridFlowRadioSelectionColumn extends GridRadioSelectionColumnBaseMixin(GridColumn) {

static get is() {
return 'grid-flow-radio-selection-column';
}

static get properties() {
return {
/**
* Override property to enable auto-width
*/
autoWidth: {
type: Boolean,
value: true
},

/**
* Override property to set custom width
*/
width: {
type: String,
value: '56px'
},

/**
* The previous state of activeItem. When activeItem turns to `null`,
* previousActiveItem will have an Object with just unselected activeItem
* @private
*/
__previousActiveItem: Object,
};
}


constructor() {
super();
this.__boundOnActiveItemChanged = this.__onActiveItemChanged.bind(this);
}

/** @protected */
disconnectedCallback() {
this._grid.removeEventListener('active-item-changed', this.__boundOnActiveItemChanged);
super.disconnectedCallback();
}

/** @protected */
connectedCallback() {
super.connectedCallback();
if (this._grid) {
this._grid.addEventListener('active-item-changed', this.__boundOnActiveItemChanged);
}
}

/**
* Override a method from `GridRadioSelectionColumnBaseMixin` to handle the user
* selecting an item.
*
* @param {Object} item the item to select
* @protected
* @override
*/
_selectItem(item) {
this._grid.$connector.doSelection([item], true);
}

/**
* Override a method from `GridRadioSelectionColumnBaseMixin` to handle the user
* deselecting an item.
*
* @param {Object} item the item to deselect
* @protected
* @override
*/
_deselectItem(item) {
this._grid.$connector.doDeselection([item], true);
}


/** @private */
__onActiveItemChanged(e) {
const activeItem = e.detail.value;
if (activeItem) {
if(this.__previousActiveItem !== activeItem) {
this._deselectItem(this.__previousActiveItem);
}
this._selectItem(activeItem);
this.__previousActiveItem = activeItem;
} else {
this.__previousActiveItem = undefined;
}
}

/**
* Override a method from `GridRadioSelectionColumnBaseMixin` to handle the user clicked on an item.
*
* @param {Object} item the clicked item
* @protected
* @override
*/
_onItemClicked(item) {
super._onItemClicked(item);
this.__previousActiveItem = item;
}

}

customElements.define(GridFlowRadioSelectionColumn.is, GridFlowRadioSelectionColumn);
Loading
Loading