From 64f5bfd71fac0b9795841fa3dedcd0bcc94a4c66 Mon Sep 17 00:00:00 2001 From: Matthias Goudjil Date: Mon, 28 Oct 2024 17:47:12 +0100 Subject: [PATCH] feat: fixed #386 - add searchResultsLocally event and emit searchSubmit when clicking reset btn --- packages/components/table/src/table.ts | 11 ++++++++++- packages/components/table/src/table.vue | 14 +++++++------- packages/components/table/stories/table.stories.ts | 9 +++++++++ packages/components/table/test/table.spec.ts | 13 +++++++++++++ 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/packages/components/table/src/table.ts b/packages/components/table/src/table.ts index 2aa23c4b..f5fb713f 100644 --- a/packages/components/table/src/table.ts +++ b/packages/components/table/src/table.ts @@ -1,6 +1,6 @@ import '@prestashopcorp/puik-components/table/style/css'; import type Table from './table.vue'; -import type { PuikTableSearchInputTypes } from '../src/table-search-input'; +import type { PuikTableSearchInputTypes, searchOption } from '../src/table-search-input'; export enum PuikTableHeaderSize { Small = 'sm', @@ -63,4 +63,13 @@ export interface TableProps { stickyLastCol?: boolean } +export type TableEmits = { + select: [index: number] + 'select:all': [] + 'update:selection': [value: number[]] + sortColumn: [column: sortOption] + searchSubmit: [column: searchOption[]] + searchResultsLocally: any[] +}; + export type TableInstance = InstanceType; diff --git a/packages/components/table/src/table.vue b/packages/components/table/src/table.vue index 86b67c9f..d604485e 100644 --- a/packages/components/table/src/table.vue +++ b/packages/components/table/src/table.vue @@ -383,6 +383,7 @@ import { PuikSkeletonLoader } from '@prestashopcorp/puik-components/skeleton-loa import { PuikTableSearchInput } from '@prestashopcorp/puik-components/table'; import { TableProps, + TableEmits, PuikTableSortOrder, PuikTableSortIcon, sortOption @@ -401,13 +402,7 @@ const props = withDefaults(defineProps(), { selection: () => [] }); -const emit = defineEmits<{ - select: [index: number]; - 'select:all': []; - 'update:selection': [value: number[]]; - sortColumn: [column: sortOption]; - searchSubmit: [column: searchOption[]]; -}>(); +const emit = defineEmits(); const { t } = useLocale(); const checked = ref(props.selection); @@ -447,6 +442,10 @@ const handleSearchReset = () => { searchReset.value = false; forceRenderInputSearch(); searchLoading.value = false; + emit('searchSubmit', toRaw(globalSearchOptions.value)); + if (!props.sortFromServer) { + emit('searchResultsLocally', props.items); + } }; const handleSearchDataLocally = () => { @@ -479,6 +478,7 @@ const handleSearchDataLocally = () => { }); searchLoading.value = false; data.value = searchedRows; + emit('searchResultsLocally', searchedRows); }; const handleSearchSubmit = () => { diff --git a/packages/components/table/stories/table.stories.ts b/packages/components/table/stories/table.stories.ts index cbf370f5..eb0c60ee 100644 --- a/packages/components/table/stories/table.stories.ts +++ b/packages/components/table/stories/table.stories.ts @@ -252,6 +252,15 @@ type inputRange = { ` } } + }, + searchResultsLocally: { + description: 'Event emitted when clicking the search button and prop searchFromServer is false. Return search results (items)', + control: 'none', + table: { + type: { + summary: 'event => any[]' + } + } } }, args: { diff --git a/packages/components/table/test/table.spec.ts b/packages/components/table/test/table.spec.ts index ec703aa2..29a70fc6 100644 --- a/packages/components/table/test/table.spec.ts +++ b/packages/components/table/test/table.spec.ts @@ -341,6 +341,19 @@ describe('Table tests', () => { expect(wrapper.emitted('searchSubmit')).toBeTruthy(); }); + it('should emit searchResultsLocally event', async () => { + const headers: PuikTableHeader[] = [ + { value: 'firstname', searchable: true }, + { value: 'lastname', searchable: true }, + { value: 'action', searchSubmit: true, preventExpand: true } + ]; + factory({ headers, searchBar: true, sortFromServer: false }); + const searchSubmitButton = getTable().find(searchSubmitButtonClass); + expect(searchSubmitButton).toBeTruthy(); + await searchSubmitButton.trigger('click'); + expect(wrapper.emitted('searchResultsLocally')).toBeTruthy(); + }); + it('should update the table when items prop changes', async () => { const headers: PuikTableHeader[] = [{ value: 'firstname' }]; factory({ headers, items: [] });