From fec030e399f60f0240c24076b4e12e90092d66a6 Mon Sep 17 00:00:00 2001 From: Etienne Burdet Date: Tue, 12 Nov 2024 16:21:05 +0100 Subject: [PATCH] =?UTF-8?q?feat:=C2=A0add=20clickable=20and=20hoverable=20?= =?UTF-8?q?rows=20to=20table?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stories/Table/Table.stories.tsx | 30 ++++++++++- .../src/components/Table/Body.svelte | 19 ++----- .../src/components/Table/Cell/Cell.svelte | 54 +++++++++++++++++-- .../src/components/Table/Cell/ZoomIcon.svelte | 15 ++++++ .../src/components/Table/Row.svelte | 52 ++++++++++++++++++ .../src/components/Table/Table.svelte | 6 +-- .../src/components/Table/TableCard.svelte | 3 +- .../src/components/Table/types.ts | 7 +++ 8 files changed, 164 insertions(+), 22 deletions(-) create mode 100644 packages/visualizations/src/components/Table/Cell/ZoomIcon.svelte create mode 100644 packages/visualizations/src/components/Table/Row.svelte diff --git a/packages/visualizations-react/stories/Table/Table.stories.tsx b/packages/visualizations-react/stories/Table/Table.stories.tsx index d64a1050..3abe61bc 100644 --- a/packages/visualizations-react/stories/Table/Table.stories.tsx +++ b/packages/visualizations-react/stories/Table/Table.stories.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState } from 'react'; import { ComponentMeta, ComponentStory } from '@storybook/react'; import type { TableData, Async } from '@opendatasoft/visualizations'; @@ -39,6 +39,28 @@ const ScrollTemplate: ComponentStory = args => ( ); +const RowHoverTemplate: ComponentStory = args => { + const { options: argOptions, data: argData } = args; + const [hoveredRecord, setHovered] = useState | undefined | null>(null); + const [lastClicked, setLastClicked] = useState | undefined | null>(null); + + const onMouseEnter = (record?: Record) => {setHovered(record);}; + const onMouseLeave = () => {setHovered(null);}; + const onClick = (record?: Record) => {setLastClicked(record);}; + + return ( + <> +

Hovered

+
{JSON.stringify(hoveredRecord)}
+

Clicked

+
{JSON.stringify(lastClicked)}
+
+ + ; + + ); +}; + const optionsWithPagination = { ...options, pagination: { @@ -107,3 +129,9 @@ RtlDirection.args = { data, options: optionsWithPagination, }; + +export const Rows = RowHoverTemplate.bind({}); +Rows.args = { + data, + options, +}; diff --git a/packages/visualizations/src/components/Table/Body.svelte b/packages/visualizations/src/components/Table/Body.svelte index e3c97202..85c77eb6 100644 --- a/packages/visualizations/src/components/Table/Body.svelte +++ b/packages/visualizations/src/components/Table/Body.svelte @@ -1,10 +1,12 @@ @@ -23,21 +25,10 @@ {/each} {:else if records} {#each records as record} - - {#each columns as column} - - {/each} - + {/each} {/if} diff --git a/packages/visualizations/src/components/Table/Cell/Cell.svelte b/packages/visualizations/src/components/Table/Cell/Cell.svelte index f501ce3d..f421f716 100644 --- a/packages/visualizations/src/components/Table/Cell/Cell.svelte +++ b/packages/visualizations/src/components/Table/Cell/Cell.svelte @@ -1,21 +1,44 @@ diff --git a/packages/visualizations/src/components/Table/Cell/ZoomIcon.svelte b/packages/visualizations/src/components/Table/Cell/ZoomIcon.svelte new file mode 100644 index 00000000..bcbc7d88 --- /dev/null +++ b/packages/visualizations/src/components/Table/Cell/ZoomIcon.svelte @@ -0,0 +1,15 @@ + + + + + + + + diff --git a/packages/visualizations/src/components/Table/Row.svelte b/packages/visualizations/src/components/Table/Row.svelte new file mode 100644 index 00000000..b5179f28 --- /dev/null +++ b/packages/visualizations/src/components/Table/Row.svelte @@ -0,0 +1,52 @@ + + + + {#each columns as column, index} + + {/each} + + + + + diff --git a/packages/visualizations/src/components/Table/Table.svelte b/packages/visualizations/src/components/Table/Table.svelte index 352ccda8..2da7999a 100644 --- a/packages/visualizations/src/components/Table/Table.svelte +++ b/packages/visualizations/src/components/Table/Table.svelte @@ -1,7 +1,7 @@
- {#if isValidRawValue(rawValue)} - - {/if} +
+ {#if onClick} + + {/if} +
+ {#if isValidRawValue(rawValue)} + + {/if} +
+
- +
{#if description} diff --git a/packages/visualizations/src/components/Table/TableCard.svelte b/packages/visualizations/src/components/Table/TableCard.svelte index 254c94b2..fabb007a 100644 --- a/packages/visualizations/src/components/Table/TableCard.svelte +++ b/packages/visualizations/src/components/Table/TableCard.svelte @@ -14,6 +14,7 @@ $: ({ value: records, loading: isLoading } = data); $: ({ columns, + rows, title, subtitle, description, @@ -33,7 +34,7 @@
- +
{#if pagination} {/if} diff --git a/packages/visualizations/src/components/Table/types.ts b/packages/visualizations/src/components/Table/types.ts index 823c2ee8..dafac9bb 100644 --- a/packages/visualizations/src/components/Table/types.ts +++ b/packages/visualizations/src/components/Table/types.ts @@ -95,6 +95,12 @@ export type GeoColumn = BaseColumn & { }>; }; +export type Rows = { + onClick?: (record?: Record, index?: number) => void; + onMouseEnter?: (record?: Record, index?: number) => void; + onMouseLeave?: (record?: Record, index?: number) => void; +}; + export type Column = | ShortTextColumn | LongTextColumn @@ -106,6 +112,7 @@ export type Column = export type TableOptions = { columns: Column[]; + rows?: Rows; title?: string; subtitle?: string; description?: string;