@dxos/react-list 0.1.23 → 0.1.41
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.
- package/README.md +1 -22
- package/dist/lib/browser/index.mjs +147 -493
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +229 -0
- package/dist/lib/node/index.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -0
- package/dist/types/src/List.d.ts +42 -0
- package/dist/types/src/List.d.ts.map +1 -0
- package/dist/types/src/ListItem.d.ts +56 -0
- package/dist/types/src/ListItem.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/package.json +24 -18
- package/src/List.tsx +87 -0
- package/src/ListItem.tsx +228 -0
- package/src/index.ts +2 -2
- package/dist/types/src/components/List/List.d.ts +0 -8
- package/dist/types/src/components/List/List.d.ts.map +0 -1
- package/dist/types/src/components/List/List.stories.d.ts +0 -17
- package/dist/types/src/components/List/List.stories.d.ts.map +0 -1
- package/dist/types/src/components/List/index.d.ts +0 -2
- package/dist/types/src/components/List/index.d.ts.map +0 -1
- package/dist/types/src/components/ListPrimitive/ListPrimitive.d.ts +0 -55
- package/dist/types/src/components/ListPrimitive/ListPrimitive.d.ts.map +0 -1
- package/dist/types/src/components/ListPrimitive/ListPrimitive.stories.d.ts +0 -31
- package/dist/types/src/components/ListPrimitive/ListPrimitive.stories.d.ts.map +0 -1
- package/dist/types/src/components/ListPrimitive/index.d.ts +0 -2
- package/dist/types/src/components/ListPrimitive/index.d.ts.map +0 -1
- package/dist/types/src/components/index.d.ts +0 -2
- package/dist/types/src/components/index.d.ts.map +0 -1
- package/dist/types/src/hooks/index.d.ts +0 -2
- package/dist/types/src/hooks/index.d.ts.map +0 -1
- package/dist/types/src/hooks/usePropStatefully.d.ts +0 -6
- package/dist/types/src/hooks/usePropStatefully.d.ts.map +0 -1
- package/dist/types/src/model.d.ts +0 -3
- package/dist/types/src/model.d.ts.map +0 -1
- package/src/components/List/List.stories.tsx +0 -123
- package/src/components/List/List.tsx +0 -111
- package/src/components/List/index.ts +0 -5
- package/src/components/ListPrimitive/ListPrimitive.stories.tsx +0 -41
- package/src/components/ListPrimitive/ListPrimitive.tsx +0 -474
- package/src/components/ListPrimitive/index.ts +0 -5
- package/src/components/index.ts +0 -5
- package/src/hooks/index.ts +0 -5
- package/src/hooks/usePropStatefully.ts +0 -33
- package/src/model.ts +0 -6
|
@@ -1,474 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2022 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import debounce from 'lodash.debounce';
|
|
6
|
-
import { Minus, Plus } from 'phosphor-react';
|
|
7
|
-
import React, { ComponentProps, useCallback, useMemo, useState, KeyboardEvent } from 'react';
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
defaultFocus,
|
|
11
|
-
Input,
|
|
12
|
-
useTranslation,
|
|
13
|
-
getSize,
|
|
14
|
-
Button,
|
|
15
|
-
randomString,
|
|
16
|
-
defaultGroup,
|
|
17
|
-
defaultHover,
|
|
18
|
-
mx
|
|
19
|
-
} from '@dxos/react-components';
|
|
20
|
-
|
|
21
|
-
import { usePropStatefully } from '../../hooks';
|
|
22
|
-
|
|
23
|
-
type ListId = string;
|
|
24
|
-
type ListItemId = string;
|
|
25
|
-
|
|
26
|
-
export interface ListItemProps {
|
|
27
|
-
id: ListItemId;
|
|
28
|
-
title?: string;
|
|
29
|
-
description?: string;
|
|
30
|
-
annotations?: Record<string, string | boolean>;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export type ListItems = Record<ListItemId, Omit<ListItemProps, 'id'>>;
|
|
34
|
-
|
|
35
|
-
interface SharedListActionProps {
|
|
36
|
-
listId: ListId;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
interface SharedListItemActionProps extends SharedListActionProps {
|
|
40
|
-
listItemId: ListItemId;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export interface ListItemChangedAction extends SharedListItemActionProps {
|
|
44
|
-
next: Partial<ListItemProps>;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export interface ListChangedAction extends SharedListActionProps {
|
|
48
|
-
next: Partial<ListPrimitiveProps>;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface ListItemCreatedAction extends SharedListActionProps {
|
|
52
|
-
created: Pick<ListItemProps, 'id'> & Partial<Omit<ListItemProps, 'id'>>;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export interface ListItemDeletedAction extends SharedListActionProps {
|
|
56
|
-
deleted: Pick<ListItemProps, 'id'> & Partial<Omit<ListItemProps, 'id'>>;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export type ListAction = ListItemChangedAction | ListChangedAction | ListItemCreatedAction | ListItemDeletedAction;
|
|
60
|
-
|
|
61
|
-
export const isListItemChangedAction = (o: any): o is ListItemChangedAction => 'listItemId' in o;
|
|
62
|
-
export const isListItemCreatedAction = (o: any): o is ListItemCreatedAction => 'created' in o;
|
|
63
|
-
export const isListItemDeletedAction = (o: any): o is ListItemDeletedAction => 'deleted' in o;
|
|
64
|
-
|
|
65
|
-
export interface ListPrimitiveProps {
|
|
66
|
-
id: ListId;
|
|
67
|
-
title?: string;
|
|
68
|
-
description?: string;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export interface ListPrimitiveComponentProps extends ListPrimitiveProps, Omit<ComponentProps<'div'>, 'id'> {
|
|
72
|
-
items: ListItems;
|
|
73
|
-
onAction?: (action: ListAction) => void;
|
|
74
|
-
createListItemId?: () => Promise<ListItemId>;
|
|
75
|
-
onChangePeriod?: number;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export interface ListItemPrimitiveComponentProps extends ListItemProps, Omit<ComponentProps<'li'>, 'id' | 'title'> {
|
|
79
|
-
updateItem: (item: ListItemProps) => void;
|
|
80
|
-
updateOrder: (id: ListItemId, delta: number) => void;
|
|
81
|
-
deleteItem: (id: ListItemId) => void;
|
|
82
|
-
createItem: () => void;
|
|
83
|
-
orderIndex: number;
|
|
84
|
-
isLast?: boolean;
|
|
85
|
-
autoFocus?: boolean;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const defaultCreateListItemId = async () => randomString(8);
|
|
89
|
-
|
|
90
|
-
const ListItemPrimitive = ({
|
|
91
|
-
id,
|
|
92
|
-
title: propsTitle,
|
|
93
|
-
description: propsDescription,
|
|
94
|
-
annotations: propsAnnotations,
|
|
95
|
-
updateItem,
|
|
96
|
-
deleteItem,
|
|
97
|
-
createItem,
|
|
98
|
-
updateOrder,
|
|
99
|
-
orderIndex,
|
|
100
|
-
isLast,
|
|
101
|
-
autoFocus
|
|
102
|
-
}: ListItemPrimitiveComponentProps) => {
|
|
103
|
-
const checkId = `${id}__checkbox`;
|
|
104
|
-
const labelId = `${id}__title`;
|
|
105
|
-
const descriptionId = `${id}__description`;
|
|
106
|
-
|
|
107
|
-
const { t } = useTranslation('appkit');
|
|
108
|
-
|
|
109
|
-
const rejectTextUpdatesWhenFocused = useCallback(
|
|
110
|
-
(a: string, b: string) => {
|
|
111
|
-
return document.activeElement?.getAttribute('data-itemid') === id ? true : a === b;
|
|
112
|
-
},
|
|
113
|
-
[id]
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
const [title, setTitle, titleSession] = usePropStatefully<string>(propsTitle ?? '', rejectTextUpdatesWhenFocused);
|
|
117
|
-
|
|
118
|
-
const [description, _setDescription, _descriptionSession] = usePropStatefully<string>(
|
|
119
|
-
propsDescription ?? '',
|
|
120
|
-
rejectTextUpdatesWhenFocused
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
const [annotations, setAnnotations] = useState(propsAnnotations ?? {});
|
|
124
|
-
const isDone = annotations?.state === 'done';
|
|
125
|
-
|
|
126
|
-
const onChangeTitle = useCallback((nextValue: string) => {
|
|
127
|
-
setTitle(nextValue);
|
|
128
|
-
updateItem({ id, title: nextValue });
|
|
129
|
-
}, []);
|
|
130
|
-
|
|
131
|
-
const _onChangeDescription = useCallback((nextValue: string) => {
|
|
132
|
-
_setDescription(nextValue);
|
|
133
|
-
updateItem({ id, description: nextValue });
|
|
134
|
-
}, []);
|
|
135
|
-
|
|
136
|
-
const _onClickMoveUp = useCallback(() => {
|
|
137
|
-
updateOrder(id, -1);
|
|
138
|
-
}, [updateOrder, id]);
|
|
139
|
-
const _onClickMoveDown = useCallback(() => {
|
|
140
|
-
updateOrder(id, 1);
|
|
141
|
-
}, [updateOrder, id]);
|
|
142
|
-
|
|
143
|
-
const onChangeCheckbox = useCallback(() => {
|
|
144
|
-
const nextAnnotations = { ...annotations, state: isDone ? 'init' : 'done' };
|
|
145
|
-
setAnnotations(nextAnnotations);
|
|
146
|
-
updateItem({ id, annotations: nextAnnotations });
|
|
147
|
-
}, [annotations, isDone]);
|
|
148
|
-
|
|
149
|
-
const onClickDelete = useCallback(() => {
|
|
150
|
-
deleteItem(id);
|
|
151
|
-
}, [deleteItem, id]);
|
|
152
|
-
|
|
153
|
-
const onKeyUp = useCallback(
|
|
154
|
-
(e: KeyboardEvent<HTMLInputElement>) => {
|
|
155
|
-
switch (e.key) {
|
|
156
|
-
case 'Enter':
|
|
157
|
-
if (!e.shiftKey) {
|
|
158
|
-
if (isLast) {
|
|
159
|
-
createItem();
|
|
160
|
-
} else {
|
|
161
|
-
(
|
|
162
|
-
document.querySelector(`input[data-orderindex="${orderIndex + 1}"]`) as HTMLElement | undefined
|
|
163
|
-
)?.focus();
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
break;
|
|
167
|
-
case 'PageDown':
|
|
168
|
-
(document.querySelector(`input[data-orderindex="${orderIndex + 1}"]`) as HTMLElement | undefined)?.focus();
|
|
169
|
-
break;
|
|
170
|
-
case 'PageUp':
|
|
171
|
-
(document.querySelector(`input[data-orderindex="${orderIndex - 1}"]`) as HTMLElement | undefined)?.focus();
|
|
172
|
-
break;
|
|
173
|
-
}
|
|
174
|
-
},
|
|
175
|
-
[orderIndex, isLast]
|
|
176
|
-
);
|
|
177
|
-
|
|
178
|
-
const inputSlots = useMemo(
|
|
179
|
-
() => ({
|
|
180
|
-
root: { className: 'm-0' },
|
|
181
|
-
input: {
|
|
182
|
-
className: 'border-0 rounded',
|
|
183
|
-
autoFocus: true,
|
|
184
|
-
'data-orderindex': orderIndex,
|
|
185
|
-
'data-itemid': id,
|
|
186
|
-
onKeyUp
|
|
187
|
-
}
|
|
188
|
-
}),
|
|
189
|
-
[orderIndex, id, onKeyUp]
|
|
190
|
-
);
|
|
191
|
-
|
|
192
|
-
return (
|
|
193
|
-
<li
|
|
194
|
-
key={id}
|
|
195
|
-
className='flex items-center gap-2 pli-4 mbe-2'
|
|
196
|
-
aria-labelledby={labelId}
|
|
197
|
-
{...(description && { 'aria-describedby': descriptionId })}
|
|
198
|
-
>
|
|
199
|
-
<input
|
|
200
|
-
{...{
|
|
201
|
-
type: 'checkbox',
|
|
202
|
-
id: checkId,
|
|
203
|
-
...(description && {
|
|
204
|
-
'aria-describedby': descriptionId
|
|
205
|
-
}),
|
|
206
|
-
className: mx(
|
|
207
|
-
getSize(5),
|
|
208
|
-
'text-primary-600 bg-neutral-50 rounded-full border-neutral-300 dark:bg-neutral-800 dark:border-neutral-600 cursor-pointer',
|
|
209
|
-
defaultFocus,
|
|
210
|
-
defaultHover({})
|
|
211
|
-
),
|
|
212
|
-
onChange: onChangeCheckbox,
|
|
213
|
-
checked: isDone
|
|
214
|
-
}}
|
|
215
|
-
/>
|
|
216
|
-
<label htmlFor={checkId} id={labelId} className='sr-only'>
|
|
217
|
-
{title}
|
|
218
|
-
</label>
|
|
219
|
-
<span id={descriptionId} className='sr-only'>
|
|
220
|
-
{description}
|
|
221
|
-
</span>
|
|
222
|
-
<div role='none' className='grow'>
|
|
223
|
-
<Input
|
|
224
|
-
key={titleSession}
|
|
225
|
-
label={t('list item title label')}
|
|
226
|
-
placeholder={t('list item title placeholder')}
|
|
227
|
-
labelVisuallyHidden
|
|
228
|
-
initialValue={title}
|
|
229
|
-
onChange={onChangeTitle}
|
|
230
|
-
slots={inputSlots}
|
|
231
|
-
/>
|
|
232
|
-
{/* TODO(thure): Re-enable this when descriptions become relevant */}
|
|
233
|
-
{/* <Input */}
|
|
234
|
-
{/* key={descriptionSession} */}
|
|
235
|
-
{/* label={t('list item description label')} */}
|
|
236
|
-
{/* placeholder={t('list item description placeholder')} */}
|
|
237
|
-
{/* labelVisuallyHidden */}
|
|
238
|
-
{/* initialValue={description} */}
|
|
239
|
-
{/* onChange={onChangeDescription} */}
|
|
240
|
-
{/* data-itemid={id} */}
|
|
241
|
-
{/* /> */}
|
|
242
|
-
</div>
|
|
243
|
-
{/* TODO(thure): Restore these, or implement drag & drop, when how best to change order in Echo is clarified. */}
|
|
244
|
-
{/* <ButtonGroup className='flex flex-col items-stretch'> */}
|
|
245
|
-
{/* <Button compact rounding='rounded-bs-md border-be-0' disabled={isFirst} onClick={onClickMoveUp}> */}
|
|
246
|
-
{/* <CaretUp /> */}
|
|
247
|
-
{/* <span className='sr-only'>{t('move list item up label')}</span> */}
|
|
248
|
-
{/* </Button> */}
|
|
249
|
-
{/* <Button compact rounding='rounded-be-md' disabled={isLast} onClick={onClickMoveDown}> */}
|
|
250
|
-
{/* <CaretDown /> */}
|
|
251
|
-
{/* <span className='sr-only'>{t('move list item down label')}</span> */}
|
|
252
|
-
{/* </Button> */}
|
|
253
|
-
{/* </ButtonGroup> */}
|
|
254
|
-
<Button onClick={onClickDelete} variant='ghost' className='p-1 self-stretch'>
|
|
255
|
-
<Minus className={getSize(4)} />
|
|
256
|
-
<span className='sr-only'>{t('delete list item label')}</span>
|
|
257
|
-
</Button>
|
|
258
|
-
</li>
|
|
259
|
-
);
|
|
260
|
-
};
|
|
261
|
-
|
|
262
|
-
const itemsEquivalent = (a: ListItems, b: ListItems) => {
|
|
263
|
-
const aKeys = Object.keys(a);
|
|
264
|
-
const bKeys = Object.keys(b);
|
|
265
|
-
for (let i = 0; i < aKeys.length; i++) {
|
|
266
|
-
const key = aKeys[i];
|
|
267
|
-
if (key !== bKeys[i] || a[key].title !== b[key].title || a[key].annotations?.state !== b[key].annotations?.state) {
|
|
268
|
-
return false;
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
return true;
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
export const ListPrimitive = ({
|
|
275
|
-
id: listId,
|
|
276
|
-
title: propsTitle,
|
|
277
|
-
description: propsDescription,
|
|
278
|
-
items: propsItems,
|
|
279
|
-
onAction,
|
|
280
|
-
createListItemId = defaultCreateListItemId,
|
|
281
|
-
onChangePeriod = 0,
|
|
282
|
-
...divProps
|
|
283
|
-
}: ListPrimitiveComponentProps) => {
|
|
284
|
-
const { t } = useTranslation('appkit');
|
|
285
|
-
|
|
286
|
-
const [creating, setCreating] = useState(false);
|
|
287
|
-
const [autoFocus, setAutofocus] = useState<string | null>(null);
|
|
288
|
-
|
|
289
|
-
const rejectTextUpdatesWhenFocused = useCallback(
|
|
290
|
-
(a: string, b: string) => {
|
|
291
|
-
return document.activeElement?.getAttribute('data-itemid') === listId ? true : a === b;
|
|
292
|
-
},
|
|
293
|
-
[listId]
|
|
294
|
-
);
|
|
295
|
-
|
|
296
|
-
// Title
|
|
297
|
-
|
|
298
|
-
const titleId = `${listId}__title`;
|
|
299
|
-
const [title, setTitle, titleSession] = usePropStatefully(propsTitle ?? '', rejectTextUpdatesWhenFocused);
|
|
300
|
-
const propagateTitle = useCallback(
|
|
301
|
-
(nextValue: string) => {
|
|
302
|
-
return onAction?.({ listId, next: { title: nextValue } });
|
|
303
|
-
},
|
|
304
|
-
[onAction]
|
|
305
|
-
);
|
|
306
|
-
const debouncedPropagateTitle = useMemo(() => debounce(propagateTitle, onChangePeriod), [propagateTitle]);
|
|
307
|
-
const onChangeTitle = useCallback(
|
|
308
|
-
(nextValue: string) => {
|
|
309
|
-
setTitle(nextValue);
|
|
310
|
-
debouncedPropagateTitle(nextValue);
|
|
311
|
-
},
|
|
312
|
-
[onAction, listId]
|
|
313
|
-
);
|
|
314
|
-
|
|
315
|
-
// Description
|
|
316
|
-
|
|
317
|
-
const descriptionId = `${listId}__description`;
|
|
318
|
-
const [description, setDescription, _descriptionSession] = usePropStatefully(
|
|
319
|
-
propsDescription ?? '',
|
|
320
|
-
rejectTextUpdatesWhenFocused
|
|
321
|
-
);
|
|
322
|
-
const propagateDescription = useCallback(
|
|
323
|
-
(nextValue: string) => onAction?.({ listId, next: { description: nextValue } }),
|
|
324
|
-
[onAction]
|
|
325
|
-
);
|
|
326
|
-
const debouncedPropagateDescription = useMemo(
|
|
327
|
-
() => debounce(propagateDescription, onChangePeriod),
|
|
328
|
-
[propagateDescription]
|
|
329
|
-
);
|
|
330
|
-
const _onChangeDescription = useCallback(
|
|
331
|
-
(nextValue: string) => {
|
|
332
|
-
setDescription(nextValue);
|
|
333
|
-
debouncedPropagateDescription(nextValue);
|
|
334
|
-
},
|
|
335
|
-
[onAction, listId]
|
|
336
|
-
);
|
|
337
|
-
|
|
338
|
-
// Items & order
|
|
339
|
-
|
|
340
|
-
const [items, setItems, itemsSession] = usePropStatefully(propsItems ?? {}, itemsEquivalent);
|
|
341
|
-
const order = Object.keys(items);
|
|
342
|
-
|
|
343
|
-
const propagateItem = useCallback(
|
|
344
|
-
({ id, ...next }: Pick<ListItemProps, 'id'> & Partial<Omit<ListItemProps, 'id'>>) => {
|
|
345
|
-
return onAction?.({ listId, listItemId: id, next });
|
|
346
|
-
},
|
|
347
|
-
[listId, onAction]
|
|
348
|
-
);
|
|
349
|
-
const debouncedPropagateItem = useMemo(() => debounce(propagateItem, onChangePeriod), [propagateItem]);
|
|
350
|
-
const updateItem = useCallback(
|
|
351
|
-
({ id, ...next }: Pick<ListItemProps, 'id'> & Partial<Omit<ListItemProps, 'id'>>) => {
|
|
352
|
-
setItems(Object.assign(items, { [id]: { ...items[id], ...next } }));
|
|
353
|
-
'title' in next && debouncedPropagateItem({ id, title: next.title });
|
|
354
|
-
'description' in next && debouncedPropagateItem({ id, description: next.description });
|
|
355
|
-
'annotations' in next && onAction?.({ listId, listItemId: id, next: { annotations: next.annotations } });
|
|
356
|
-
},
|
|
357
|
-
[items, listId, onAction]
|
|
358
|
-
);
|
|
359
|
-
|
|
360
|
-
const updateOrder = useCallback(
|
|
361
|
-
(id: ListItemId, delta: number) => {
|
|
362
|
-
const fromIndex = order.indexOf(id);
|
|
363
|
-
const toIndex = fromIndex + delta;
|
|
364
|
-
const nextOrder = Array.from(order);
|
|
365
|
-
nextOrder.splice(fromIndex, 1);
|
|
366
|
-
nextOrder.splice(toIndex, 0, id);
|
|
367
|
-
const nextItems = nextOrder.reduce((acc: ListItems, listItemId) => {
|
|
368
|
-
acc[listItemId] = items[listItemId];
|
|
369
|
-
return acc;
|
|
370
|
-
}, {});
|
|
371
|
-
setItems(nextItems);
|
|
372
|
-
onAction?.({ listId, next: {} });
|
|
373
|
-
},
|
|
374
|
-
[items, onAction]
|
|
375
|
-
);
|
|
376
|
-
|
|
377
|
-
const deleteItem = useCallback(
|
|
378
|
-
(id: ListItemId) => {
|
|
379
|
-
if (items[id]) {
|
|
380
|
-
const { [id]: deletedItem, ...nextItems } = items;
|
|
381
|
-
setItems(nextItems);
|
|
382
|
-
onAction?.({ listId, next: {}, deleted: { id, ...deletedItem } });
|
|
383
|
-
}
|
|
384
|
-
},
|
|
385
|
-
[items, onAction]
|
|
386
|
-
);
|
|
387
|
-
|
|
388
|
-
const createItem = useCallback(() => {
|
|
389
|
-
setCreating(true);
|
|
390
|
-
return createListItemId()
|
|
391
|
-
.then((id) => {
|
|
392
|
-
const next = {
|
|
393
|
-
items: { ...items, [id]: {} }
|
|
394
|
-
};
|
|
395
|
-
const created = { id };
|
|
396
|
-
setItems(next.items);
|
|
397
|
-
setAutofocus(id);
|
|
398
|
-
onAction?.({ listId, next: {}, created });
|
|
399
|
-
})
|
|
400
|
-
.finally(() => setCreating(false));
|
|
401
|
-
}, [items, createListItemId, onAction]);
|
|
402
|
-
|
|
403
|
-
// Render
|
|
404
|
-
|
|
405
|
-
const inputSlots = useMemo(
|
|
406
|
-
() => ({
|
|
407
|
-
root: { className: 'mli-2 mbs-0 mbe-2' },
|
|
408
|
-
input: {
|
|
409
|
-
className: 'border-0 rounded text-xl font-display font-semibold',
|
|
410
|
-
'data-itemid': listId
|
|
411
|
-
}
|
|
412
|
-
}),
|
|
413
|
-
[listId]
|
|
414
|
-
);
|
|
415
|
-
|
|
416
|
-
return (
|
|
417
|
-
<div
|
|
418
|
-
role='group'
|
|
419
|
-
{...divProps}
|
|
420
|
-
className={mx(defaultGroup({ elevation: 3, spacing: 'pbs-2 pbe-1' }), divProps.className)}
|
|
421
|
-
aria-labelledby={titleId}
|
|
422
|
-
{...(description && { 'aria-describedby': descriptionId })}
|
|
423
|
-
>
|
|
424
|
-
<span id={titleId} className='sr-only'>
|
|
425
|
-
{title}
|
|
426
|
-
</span>
|
|
427
|
-
{description && (
|
|
428
|
-
<span id={descriptionId} className='sr-only'>
|
|
429
|
-
{description}
|
|
430
|
-
</span>
|
|
431
|
-
)}
|
|
432
|
-
<Input
|
|
433
|
-
key={titleSession}
|
|
434
|
-
label={t('list title label')}
|
|
435
|
-
placeholder={t('list title placeholder')}
|
|
436
|
-
labelVisuallyHidden
|
|
437
|
-
initialValue={title}
|
|
438
|
-
onChange={onChangeTitle}
|
|
439
|
-
slots={inputSlots}
|
|
440
|
-
/>
|
|
441
|
-
{/* TODO(thure): Re-enable this when relevant */}
|
|
442
|
-
{/* <Input */}
|
|
443
|
-
{/* key={descriptionSession} */}
|
|
444
|
-
{/* label={t('list description label')} */}
|
|
445
|
-
{/* placeholder={t('list description placeholder')} */}
|
|
446
|
-
{/* labelVisuallyHidden */}
|
|
447
|
-
{/* initialValue={description} */}
|
|
448
|
-
{/* onChange={onChangeDescription} */}
|
|
449
|
-
{/* className='mli-4' */}
|
|
450
|
-
{/* data-itemid={listId} */}
|
|
451
|
-
{/* /> */}
|
|
452
|
-
<ol className='contents'>
|
|
453
|
-
{order.map((listItemId, index) => {
|
|
454
|
-
return (
|
|
455
|
-
<ListItemPrimitive
|
|
456
|
-
key={`${itemsSession}__${listItemId}`}
|
|
457
|
-
orderIndex={index}
|
|
458
|
-
isLast={index === order.length - 1}
|
|
459
|
-
{...{ id: listItemId, updateItem, updateOrder, deleteItem, createItem }}
|
|
460
|
-
{...items[listItemId]}
|
|
461
|
-
autoFocus={listItemId === autoFocus}
|
|
462
|
-
/>
|
|
463
|
-
);
|
|
464
|
-
})}
|
|
465
|
-
</ol>
|
|
466
|
-
<div role='none' className='mli-4 mlb-4'>
|
|
467
|
-
<Button className='is-full' onClick={createItem} disabled={creating}>
|
|
468
|
-
<Plus className={getSize(5)} />
|
|
469
|
-
<span className='sr-only'>{t('add list item label')}</span>
|
|
470
|
-
</Button>
|
|
471
|
-
</div>
|
|
472
|
-
</div>
|
|
473
|
-
);
|
|
474
|
-
};
|
package/src/components/index.ts
DELETED
package/src/hooks/index.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2022 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { useEffect, useRef, useState, Dispatch, SetStateAction } from 'react';
|
|
6
|
-
|
|
7
|
-
import { randomString } from '@dxos/react-components';
|
|
8
|
-
|
|
9
|
-
const isEqual = <T>(a: T, b: T) => a === b;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @deprecated
|
|
13
|
-
*/
|
|
14
|
-
export const usePropStatefully = <T>(
|
|
15
|
-
propsValue: T,
|
|
16
|
-
equivalentPredicate?: (a: T, b: T) => boolean
|
|
17
|
-
): [T, Dispatch<SetStateAction<T>>, string] => {
|
|
18
|
-
const [value, setValue] = useState<T>(propsValue);
|
|
19
|
-
const [valueSession, setValueSession] = useState(randomString());
|
|
20
|
-
const mounted = useRef(false);
|
|
21
|
-
const predicate = equivalentPredicate ?? isEqual<T>;
|
|
22
|
-
|
|
23
|
-
useEffect(() => {
|
|
24
|
-
if (mounted.current && !predicate(propsValue, value)) {
|
|
25
|
-
setValue(propsValue);
|
|
26
|
-
setValueSession(randomString());
|
|
27
|
-
} else {
|
|
28
|
-
mounted.current = true;
|
|
29
|
-
}
|
|
30
|
-
}, [propsValue]);
|
|
31
|
-
|
|
32
|
-
return [value, setValue, valueSession];
|
|
33
|
-
};
|