SchwarzIT / onyx

🚀 A design system and Vue.js component library created by Schwarz IT
https://onyx.schwarz
Apache License 2.0
59 stars 5 forks source link

Implement basic OnyxDataGrid useTableFeature composable #1852

Closed JoCa96 closed 2 weeks ago

JoCa96 commented 2 months ago

Depends on

Description

The composable provides the basic API for implementing table features.

This is done by taking the provided user table data and transform and extend it. Every feature can perform some transformations and wrap rendered slots.

In the first step, we implement the basic useTableFeature composable. It should be enough to implement a Sort feature.

Implementation details

The basic useTableFeature composable API should look like this:

// we need to intersect the empty array, so the array entry types are inferred
export const useTableFeatures = <
  T extends TableFeature<any, any, any, any, any, any>[] | [],
  TFeatureName extends symbol = ExtractTFeatureName<T>,
>(
  features: T,
) => {
return {
    /** Takes the column definition and maps all, calls mutation func and maps at the end to RendererCell */
    enrichTableData,
    /** Takes the column definition and creates a RenderHeader for each, adds actions from features */
    enrichHeaders,
    // the combined `watch` for all features
    watcheSources,
  };
}

export type TableEntry = {
  id: string | number;
  [key: PropertyKey]: unknown;
};

export type TableFeature<
  TEntry extends TableEntry,
  TFeatureName extends symbol,
> = {
  /**
   * Unique name and identifier of the table feature
   */
  name: TFeatureName;

  /**
   * An array of reactive states that should trigger a table re-calcualtion
   */
  watch: WatchSource[];

  /**
   * Allows modifying the table state as a whole.
   */
  mutation?: {
    func: (state: EntryState<TEntry>[]) => EntryState<TEntry>[];
  };

  /**
   * Allows the modification of the header columns before render.
   */
  header?: {
    /** actions are shown after the header label, later `listComponent` can be added */
    actions?: { iconComponent: Component, onTrigger?: (event: MouseEvent) => void }[];
  };
};

Reference implementation

Design

https://www.figma.com/design/YfEUBOHk4J4nYrk04geswG/Onyx-Component-Library?node-id=4435-44768&node-type=text&m=dev

Acceptance criteria

Definition of Done

Approval

larsrickert commented 2 weeks ago

@JoCa96 Review/approval notes:

  1. The example code of the features property breaks the control panel layout. Maybe we should just add a link to the Storybook example?: image
  2. The sorting changed action is only logged/emitted for ascending. For descending and reset, nothing is shown/emitted
JoCa96 commented 2 weeks ago

Follow-up: #2082