Skip to content

Commit

Permalink
Update initialColumns default (#5623)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed May 5, 2021
1 parent 4839322 commit b0a72a1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-needles-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/admin-ui': patch
---

Improved the default value for `ui.initialColumns` to show the first three fields, rather than just the label field.
2 changes: 1 addition & 1 deletion docs/pages/apis/schema.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Options:
Can be overridden by per-field values in the `field.ui.listView.fieldMode` config.
See the [Fields API](./fields) for details.
Can be one of `['read', 'hidden']`, or an async function with an argument `{ session }` that returns one of `['read', 'hidden']`.
- `initialColumns` (default: `[labelField]`). A list of field names to display in columns in the list view. By default only the label column, as determined by `labelField`, is shown.
- `initialColumns` (default: The first three fields defined in the list). A list of field names to display in columns in the list view. By default only the label column, as determined by `labelField`, is shown.
- `initialSort` (default: `undefined`): Sets the field and direction to be used to initially sort the data in the list view.
Option `field` is the name of the field to sort by, and `direction` is either `'ASC'` or `'DESC'` for ascending and descending sorting respectively.
If undefined then data will be unsorted.
Expand Down
20 changes: 19 additions & 1 deletion packages-next/admin-ui/src/system/createAdminMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ export function createAdminMeta(config: KeystoneConfig, keystone: BaseKeystone)
: listConfig.fields.title
? 'title'
: 'id');

let initialColumns: string[];
if (listConfig.ui?.listView?.initialColumns) {
// If they've asked for a particular thing, give them that thing
initialColumns = listConfig.ui.listView.initialColumns as string[];
} else {
// Otherwise, we'll start with the labelField on the left and then add
// 2 more fields to the right of that. We don't include the 'id' field
// unless it happened to be the labelField
initialColumns = [
labelField,
...Object.keys(listConfig.fields)
.filter(fieldKey => listConfig.fields[fieldKey].config.access?.read !== false)
.filter(fieldKey => fieldKey !== labelField)
.filter(fieldKey => fieldKey !== 'id'),
].slice(0, 3);
}

adminMetaRoot.listsByKey[key] = {
key,
labelField,
Expand All @@ -32,7 +50,7 @@ export function createAdminMeta(config: KeystoneConfig, keystone: BaseKeystone)
path: list.adminUILabels.path,
fields: [],
pageSize: listConfig.ui?.listView?.pageSize ?? 50,
initialColumns: (listConfig.ui?.listView?.initialColumns as string[]) ?? [labelField],
initialColumns,
initialSort:
(listConfig.ui?.listView?.initialSort as
| { field: string; direction: 'ASC' | 'DESC' }
Expand Down

1 comment on commit b0a72a1

@vercel
Copy link

@vercel vercel bot commented on b0a72a1 May 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.