-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: update React grid data provider example (#4133)
* docs: update React grid data provider example * fix mock not being used * make it work without a JPA repository * use new grid data provider hook * remove count implementation
- Loading branch information
1 parent
2f54cae
commit 32465c1
Showing
6 changed files
with
126 additions
and
72 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
81 changes: 10 additions & 71 deletions
81
frontend/demo/component/grid/react/grid-data-provider.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
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,53 @@ | ||
import { getPeople } from 'Frontend/demo/domain/DataService'; | ||
import { CrudMockService } from 'Frontend/demo/services/CrudService'; | ||
import type Person from 'Frontend/generated/com/vaadin/demo/domain/Person'; | ||
import type OrFilter from 'Frontend/generated/com/vaadin/hilla/crud/filter/OrFilter'; | ||
import Matcher from 'Frontend/generated/com/vaadin/hilla/crud/filter/PropertyStringFilter/Matcher'; | ||
import type Pageable from 'Frontend/generated/com/vaadin/hilla/mappedtypes/Pageable'; | ||
|
||
interface PersonWithFullName extends Person { | ||
fullName: string; | ||
} | ||
|
||
class GridPersonService { | ||
private mockService?: CrudMockService<PersonWithFullName>; | ||
|
||
async list(pageable: Pageable, searchTerm: string): Promise<PersonWithFullName[]> { | ||
await this.initMockService(); | ||
|
||
return this.mockService!.list(pageable, this.createFilter(searchTerm)); | ||
} | ||
|
||
private createFilter(searchTerm: string): OrFilter { | ||
return { | ||
'@type': 'or', | ||
children: [ | ||
{ | ||
'@type': 'propertyString', | ||
propertyId: 'fullName', | ||
filterValue: searchTerm, | ||
matcher: Matcher.CONTAINS, | ||
}, | ||
{ | ||
'@type': 'propertyString', | ||
propertyId: 'profession', | ||
filterValue: searchTerm, | ||
matcher: Matcher.CONTAINS, | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
private async initMockService() { | ||
if (this.mockService) { | ||
return; | ||
} | ||
const data = (await getPeople()).people.map((person) => ({ | ||
...person, | ||
fullName: `${person.firstName} ${person.lastName}`, | ||
})); | ||
this.mockService = new CrudMockService(data); | ||
} | ||
} | ||
|
||
export default new GridPersonService(); |
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
26 changes: 26 additions & 0 deletions
26
src/main/java/com/vaadin/demo/component/grid/GridPersonRepository.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,26 @@ | ||
package com.vaadin.demo.component.grid; | ||
|
||
import com.vaadin.demo.domain.Person; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
// hidden-source-line - We don't want to register an actual JPA repository here | ||
// hidden-source-line - So we put the source code to show in a comment and declare a dummy interface that is hidden in the docs | ||
// tag::snippet[] | ||
/* // hidden-source-line | ||
public interface GridPersonRepository extends JpaRepository<Person, Long> { | ||
List<Person> findByFullNameContainingIgnoreCaseOrProfessionContainingIgnoreCase( | ||
String fullName, String profession, Pageable pageable); | ||
} | ||
*/ // hidden-source-line | ||
// end::snippet[] | ||
|
||
@Component // hidden-source-line | ||
public class GridPersonRepository { // hidden-source-line | ||
List<Person> findByFullNameContainingIgnoreCaseOrProfessionContainingIgnoreCase(String fullName, String profession, Pageable pageable) { // hidden-source-line | ||
return List.of(); // hidden-source-line | ||
} // hidden-source-line | ||
}// hidden-source-line |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/vaadin/demo/component/grid/GridPersonService.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,27 @@ | ||
package com.vaadin.demo.component.grid; | ||
|
||
import com.vaadin.demo.domain.Person; | ||
import com.vaadin.flow.server.auth.AnonymousAllowed; | ||
import com.vaadin.hilla.BrowserCallable; | ||
import org.jspecify.annotations.NonNull; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import java.util.List; | ||
|
||
// tag::snippet[] | ||
@BrowserCallable | ||
@AnonymousAllowed | ||
public class GridPersonService { | ||
private final GridPersonRepository personRepository; | ||
|
||
public GridPersonService(GridPersonRepository personRepository) { | ||
this.personRepository = personRepository; | ||
} | ||
|
||
public @NonNull List<@NonNull Person> list(Pageable pageable, String filter) { | ||
// Implement your data fetching logic here | ||
// For this example, we're using a Spring Data repository | ||
return personRepository.findByFullNameContainingIgnoreCaseOrProfessionContainingIgnoreCase(filter, filter, pageable); | ||
} | ||
} | ||
// end::snippet[] |