@lenso/primitives 0.8.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ "use client";
2
+ import { createSwipeActionsController, swipeActions } from "./swipe-actions.js";
3
+ import { createParts } from "@ui-sdk/react/primitive";
4
+ const SwipeActions = createParts(swipeActions, {
5
+ root: "div",
6
+ content: "div",
7
+ body: "div",
8
+ actions: "div",
9
+ trigger: "button"
10
+ });
11
+ export { SwipeActions, createSwipeActionsController, swipeActions };
@@ -0,0 +1,40 @@
1
+ import { createSwipeActions, definePrimitive, type ChangeDetails, type ChangeOptions, type SlotDefinition, type SwipeDraft, type SwipeOpen, type SwipeSide } from "@ui-sdk/core";
2
+ export type SwipeActionsReason = "swipe" | "trigger-press" | "escape-key" | "outside-press" | "imperative";
3
+ export type SwipeActionsController = {
4
+ getOpenSide(): SwipeSide | null;
5
+ setOpenSide(side: SwipeSide | null, options?: ChangeOptions<SwipeActionsReason>): boolean;
6
+ };
7
+ export type SwipeActionsOptions = {
8
+ open?: SwipeOpen | null;
9
+ defaultOpen?: SwipeOpen | null;
10
+ onOpenChange?: (open: SwipeOpen | null, details: ChangeDetails<SwipeActionsReason>) => void;
11
+ onFullSwipe?: (side: SwipeSide, details: {
12
+ event: Event;
13
+ }) => boolean;
14
+ disabled?: boolean;
15
+ controller?: SwipeActionsController;
16
+ };
17
+ type State = {
18
+ open: SwipeOpen | null;
19
+ swiping: SwipeDraft | null;
20
+ disabled: boolean;
21
+ };
22
+ type Api = SwipeActionsController & {
23
+ swipe: ReturnType<typeof createSwipeActions<State>>;
24
+ run(event: Event, action: () => void): void;
25
+ keydown(event: KeyboardEvent): void;
26
+ };
27
+ type ActionProps = {
28
+ side: SwipeSide;
29
+ value: string;
30
+ };
31
+ type Slots = {
32
+ root: SlotDefinition<State, Api, {}, State>;
33
+ content: SlotDefinition<State, Api, {}>;
34
+ body: SlotDefinition<State, Api, {}>;
35
+ actions: SlotDefinition<State, Api, ActionProps>;
36
+ trigger: SlotDefinition<State, Api, ActionProps>;
37
+ };
38
+ export declare function createSwipeActionsController(): SwipeActionsController;
39
+ export declare const swipeActions: ReturnType<typeof definePrimitive<SwipeActionsOptions, State, Api, Slots>>;
40
+ export {};
@@ -0,0 +1,200 @@
1
+ import { createChange, createController, createSwipeActions, definePrimitive, elements, live } from "@ui-sdk/core";
2
+ // src/swipe-actions/swipe-actions.ts
3
+ function createSwipeActionsController() {
4
+ return createController();
5
+ }
6
+ const swipeActions = definePrimitive({
7
+ name: "swipe-actions",
8
+ options: {
9
+ open: "value",
10
+ defaultOpen: "value",
11
+ onOpenChange: "callback",
12
+ onFullSwipe: "action",
13
+ disabled: "boolean",
14
+ controller: "value"
15
+ },
16
+ controlled: ["open"],
17
+ live: ["swiping"],
18
+ state: (options) => ({
19
+ open: options.open === void 0 ? options.defaultOpen ?? null : options.open,
20
+ swiping: null,
21
+ disabled: !!options.disabled
22
+ }),
23
+ sync: (options) => ({
24
+ ...options.open === void 0 ? {} : { open: options.open },
25
+ disabled: !!options.disabled
26
+ }),
27
+ api: (context) => {
28
+ const change = createChange(context);
29
+ let cause = { reason: "imperative" };
30
+ let accepted = false;
31
+ let canceled = false;
32
+ const store = {
33
+ ...context.store,
34
+ set(patch, delivered) {
35
+ const { open, ...internal } = patch;
36
+ if (open !== void 0 && !canceled) accepted = change("open", open, {
37
+ ...cause,
38
+ reason: cause.reason ?? "imperative"
39
+ });
40
+ context.store.set(internal, delivered);
41
+ }
42
+ };
43
+ const swipe = createSwipeActions({
44
+ ...context,
45
+ store
46
+ }, {
47
+ key: "open",
48
+ draft: "swiping",
49
+ disabled: () => context.store.get().disabled,
50
+ fullSwipe: () => context.options().onFullSwipe ? .8 : Infinity,
51
+ onFullSwipe: (_key, side, details) => context.options().onFullSwipe?.(side, details) ?? false
52
+ });
53
+ const run = (event, action) => {
54
+ if (event.type === "pointerdown" || event instanceof KeyboardEvent) canceled = false;
55
+ if (event.type === "pointercancel" || event.type === "lostpointercapture") canceled = true;
56
+ cause = {
57
+ event,
58
+ reason: event instanceof KeyboardEvent ? event.key === "Escape" ? "escape-key" : "trigger-press" : event instanceof PointerEvent && event.target instanceof Element && event.target.closest("[data-swipe-body]") && elements(context, "root").some(({ el }) => el.contains(event.target)) ? "swipe" : "outside-press"
59
+ };
60
+ action();
61
+ };
62
+ const setOpenSide = (side, options = {}) => {
63
+ if (context.store.get().disabled && side !== null) return false;
64
+ canceled = false;
65
+ cause = {
66
+ ...options,
67
+ reason: options.reason ?? "imperative"
68
+ };
69
+ accepted = false;
70
+ if (side) swipe.show("row", side);
71
+ else swipe.close();
72
+ return accepted;
73
+ };
74
+ const keydown = (event) => run(event, () => {
75
+ const side = context.store.get().open?.side;
76
+ swipe.keydown(event, "row");
77
+ if (event.key === "Escape" && side && !context.store.get().open) elements(context, "trigger").find((part) => context.props("trigger", part.key)?.side === side)?.el.focus();
78
+ });
79
+ return {
80
+ swipe,
81
+ run,
82
+ keydown,
83
+ getOpenSide: () => context.store.get().open?.side ?? null,
84
+ setOpenSide
85
+ };
86
+ },
87
+ slots: (slot) => ({
88
+ root: slot({
89
+ state: (state) => state,
90
+ attrs: (state) => ({
91
+ role: "group",
92
+ tabindex: "-1",
93
+ "data-disabled": state.disabled ? "" : void 0,
94
+ "data-open-side": state.open?.side
95
+ }),
96
+ live: live((state, { api }) => {
97
+ const attrs = api.swipe.contentAttrs(state, "row");
98
+ return {
99
+ "data-swipe": attrs["data-swipe"],
100
+ "data-swiping": attrs["data-swiping"]
101
+ };
102
+ }),
103
+ on: {
104
+ pointerdown: (event, { api, el }) => {
105
+ const body = event.target instanceof Element ? event.target.closest("[data-swipe-body]") : null;
106
+ if (body && el?.contains(body)) api.run(event, () => api.swipe.pointerdown(event, "row", body));
107
+ },
108
+ keydown: (event, { api }) => api.keydown(event)
109
+ },
110
+ bind: (el, { api }) => {
111
+ const unbind = api.swipe.bindRow(el, "row");
112
+ let pointer;
113
+ const capture = (event) => {
114
+ if (event.type === "pointerdown") {
115
+ if (!(event.target instanceof Node) || !el.contains(event.target)) api.run(event, () => {});
116
+ const body = event.target instanceof Element ? event.target.closest("[data-swipe-body]") : null;
117
+ if (event.button !== 0 || !event.isPrimary || !body || !el.contains(body)) return;
118
+ pointer = {
119
+ id: event.pointerId,
120
+ body
121
+ };
122
+ }
123
+ if (!pointer || event.pointerId !== pointer.id) return;
124
+ if (event.type === "lostpointercapture" && (event.target !== pointer.body || pointer.body.hasPointerCapture(pointer.id))) return;
125
+ api.run(event, () => {});
126
+ if (event.type !== "pointerdown") pointer = void 0;
127
+ };
128
+ const selection = (event) => {
129
+ if (!(event.target instanceof Element) || !event.target.closest("input,textarea,select,[contenteditable]")) event.preventDefault();
130
+ };
131
+ el.addEventListener("selectstart", selection);
132
+ for (const type of [
133
+ "pointerdown",
134
+ "pointerup",
135
+ "pointercancel",
136
+ "lostpointercapture"
137
+ ]) el.ownerDocument.addEventListener(type, capture, true);
138
+ return () => {
139
+ el.removeEventListener("selectstart", selection);
140
+ for (const type of [
141
+ "pointerdown",
142
+ "pointerup",
143
+ "pointercancel",
144
+ "lostpointercapture"
145
+ ]) el.ownerDocument.removeEventListener(type, capture, true);
146
+ unbind();
147
+ };
148
+ }
149
+ }),
150
+ body: slot({ attrs: () => ({ "data-swipe-body": "" }) }),
151
+ content: slot({ live: live((state, { api }) => api.swipe.contentAttrs(state, "row")) }),
152
+ actions: slot({
153
+ props: {
154
+ side: "string",
155
+ value: "string"
156
+ },
157
+ attrs: (state, { api, props, id }) => ({
158
+ id: elementsId(id, props.side),
159
+ ...api.swipe.actionsAttrs(state, "row", props.side),
160
+ "aria-hidden": String(state.disabled || state.open?.side !== props.side)
161
+ }),
162
+ bind: (el, { api, props }) => api.swipe.bindActions(el, "row", props.side)
163
+ }),
164
+ trigger: slot({
165
+ props: {
166
+ side: "string",
167
+ value: "string"
168
+ },
169
+ role: "button",
170
+ focus: "native",
171
+ attrs: (state, { id, props }) => ({
172
+ type: "button",
173
+ disabled: state.disabled ? "" : void 0,
174
+ "aria-expanded": String(state.open?.side === props.side),
175
+ "aria-controls": elementsId(id, props.side)
176
+ }),
177
+ press: ({ api, props }, event) => {
178
+ api.setOpenSide(api.getOpenSide() === props.side ? null : props.side, {
179
+ reason: "trigger-press",
180
+ ...event ? { event } : {}
181
+ });
182
+ }
183
+ })
184
+ }),
185
+ agent: {
186
+ actions: ["setOpenSide"],
187
+ reveals: true,
188
+ items: () => [{
189
+ value: "start",
190
+ label: "Leading actions"
191
+ }, {
192
+ value: "end",
193
+ label: "Trailing actions"
194
+ }]
195
+ }
196
+ });
197
+ function elementsId(id, side) {
198
+ return `${id}-${side}-actions`;
199
+ }
200
+ export { createSwipeActionsController, swipeActions };
@@ -0,0 +1,36 @@
1
+ import { definePrimitive, type ChangeDetails, type ChangeOptions, type SlotDefinition } from "@ui-sdk/core";
2
+ export type WheelOption = {
3
+ value: string;
4
+ label: string;
5
+ disabled?: boolean;
6
+ };
7
+ export type WheelReason = "item-press" | "list-navigation" | "scroll" | "drag" | "reset" | "restore" | "imperative";
8
+ export type WheelController = {
9
+ getValue(): string | null;
10
+ setValue(value: string | null, options?: ChangeOptions<WheelReason>): boolean;
11
+ clear(options?: ChangeOptions<WheelReason>): boolean;
12
+ reset(): boolean;
13
+ };
14
+ export type WheelOptions = {
15
+ options: readonly WheelOption[];
16
+ value?: string | null;
17
+ defaultValue?: string | null;
18
+ onValueChange?: (value: string | null, details: ChangeDetails<WheelReason>) => void;
19
+ disabled?: boolean;
20
+ readOnly?: boolean;
21
+ controller?: WheelController;
22
+ };
23
+ type State = {
24
+ value: string | null;
25
+ disabled: boolean;
26
+ readOnly: boolean;
27
+ };
28
+ type Api = WheelController & {
29
+ items(): readonly WheelOption[];
30
+ };
31
+ type Slots = {
32
+ root: SlotDefinition<State, Api, {}, State>;
33
+ };
34
+ export declare function createWheelPickerController(): WheelController;
35
+ export declare const wheelPicker: ReturnType<typeof definePrimitive<WheelOptions, State, Api, Slots>>;
36
+ export {};
@@ -0,0 +1,68 @@
1
+ import { createChange, createController, definePrimitive } from "@ui-sdk/core";
2
+ // src/wheel-picker/wheel-picker.ts
3
+ function createWheelPickerController() {
4
+ return createController();
5
+ }
6
+ const wheelPicker = definePrimitive({
7
+ name: "wheel-picker",
8
+ options: {
9
+ options: "list",
10
+ value: "string",
11
+ defaultValue: "string",
12
+ onValueChange: "callback",
13
+ disabled: "boolean",
14
+ readOnly: "boolean",
15
+ controller: "value"
16
+ },
17
+ controlled: ["value"],
18
+ state: (options) => ({
19
+ value: options.value === void 0 ? options.defaultValue ?? options.options.find((item) => !item.disabled)?.value ?? null : options.value,
20
+ disabled: !!options.disabled,
21
+ readOnly: !!options.readOnly
22
+ }),
23
+ sync: (options) => ({
24
+ ...options.value === void 0 ? {} : { value: options.value },
25
+ disabled: !!options.disabled,
26
+ readOnly: !!options.readOnly
27
+ }),
28
+ api: (context) => {
29
+ const change = createChange(context);
30
+ const setValue = (value, options = {}) => {
31
+ const state = context.store.get();
32
+ if ((state.disabled || state.readOnly) && options.reason !== "reset" && options.reason !== "restore") return false;
33
+ if (value !== null && !context.options().options.some((item) => item.value === value && !item.disabled)) return false;
34
+ return change("value", value, {
35
+ ...options,
36
+ reason: options.reason ?? "imperative"
37
+ });
38
+ };
39
+ return {
40
+ reset: () => setValue(context.options().defaultValue ?? context.options().options.find((item) => !item.disabled)?.value ?? null, { reason: "reset" }),
41
+ getValue: () => context.store.get().value,
42
+ setValue,
43
+ clear: (options) => setValue(null, options),
44
+ items: () => context.options().options
45
+ };
46
+ },
47
+ slots: (slot) => ({ root: slot({
48
+ state: (state) => state,
49
+ attrs: (state) => ({
50
+ role: "group",
51
+ "data-disabled": state.disabled ? "" : void 0,
52
+ "data-readonly": state.readOnly ? "" : void 0
53
+ })
54
+ }) }),
55
+ agent: {
56
+ actions: [
57
+ "setValue",
58
+ "clear",
59
+ "reset"
60
+ ],
61
+ closed: true,
62
+ items: (api) => api.items().map(({ value, label }) => ({
63
+ value,
64
+ label
65
+ }))
66
+ }
67
+ });
68
+ export { createWheelPickerController, wheelPicker };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lenso/primitives",
3
- "version": "0.8.0",
3
+ "version": "0.9.1",
4
4
  "description": "Headless product primitives for Lenso UI.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -15,21 +15,41 @@
