-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a dependency list to useGridDataProvider and useComboBoxDataProvider
This is to support using state and signals in the fetch callback
- Loading branch information
Showing
7 changed files
with
285 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 30 additions & 9 deletions
39
packages/java/tests/spring/react-grid-test/frontend/views/GridUseGridDataProviderHook.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,41 @@ | ||
import { useGridDataProvider } from '@vaadin/hilla-react-crud'; | ||
import { useSignal } from '@vaadin/hilla-react-signals'; | ||
import { Button } from '@vaadin/react-components'; | ||
import { Grid } from '@vaadin/react-components/Grid'; | ||
import { GridSortColumn } from '@vaadin/react-components/GridSortColumn'; | ||
import { useState } from 'react'; | ||
import type Pageable from 'Frontend/generated/com/vaadin/hilla/mappedtypes/Pageable'; | ||
import { PersonCustomService } from 'Frontend/generated/endpoints'; | ||
|
||
export default function GridUseGridDataProviderHook(): React.JSX.Element { | ||
const dataProvider = useGridDataProvider(PersonCustomService.listPersonsLazy); | ||
const filterSignal = useSignal(''); | ||
const fetch = async (pageable: Pageable) => | ||
PersonCustomService.listPersonsLazyWithFilter(pageable, filterSignal.value); | ||
const dataProviderWithFilter = useGridDataProvider(fetch, [filterSignal.value]); | ||
|
||
const [state, setState] = useState(0); | ||
|
||
return ( | ||
<div className="p-m flex flex-col gap-m"> | ||
<Grid pageSize={10} dataProvider={dataProvider}> | ||
<GridSortColumn path="firstName" /> | ||
<GridSortColumn path="lastName" /> | ||
<GridSortColumn path="gender" /> | ||
</Grid> | ||
<Button onClick={() => dataProvider.refresh()}>Refresh</Button> | ||
</div> | ||
<> | ||
<div className="p-m flex flex-col gap-m"></div> | ||
<div> | ||
<input | ||
type="text" | ||
onInput={(e) => { | ||
const filterString = (e.target as HTMLInputElement).value; | ||
setState(state + 1); | ||
filterSignal.value = filterString; | ||
}} | ||
/> | ||
<div>Filter length useState is {state}</div> | ||
<div>Filter value is {filterSignal.value}</div> | ||
<Grid pageSize={10} dataProvider={dataProviderWithFilter}> | ||
<GridSortColumn path="firstName" /> | ||
<GridSortColumn path="lastName" /> | ||
<GridSortColumn path="gender" /> | ||
</Grid> | ||
<Button onClick={() => dataProviderWithFilter.refresh()}>Refresh</Button> | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
.../src/test/java/com/vaadin/hilla/test/reactgrid/ComboBoxUseComboBoxDataProviderHookIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.vaadin.hilla.test.reactgrid; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.vaadin.flow.component.button.testbench.ButtonElement; | ||
import com.vaadin.flow.component.combobox.testbench.ComboBoxElement; | ||
import com.vaadin.flow.testutil.ChromeBrowserTest; | ||
import com.vaadin.testbench.TestBenchElement; | ||
|
||
public class ComboBoxUseComboBoxDataProviderHookIT extends ChromeBrowserTest { | ||
|
||
@Override | ||
protected String getTestPath() { | ||
return getRootURL() + "/" | ||
+ getClass().getSimpleName().replace("IT", ""); | ||
|
||
} | ||
|
||
@Override | ||
@Before | ||
public void setup() throws Exception { | ||
super.setup(); | ||
getDriver().get(getTestPath()); | ||
} | ||
|
||
@After | ||
public void checkBrowserLogs() { | ||
checkLogsForErrors(); | ||
} | ||
|
||
@Test | ||
public void defaultSort_dataShown() { | ||
ComboBoxElement comboBox = $(ComboBoxElement.class).id("defaultSort"); | ||
List<String> options = getOptions(comboBox); | ||
Assert.assertEquals("Johnson", options.get(0)); | ||
Assert.assertEquals("Lewis", options.get(9)); | ||
|
||
} | ||
|
||
@Test | ||
public void sortUsingLastname_dataShown() { | ||
ComboBoxElement comboBox = $(ComboBoxElement.class).id("sortLastName"); | ||
List<String> options = getOptions(comboBox); | ||
Assert.assertEquals("Adams", options.get(0)); | ||
Assert.assertEquals("Evans", options.get(9)); | ||
} | ||
|
||
@Test | ||
public void filteringUsingSignalWorks() { | ||
ComboBoxElement comboBox = $(ComboBoxElement.class).id("prependFilter"); | ||
List<String> options = getOptions(comboBox); | ||
Assert.assertEquals("Adams", options.get(0)); | ||
Assert.assertEquals("Evans", options.get(9)); | ||
comboBox.closePopup(); | ||
|
||
TestBenchElement filterInput = $("input").id("filter"); | ||
filterInput.sendKeys("c"); | ||
options = getOptions(comboBox); | ||
Assert.assertEquals("Baker", options.get(0)); // Zack | ||
Assert.assertEquals("Johnson", options.get(9)); // Alice | ||
} | ||
|
||
private List<String> getOptions(ComboBoxElement comboBox) { | ||
comboBox.openPopup(); | ||
return waitUntil(driver -> { | ||
List<String> opt = comboBox.getOptions(); | ||
if (opt.isEmpty()) { | ||
return null; | ||
} | ||
return opt; | ||
}); | ||
} | ||
|
||
private void setFilter(String string) { | ||
TestBenchElement filterInput = $("input").first(); | ||
filterInput.clear(); | ||
filterInput.sendKeys(string); | ||
filterInput.dispatchEvent("input", Map.of("bubbles", true)); | ||
} | ||
|
||
private void refresh() { | ||
var refreshButton = $(ButtonElement.class).all().stream() | ||
.filter(button -> button.getText().equals("Refresh")) | ||
.findFirst(); | ||
if (refreshButton.isPresent()) { | ||
refreshButton.get().click(); | ||
} else { | ||
Assert.fail("Refresh button not found"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.