@workday/canvas-kit-react 7.1.4 → 7.2.0-next.2

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.
Files changed (30) hide show
  1. package/collection/index.ts +2 -0
  2. package/collection/lib/useBaseListModel.tsx +1 -1
  3. package/collection/lib/useCursorListModel.tsx +88 -20
  4. package/collection/lib/useGridModel.ts +34 -0
  5. package/collection/lib/useListItemRovingFocus.tsx +6 -4
  6. package/dist/commonjs/collection/index.d.ts +2 -0
  7. package/dist/commonjs/collection/index.d.ts.map +1 -1
  8. package/dist/commonjs/collection/index.js +5 -1
  9. package/dist/commonjs/collection/lib/useBaseListModel.js +1 -1
  10. package/dist/commonjs/collection/lib/useCursorListModel.d.ts +64 -101
  11. package/dist/commonjs/collection/lib/useCursorListModel.d.ts.map +1 -1
  12. package/dist/commonjs/collection/lib/useCursorListModel.js +64 -23
  13. package/dist/commonjs/collection/lib/useGridModel.d.ts +955 -0
  14. package/dist/commonjs/collection/lib/useGridModel.d.ts.map +1 -0
  15. package/dist/commonjs/collection/lib/useGridModel.js +52 -0
  16. package/dist/commonjs/collection/lib/useListItemRovingFocus.d.ts.map +1 -1
  17. package/dist/commonjs/collection/lib/useListItemRovingFocus.js +6 -4
  18. package/dist/es6/collection/index.d.ts +2 -0
  19. package/dist/es6/collection/index.d.ts.map +1 -1
  20. package/dist/es6/collection/index.js +2 -0
  21. package/dist/es6/collection/lib/useBaseListModel.js +1 -1
  22. package/dist/es6/collection/lib/useCursorListModel.d.ts +64 -101
  23. package/dist/es6/collection/lib/useCursorListModel.d.ts.map +1 -1
  24. package/dist/es6/collection/lib/useCursorListModel.js +64 -23
  25. package/dist/es6/collection/lib/useGridModel.d.ts +955 -0
  26. package/dist/es6/collection/lib/useGridModel.d.ts.map +1 -0
  27. package/dist/es6/collection/lib/useGridModel.js +49 -0
  28. package/dist/es6/collection/lib/useListItemRovingFocus.d.ts.map +1 -1
  29. package/dist/es6/collection/lib/useListItemRovingFocus.js +6 -4
  30. package/package.json +5 -5