15
15
  ],
16
16
  "type": "module",
17
17
  "exports": {
18
- "./resize-handle": {
19
- "types": "./dist/resize-handle/index.d.ts",
20
- "import": "./dist/resize-handle/index.js"
21
- },
22
- "./data-grid": {
23
- "types": "./dist/data-grid/index.d.ts",
24
- "import": "./dist/data-grid/index.js"
25
- },
26
18
  "./sidebar": {
27
19
  "types": "./dist/sidebar/index.d.ts",
28
20
  "import": "./dist/sidebar/index.js"
29
21
  },
30
- "./command": {
31
- "types": "./dist/command/index.d.ts",
32
- "import": "./dist/command/index.js"
22
+ "./sidebar/core": {
23
+ "types": "./dist/sidebar/sidebar.d.ts",
24
+ "import": "./dist/sidebar/sidebar.js"
25
+ },
26
+ "./section-index": {
27
+ "types": "./dist/section-index/index.d.ts",
28
+ "import": "./dist/section-index/index.js"
29
+ },
30
+ "./section-index/core": {
31
+ "types": "./dist/section-index/section-index.d.ts",
32
+ "import": "./dist/section-index/section-index.js"
33
+ },
34
+ "./swipe-actions": {
35
+ "types": "./dist/swipe-actions/index.d.ts",
36
+ "import": "./dist/swipe-actions/index.js"
37
+ },
38
+ "./swipe-actions/core": {
39
+ "types": "./dist/swipe-actions/swipe-actions.d.ts",
40
+ "import": "./dist/swipe-actions/swipe-actions.js"
41
+ },
42
+ "./model-control": {
43
+ "types": "./dist/model-control/model-control.d.ts",
44
+ "import": "./dist/model-control/model-control.js"
45
+ },
46
+ "./list/core": {
47
+ "types": "./dist/list/list.d.ts",
48
+ "import": "./dist/list/list.js"
49
+ },
50
+ "./wheel-picker/core": {
51
+ "types": "./dist/wheel-picker/wheel-picker.d.ts",
52
+ "import": "./dist/wheel-picker/wheel-picker.js"
33
53
  }
34
54
  },
35
55
  "publishConfig": {
@@ -41,11 +61,10 @@
41
61
  "typecheck": "bun check --no-script --noEmit"
42
62
  },
43
63
  "dependencies": {
44
- "@tanstack/react-table": "9.3.1"
64
+ "@ui-sdk/core": "0.1.0-beta.1",
65
+ "@ui-sdk/react": "0.1.0-beta.1"
45
66
  },
46
67
  "devDependencies": {
47
- "@base-ui/react": "1.9.0",
48
- "@stylexjs/stylex": "0.19.1",
49
68
  "@types/bun": "1.4.4",
50
69
  "@types/react": "19.3.0",
51
70
  "@types/react-dom": "19.3.0",
@@ -55,8 +74,6 @@
55
74
  "typescript": "7.0.2"
56
75
  },
57
76
  "peerDependencies": {
58
- "@base-ui/react": ">=1.7.0 <2.0.0",
59
- "@stylexjs/stylex": ">=0.19.0 <1.0.0",
60
77
  "react": ">=19.2.0 <20.0.0"
61
78
  }
62
79
  }