@@ -59,8 +59,8 @@ exports.getLast = getLast;
59
59
  var getFirstOfRow = function (id, _a) {
60
60
  var state = _a.state;
61
61
  if (state.columnCount) {
62
- var item_1 = getItem(id, { state: state });
63
- var currentIndex = state.items.findIndex(function (i) { return useBaseListModel_1.defaultGetId(item_1) === useBaseListModel_1.defaultGetId(i); });
62
+ var item = getItem(id, { state: state });
63
+ var currentIndex = item.index;
64
64
  var offset = currentIndex % state.columnCount;
65
65
  return state.items[currentIndex - offset];
66
66
  }
@@ -73,10 +73,14 @@ exports.getFirstOfRow = getFirstOfRow;
73
73
  var getLastOfRow = function (id, _a) {
74
74
  var state = _a.state;
75
75
  if (state.columnCount) {
76
- var item_2 = getItem(id, { state: state });
77
- var currentIndex = state.items.findIndex(function (i) { return useBaseListModel_1.defaultGetId(item_2) === useBaseListModel_1.defaultGetId(i); });
76
+ var item = getItem(id, { state: state });
77
+ var currentIndex = item.index;
78
78
  var offset = (currentIndex % state.columnCount) - state.columnCount + 1;
79
- return state.items[currentIndex - offset];
79
+ var nextIndex = currentIndex - offset;
80
+ if (nextIndex >= state.items.length) {
81
+ nextIndex = state.items.length - 1;
82
+ }
83
+ return state.items[nextIndex];
80
84
  }
81
85
  return exports.getLast(id, { state: state });
82
86
  };
@@ -88,8 +92,8 @@ exports.getLastOfRow = getLastOfRow;
88
92
  var getPreviousPage = function (id, _a) {
89
93
  var state = _a.state;
90
94
  if (state.columnCount) {
91
- var item_3 = getItem(id, { state: state });
92
- var currentIndex = state.items.findIndex(function (i) { return useBaseListModel_1.defaultGetId(item_3) === useBaseListModel_1.defaultGetId(i); });
95
+ var item = getItem(id, { state: state });
96
+ var currentIndex = item.index;
93
97
  return state.items[currentIndex % state.columnCount];
94
98
  }
95
99
  return exports.getFirst(id, { state: state });
@@ -102,8 +106,8 @@ exports.getPreviousPage = getPreviousPage;
102
106
  var getNextPage = function (id, _a) {
103
107
  var state = _a.state;
104
108
  if (state.columnCount) {
105
- var item_4 = getItem(id, { state: state });
106
- var currentIndex = state.items.findIndex(function (i) { return useBaseListModel_1.defaultGetId(item_4) === useBaseListModel_1.defaultGetId(i); });
109
+ var item = getItem(id, { state: state });
110
+ var currentIndex = item.index;
107
111
  var lastRowIndex = state.items.length - state.columnCount;
108
112
  return state.items[lastRowIndex + (currentIndex % state.columnCount)];
109
113
  }
@@ -121,13 +125,40 @@ var getWrappingOffsetItem = function (offset) { return function (id, _a, tries)
121
125
  if (tries === void 0) { tries = state.items.length; }
122
126
  var items = state.items;
123
127
  var item = getItem(id, { state: state });
124
- var currentIndex = items.findIndex(function (i) { return useBaseListModel_1.defaultGetId(item) === useBaseListModel_1.defaultGetId(i); });
128
+ var currentIndex = item.index;
125
129
  var nextIndex = currentIndex + offset;
130
+ // calculate idealLength as in if the grid was a perfect rectangle
131
+ var rows = Math.ceil(items.length / state.columnCount);
132
+ var idealLength = rows * state.columnCount;
126
133
  if (nextIndex < 0) {
127
- nextIndex = items.length + nextIndex;
134
+ if (offset === -1) {
135
+ // if the offset is -1, we want to wrap to the end
136
+ nextIndex = items.length - 1;
137
+ }
138
+ else {
139
+ // if the offset is smaller than -1, we want to wrap by column
140
+ if (idealLength + nextIndex >= items.length) {
141
+ // we'll overflow the grid because there isn't enough items. Move `nextIndex` up so we wrap in
142
+ // the right spot
143
+ nextIndex -= state.columnCount;
144
+ }
145
+ nextIndex = idealLength + nextIndex;
146
+ }
128
147
  }
129
148
  else if (nextIndex >= items.length) {
130
- nextIndex = nextIndex - items.length;
149
+ if (offset === 1) {
150
+ // if the offset is 1, we want to wrap to the beginning
151
+ nextIndex = 0;
152
+ }
153
+ else {
154
+ // if the offset is larger than 1, we want to wrap by column
155
+ if (nextIndex - idealLength < 0) {
156
+ // we're going to overflow the grid because there isn't enough items. Move `nextIndex` down to
157
+ // the missing item in the next row. This way we'll end up wrapping in the right spot
158
+ nextIndex += state.columnCount;
159
+ }
160
+ nextIndex = nextIndex - idealLength;
161
+ }
131
162
  }
132
163
  if (state.nonInteractiveIds.includes(useBaseListModel_1.defaultGetId(items[nextIndex])) && tries > 0) {
133
164
  // The next item is disabled, try again, but only if we haven't already tried everything.
@@ -142,7 +173,7 @@ var getOffsetItem = function (offset) { return function (id, _a, tries) {
142
173
  if (tries === void 0) { tries = state.items.length; }
143
174
  var items = state.items, columnCount = state.columnCount;
144
175
  var item = getItem(id, { state: state });
145
- var currentIndex = items.findIndex(function (i) { return useBaseListModel_1.defaultGetId(item) === useBaseListModel_1.defaultGetId(i); });
176
+ var currentIndex = item.index;
146
177
  var nextIndex = currentIndex + offset;
147
178
  if (Math.abs(offset) < columnCount) {
148
179
  // if we're here, the columnCount is non-zero and the absolute value of offset is less than the
@@ -160,8 +191,13 @@ var getOffsetItem = function (offset) { return function (id, _a, tries) {
160
191
  if (nextRow < 0 || nextRow >= columnCount) {
161
192
  nextIndex = currentIndex;
162
193
  }
194
+ // make sure we don't go out of bounds if the grid isn't a perfect rectangle
195
+ if (nextIndex > items.length - 1) {
196
+ nextIndex = currentIndex;
197
+ }
163
198
  }
164
- else if (nextIndex < 0) {
199
+ // make sure we're always in bounds
200
+ if (nextIndex < 0) {
165
201
  nextIndex = 0;
166
202
  }
167
203
  else if (nextIndex >= items.length) {
@@ -176,7 +212,9 @@ var getOffsetItem = function (offset) { return function (id, _a, tries) {
176
212
  }; };
177
213
  exports.getOffsetItem = getOffsetItem;
178
214
  /**
179
- * The default navigation manager of lists
215
+ * The default navigation manager of lists. This navigation manager will wrap around when the edge
216
+ * is hit. For example, if the user is the the right-most item in a list or right-most item in a
217
+ * row, the cursor will wrap around to the beginning or the next row.
180
218
  */
181
219
  exports.wrappingNavigationManager = exports.createNavigationManager({
182
220
  getFirst: exports.getFirst,
@@ -185,18 +223,22 @@ exports.wrappingNavigationManager = exports.createNavigationManager({
185
223
  getNext: exports.getWrappingOffsetItem(1),
186
224
  getNextRow: function (id, _a) {
187
225
  var state = _a.state;
188
- return exports.getWrappingOffsetItem(-state.columnCount)(id, { state: state });
226
+ return exports.getWrappingOffsetItem(state.columnCount)(id, { state: state });
189
227
  },
190
228
  getPrevious: exports.getWrappingOffsetItem(-1),
191
229
  getPreviousRow: function (id, _a) {
192
230
  var state = _a.state;
193
- return exports.getWrappingOffsetItem(state.columnCount)(id, { state: state });
231
+ return exports.getWrappingOffsetItem(-state.columnCount)(id, { state: state });
194
232
  },
195
233
  getPreviousPage: exports.getPreviousPage,
196
234
  getNextPage: exports.getNextPage,
197
235
  getFirstOfRow: exports.getFirstOfRow,
198
236
  getLastOfRow: exports.getLastOfRow,
199
237
  });
238
+ /**
239
+ * The default navigation of grids. This navigation manager will not wrap, but will stop when an
240
+ * edge is detected. This could be the last item in a list or the last item of a row in a grid.
241
+ */
200
242
  exports.navigationManager = exports.createNavigationManager({
201
243
  getFirst: exports.getFirst,
202
244
  getLast: exports.getLast,
@@ -204,12 +246,12 @@ exports.navigationManager = exports.createNavigationManager({
204
246
  getNext: exports.getOffsetItem(1),
205
247
  getNextRow: function (id, _a) {
206
248
  var state = _a.state;
207
- return exports.getOffsetItem(-state.columnCount)(id, { state: state });
249
+ return exports.getOffsetItem(state.columnCount)(id, { state: state });
208
250
  },
209
251
  getPrevious: exports.getOffsetItem(-1),
210
252
  getPreviousRow: function (id, _a) {
211
253
  var state = _a.state;
212
- return exports.getOffsetItem(state.columnCount)(id, { state: state });
254
+ return exports.getOffsetItem(-state.columnCount)(id, { state: state });
213
255
  },
214
256
  getPreviousPage: exports.getPreviousPage,
215
257
  getNextPage: exports.getNextPage,
@@ -238,9 +280,7 @@ exports.useCursorListModel = common_1.createModelHook({
238
280
  * (left-to-right or right-to-left) and call the correct navigation method. In our example, a
239
281
  * left-to-right language would send a `getNext`. The navigation manager may return the next item
240
282
  * in the list. Different managers can be created for slightly different use cases. The default
241
- * navigation manager will accept `orientation` and directionality to determine mapping. If
242
- * `columnCount` is non-zero, it will use a grid mode where `orientation` is ignored and only
243
- * directionality matters.
283
+ * navigation manager will accept `orientation` and directionality to determine mapping.
244
284
  *
245
285
  * An example override might be a tab list with an overflow menu that is meant to be transparent
246
286
  * to screen reader users. This would require the overflow menu to accept both up/down keys as
@@ -254,15 +294,16 @@ exports.useCursorListModel = common_1.createModelHook({
254
294
  var columnCount = config.columnCount || 0;
255
295
  var list = useBaseListModel_1.useBaseListModel(config);
256
296
  var initialCurrentRef = react_1.default.useRef(config.initialCursorId || (((_a = config.items) === null || _a === void 0 ? void 0 : _a.length) ? useBaseListModel_1.defaultGetId(config.items[0]) : ''));
297
+ var navigation = config.navigation;
257
298
  var state = __assign(__assign({}, list.state), {
258
299
  /** The id of the list item the cursor is pointing to */
259
300
  cursorId: cursorId,
260
301
  /**
261
302
  * Any positive non-zero value treats the list like a grid with rows and columns
262
303
  * @default 0
304
+ * @private Use useGridModel instead to make a grid instead of a list
263
305
  */
264
306
  columnCount: columnCount });
265
- var navigation = config.navigation || exports.wrappingNavigationManager;
266
307
  var events = __assign(__assign({}, list.events), {
267
308
  /**
268
309
  * Register an item to the list. Takes in an identifier, a React.Ref and an optional index. This