@@ -1,25 +0,0 @@
1
- import { Autocomplete } from "@base-ui/react/autocomplete";
2
- import { type RefAttributes } from "react";
3
- export type CommandRootProps<Value> = Omit<Autocomplete.Root.Props<Value>, "inline" | "open" | "defaultOpen" | "onOpenChange" | "mode" | "filter" | "items" | "filteredItems"> & {
4
- items?: readonly Value[];
5
- filteredItems?: readonly Value[];
6
- filter?: Autocomplete.Root.Props<Value>["filter"] | false;
7
- };
8
- /** An inline command list. Compose with Dialog when an overlay is needed. */
9
- export declare function CommandRoot<Value>({ value, defaultValue, onValueChange, filter, autoHighlight, keepHighlight, readOnly, ...props }: CommandRootProps<Value>): import("react").JSX.Element;
10
- export type CommandItemProps<Value = string> = Omit<Autocomplete.Item.Props, "value" | "onSelect"> & RefAttributes<HTMLDivElement> & {
11
- value: Value;
12
- onSelect?: (value: Value) => void;
13
- };
14
- export declare function CommandItem<Value = string>({ value, onClick, onSelect, ...props }: CommandItemProps<Value>): import("react").JSX.Element;
15
- export type CommandInputProps = Autocomplete.Input.Props;
16
- export declare function CommandInput({ onKeyDown, ...props }: CommandInputProps): import("react").JSX.Element;
17
- export declare const CommandList: typeof Autocomplete.List;
18
- export declare const CommandEmpty: typeof Autocomplete.Empty;
19
- export declare const CommandGroup: typeof Autocomplete.Group;
20
- export declare const CommandGroupLabel: typeof Autocomplete.GroupLabel;
21
- export declare const CommandSeparator: typeof Autocomplete.Separator;
22
- export declare const CommandCollection: typeof Autocomplete.Collection;
23
- export declare const CommandStatus: typeof Autocomplete.Status;
24
- export declare const CommandClear: typeof Autocomplete.Clear;
25
- export declare const useCommandFilter: typeof Autocomplete.useFilter;
@@ -1,16 +0,0 @@
1
- import { CommandRoot, CommandInput, CommandList, CommandItem, CommandEmpty, CommandGroup, CommandGroupLabel, CommandSeparator, CommandCollection, CommandStatus, CommandClear, useCommandFilter } from "./command.js";
2
- export declare const Command: typeof CommandRoot & {
3
- Root: typeof CommandRoot;
4
- Input: typeof CommandInput;
5
- List: typeof CommandList;
6
- Item: typeof CommandItem;
7
- Empty: typeof CommandEmpty;
8
- Group: typeof CommandGroup;
9
- GroupLabel: typeof CommandGroupLabel;
10
- Separator: typeof CommandSeparator;
11
- Collection: typeof CommandCollection;
12
- Status: typeof CommandStatus;
13
- Clear: typeof CommandClear;
14
- useFilter: typeof useCommandFilter;
15
- };
16
- export * from "./command.js";
@@ -1,76 +0,0 @@
1
- "use client";
2
- import { Autocomplete } from "@base-ui/react/autocomplete";
3
- import { createContext, useContext, useState } from "react";
4
- import { jsx } from "react/jsx-runtime";
5
- // src/command/command.tsx
6
- const CommandReadOnly = createContext(false);
7
- /** An inline command list. Compose with Dialog when an overlay is needed. */
8
- function CommandRoot({ value, defaultValue = "", onValueChange, filter, autoHighlight = "always", keepHighlight = true, readOnly = false, ...props }) {
9
- const [query, setQuery] = useState(defaultValue);
10
- return /* @__PURE__ */ jsx(CommandReadOnly, {
11
- value: readOnly,
12
- children: /* @__PURE__ */ jsx(Autocomplete.Root, {
13
- ...props,
14
- readOnly,
15
- inline: true,
16
- open: true,
17
- autoHighlight,
18
- keepHighlight,
19
- filter: filter === false ? null : filter,
20
- value: value ?? query,
21
- onValueChange: (next, details) => {
22
- if (details.reason === "item-press") {
23
- details.cancel();
24
- return;
25
- }
26
- onValueChange?.(next, details);
27
- if (!details.isCanceled) setQuery(next);
28
- }
29
- })
30
- });
31
- }
32
- function CommandItem({ value, onClick, onSelect, ...props }) {
33
- const readOnly = useContext(CommandReadOnly);
34
- return /* @__PURE__ */ jsx(Autocomplete.Item, {
35
- ...props,
36
- value,
37
- onClick: (event) => {
38
- onClick?.(event);
39
- if (!readOnly && !event.defaultPrevented && !event.baseUIHandlerPrevented) onSelect?.(value);
40
- }
41
- });
42
- }
43
- function CommandInput({ onKeyDown, ...props }) {
44
- return /* @__PURE__ */ jsx(Autocomplete.Input, {
45
- ...props,
46
- onKeyDown: (event) => {
47
- onKeyDown?.(event);
48
- if (event.nativeEvent.isComposing || event.nativeEvent.keyCode === 229) event.preventBaseUIHandler();
49
- }
50
- });
51
- }
52
- const CommandList = Autocomplete.List;
53
- const CommandEmpty = Autocomplete.Empty;
54
- const CommandGroup = Autocomplete.Group;
55
- const CommandGroupLabel = Autocomplete.GroupLabel;
56
- const CommandSeparator = Autocomplete.Separator;
57
- const CommandCollection = Autocomplete.Collection;
58
- const CommandStatus = Autocomplete.Status;
59
- const CommandClear = Autocomplete.Clear;
60
- const useCommandFilter = Autocomplete.useFilter;
61
- // src/command/index.ts
62
- const Command = Object.assign(CommandRoot, {
63
- Root: CommandRoot,
64
- Input: CommandInput,
65
- List: CommandList,
66
- Item: CommandItem,
67
- Empty: CommandEmpty,
68
- Group: CommandGroup,
69
- GroupLabel: CommandGroupLabel,
70
- Separator: CommandSeparator,
71
- Collection: CommandCollection,
72
- Status: CommandStatus,
73
- Clear: CommandClear,
74
- useFilter: useCommandFilter
75
- });
76
- export { Command, CommandClear, CommandCollection, CommandEmpty, CommandGroup, CommandGroupLabel, CommandInput, CommandItem, CommandList, CommandRoot, CommandSeparator, CommandStatus, useCommandFilter };
@@ -1,117 +0,0 @@
1
- import { type ColumnDef, type CellSelectionState, type RowData, type ReactTable, type SortingState, type TableOptions } from "@tanstack/react-table";
2
- export type DataGridValue = string | number | boolean | null;
3
- export type DataGridEditReason = "edit" | "paste" | "fill" | "programmatic";
4
- export interface DataGridCellChange<TRow> {
5
- rowId: string;
6
- rowIndex: number;
7
- columnId: string;
8
- previousValue: DataGridValue;
9
- value: DataGridValue;
10
- previousRow: TRow;
11
- row: TRow;
12
- reason: DataGridEditReason;
13
- }
14
- export interface DataGridRowChange<TRow> {
15
- rowId: string;
16
- rowIndex: number;
17
- previousRow: TRow;
18
- row: TRow;
19
- changes: readonly DataGridCellChange<TRow>[];
20
- reason: DataGridEditReason;
21
- }
22
- export interface DataGridColumn<TRow extends RowData, TValidationData = unknown> {
23
- id: string;
24
- header: string;
25
- getValue: (row: TRow) => DataGridValue;
26
- setValue?: (row: TRow, value: DataGridValue) => TRow;
27
- parse?: (input: string, row: TRow) => DataGridValue;
28
- validate?: (value: DataGridValue, row: TRow, context: DataGridValidationContext<TRow, TValidationData>) => string | null;
29
- editable?: boolean | ((row: TRow) => boolean);
30
- sortable?: boolean;
31
- width?: number;
32
- minWidth?: number;
33
- maxWidth?: number;
34
- pinned?: boolean;
35
- onCellEditComplete?: (change: DataGridCellChange<TRow>) => void;
36
- tableColumn?: Omit<Partial<ColumnDef<typeof dataGridFeatures, TRow, unknown>>, "id" | "header" | "accessorFn">;
37
- }
38
- export interface DataGridEdit {
39
- rowId: string;
40
- columnId: string;
41
- value: DataGridValue;
42
- }
43
- export interface DataGridCellEvent<TRow> {
44
- rowId: string;
45
- columnId: string;
46
- onEditComplete: (change: DataGridCellChange<TRow>) => void;
47
- }
48
- export type DataGridCommitResult<TRow> = {
49
- ok: true;
50
- changes: readonly DataGridCellChange<TRow>[];
51
- } | {
52
- ok: false;
53
- error: string;
54
- edit: DataGridEdit;
55
- };
56
- export interface UseDataGridOptions<TRow extends RowData, TValidationData = unknown> {
57
- rows: readonly TRow[];
58
- columns: readonly DataGridColumn<TRow, TValidationData>[];
59
- getRowId: (row: TRow) => string;
60
- readOnly?: boolean;
61
- rowSelection?: boolean;
62
- cellSelection?: boolean;
63
- sorting?: boolean;
64
- onRowsChange?: (rows: TRow[], changes: readonly DataGridCellChange<TRow>[]) => void;
65
- onCellEditComplete?: (change: DataGridCellChange<TRow>) => void;
66
- cellEvents?: readonly DataGridCellEvent<TRow>[];
67
- onRowEditComplete?: (change: DataGridRowChange<TRow>) => void;
68
- onRowSelectionChange?: (rowIds: ReadonlySet<string>) => void;
69
- onCellSelectionChange?: (ranges: CellSelectionState) => void;
70
- onSortingChange?: (sorting: SortingState) => void;
71
- validationData?: TValidationData;
72
- tableOptions?: DataGridTableOptions<TRow>;
73
- }
74
- export declare const dataGridFeatures: {
75
- cellSelectionFeature: import("@tanstack/react-table").TableFeature;
76
- columnSizingFeature: import("@tanstack/react-table").TableFeature;
77
- columnResizingFeature: import("@tanstack/react-table").TableFeature;
78
- rowSelectionFeature: import("@tanstack/react-table").TableFeature;
79
- rowSortingFeature: import("@tanstack/react-table").TableFeature;
80
- sortedRowModel: (table: import("@tanstack/react-table").Table<any, any>) => () => import("@tanstack/react-table").RowModel<any, any>;
81
- sortFns: {
82
- alphanumeric: import("@tanstack/react-table").CreatedSortFn<any, any>;
83
- basic: import("@tanstack/react-table").CreatedSortFn<any, any>;
84
- datetime: import("@tanstack/react-table").CreatedSortFn<any, any>;
85
- text: import("@tanstack/react-table").CreatedSortFn<any, any>;
86
- };
87
- };
88
- export type DataGridTable<TRow extends RowData> = ReactTable<typeof dataGridFeatures, TRow>;
89
- export type DataGridTableOptions<TRow extends RowData> = Partial<Omit<TableOptions<typeof dataGridFeatures, TRow>, "features" | "columns" | "data" | "getRowId">>;
90
- export interface DataGridValidationContext<TRow extends RowData, TValidationData = unknown> {
91
- rowId: string;
92
- rowIndex: number;
93
- columnId: string;
94
- rows: readonly TRow[];
95
- originalRows: readonly TRow[];
96
- changes: readonly DataGridCellChange<TRow>[];
97
- reason: DataGridEditReason;
98
- data: TValidationData | undefined;
99
- table: DataGridTable<TRow>;
100
- }
101
- export declare function useDataGrid<TRow extends RowData, TValidationData = unknown>({ rows, columns, getRowId, readOnly, rowSelection, cellSelection, sorting, onRowsChange, onCellEditComplete, cellEvents, onRowEditComplete, onRowSelectionChange, onCellSelectionChange, onSortingChange, validationData, tableOptions, }: UseDataGridOptions<TRow, TValidationData>): {
102
- table: ReactTable<{
103
- cellSelectionFeature: import("@tanstack/react-table").TableFeature;
104
- columnSizingFeature: import("@tanstack/react-table").TableFeature;
105
- columnResizingFeature: import("@tanstack/react-table").TableFeature;
106
- rowSelectionFeature: import("@tanstack/react-table").TableFeature;
107
- rowSortingFeature: import("@tanstack/react-table").TableFeature;
108
- sortedRowModel: (table: import("@tanstack/react-table").Table<any, any>) => () => import("@tanstack/react-table").RowModel<any, any>;
109
- sortFns: {
110
- alphanumeric: import("@tanstack/react-table").CreatedSortFn<any, any>;
111
- basic: import("@tanstack/react-table").CreatedSortFn<any, any>;
112
- datetime: import("@tanstack/react-table").CreatedSortFn<any, any>;
113
- text: import("@tanstack/react-table").CreatedSortFn<any, any>;
114
- };
115
- }, TRow, import("@tanstack/react-table").TableState_CellSelection & import("@tanstack/react-table").TableState_ColumnResizing & import("@tanstack/react-table").TableState_ColumnSizing & import("@tanstack/react-table").TableState_RowSelection & import("@tanstack/react-table").TableState_RowSorting>;
116
- commitCells: (edits: readonly DataGridEdit[], reason?: DataGridEditReason) => DataGridCommitResult<TRow>;
117
- };