Skip to content

Commit

Permalink
Add context argument to various ui config functions (#6746)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Oct 8, 2021
1 parent 0ef1ee3 commit d64bd4a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
14 changes: 14 additions & 0 deletions .changeset/heavy-timers-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@keystone-next/keystone': minor
---

The following config functions now also access a `context` argument:
- `list.ui.isHidden`
- `list.ui.hideCreate`
- `list.ui.hideDelete`
- `list.ui.createView.defaultFieldMode`
- `list.ui.itemView.defaultFieldMode`
- `list.ui.listView.defaultFieldMode`
- `field.ui.createView.fieldMode`
- `field.ui.itemView.fieldMode`
- `field.ui.listView.fieldMode`
12 changes: 6 additions & 6 deletions docs/pages/docs/apis/fields.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ Options:
- `ui`: Controls how the field is displayed in the Admin UI.
- `views`: A [resolved](https://nodejs.org/api/modules.html#modules_require_resolve_request_options) path to a module containing code to replace or extend the default Admin UI components for this field. See the [Custom Field Views](../guides/custom-field-views) guide for details on how to use this option.
- `createView` (default: `'edit'`): Controls the create view page of the Admin UI.
Can be one of `['edit', 'hidden']`, or an async function with an argument `{ session }` that returns one of `['edit', 'hidden']`.
Can be one of `['edit', 'hidden']`, or an async function with an argument `{ session, context }` that returns one of `['edit', 'hidden']`.
Defaults to the list's `ui.createView.defaultFieldMode` config if defined.
See the [Schema API](./schema#ui) for details.
- `itemView` (default: `'edit'`): Controls the item view page of the Admin UI.
Can be one of `['edit', 'read', 'hidden']`, or an async function with an argument `{ session, item }` that returns one of `['edit', 'read', 'hidden']`.
Can be one of `['edit', 'read', 'hidden']`, or an async function with an argument `{ session, context, item }` that returns one of `['edit', 'read', 'hidden']`.
Defaults to the list's `ui.itemView.defaultFieldMode` config if defined.
See the [Schema API](./schema#ui) for details.
- `listView` (default: `'read'`): Controls the list view page of the Admin UI.
Can be one of `['read', 'hidden']`, or an async function with an argument `{ session }` that returns one of `['read', 'hidden']`.
Can be one of `['read', 'hidden']`, or an async function with an argument `{ session, context }` that returns one of `['read', 'hidden']`.
Defaults to the list's `ui.listView.defaultFieldMode` config if defined.
See the [Schema API](./schema#ui) for details.
- `graphql`: Configures certain aspects of the GraphQL API.
Expand All @@ -109,13 +109,13 @@ export default config({
ui: {
views: require.resolve('path/to/viewsModule.tsx'),
createView: {
fieldMode: ({ session }) => 'edit',
fieldMode: ({ session, context }) => 'edit',
},
itemView: {
fieldMode: ({ session, item }) => 'read',
fieldMode: ({ session, context, item }) => 'read',
},
listView: {
fieldMode: ({ session }) => 'read',
fieldMode: ({ session, context }) => 'read',
},
},
graphql: {
Expand Down
24 changes: 12 additions & 12 deletions docs/pages/docs/apis/schema.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,26 @@ Options:
By default, the `labelField` is used if it has a string `contains` filter, otherwise none.
- `description` (default: `undefined`): Sets the list description displayed in the Admin UI.
- `isHidden` (default: `false`): Controls whether the list is visible in the navigation elements of the Admin UI.
Can be either a boolean value, or an async function with an argument `{ session }` that returns a boolean value.
Can be either a boolean value, or an async function with an argument `{ session, context }` that returns a boolean value.
- `hideCreate` (default: `false`): Controls whether the `create` button is available in the Admin UI for this list.
Can be either a boolean value, or an async function with an argument `{ session }` that returns a boolean value.
Can be either a boolean value, or an async function with an argument `{ session, context }` that returns a boolean value.
- `hideDelete` (default: `false`): Controls whether the `delete` button is available in the Admin UI for this list.
Can be either a boolean value, or an async function with an argument `{ session }` that returns a boolean value.
Can be either a boolean value, or an async function with an argument `{ session, context }` that returns a boolean value.
- `createView`: Controls the create view page of the Admin UI.
- `defaultFieldMode` (default: `'edit'`):
Can be overridden by per-field values in the `field.ui.createView.fieldMode` config.
See the [Fields API](./fields#common-configuration) for details.
Can be one of `['edit', 'hidden']`, or an async function with an argument `{ session }` that returns one of `['edit', 'hidden']`.
Can be one of `['edit', 'hidden']`, or an async function with an argument `{ session, context }` that returns one of `['edit', 'hidden']`.
- `itemView`: Controls the item view page of the Admin UI.
- `defaultFieldMode` (default: `'edit'`):
Can be overridden by per-field values in the `field.ui.itemView.fieldMode` config.
See the [Fields API](./fields#common-configuration) for details.
Can be one of `['edit', 'read', 'hidden']`, or an async function with an argument `{ session, item }` that returns one of `['edit', 'read', 'hidden']`.
Can be one of `['edit', 'read', 'hidden']`, or an async function with an argument `{ session, context, item }` that returns one of `['edit', 'read', 'hidden']`.
- `listView`: Controls the list view page of the Admin UI.
- `defaultFieldMode` (default: `'read'`): Controls the default mode of fields in the list view.
Can be overridden by per-field values in the `field.ui.listView.fieldMode` config.
See the [Fields API](./fields#common-configuration) for details.
Can be one of `['read', 'hidden']`, or an async function with an argument `{ session }` that returns one of `['read', 'hidden']`.
Can be one of `['read', 'hidden']`, or an async function with an argument `{ session, context }` that returns one of `['read', 'hidden']`.
- `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.
Expand All @@ -119,17 +119,17 @@ export default config({
labelField: 'name',
searchFields: ['name', 'alternativeName'],
description: '...',
isHidden: ({ session }) => false,
hideCreate: ({ session }) => false,
hideDelete: ({ session }) => false,
isHidden: ({ session, context }) => false,
hideCreate: ({ session, context }) => false,
hideDelete: ({ session, context }) => false,
createView: {
defaultFieldMode: ({ session }) => 'edit',
defaultFieldMode: ({ session, context }) => 'edit',
},
itemView: {
defaultFieldMode: ({ session, item }) => 'edit',
defaultFieldMode: ({ session, context, item }) => 'edit',
},
listView: {
defaultFieldMode: ({ session }) => 'read',
defaultFieldMode: ({ session, context }) => 'read',
initialColumns: ['name', /* ... */],
initialSort: { field: 'name', direction: 'ASC' },
pageSize: 50,
Expand Down
26 changes: 21 additions & 5 deletions packages/keystone/src/admin-ui/system/getAdminMetaSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ export function getAdminMetaSchema({
const sessionFunction =
lists[rootVal.listKey].fields[rootVal.fieldPath].ui?.createView?.fieldMode ??
listConfig.ui?.createView?.defaultFieldMode;
return runMaybeFunction(sessionFunction, 'edit', { session: context.session });
return runMaybeFunction(sessionFunction, 'edit', {
session: context.session,
context,
});
},
}),
},
Expand Down Expand Up @@ -161,7 +164,10 @@ export function getAdminMetaSchema({
const sessionFunction =
lists[rootVal.listKey].fields[rootVal.fieldPath].ui?.listView?.fieldMode ??
listConfig.ui?.listView?.defaultFieldMode;
return runMaybeFunction(sessionFunction, 'read', { session: context.session });
return runMaybeFunction(sessionFunction, 'read', {
session: context.session,
context,
});
},
}),
},
Expand Down Expand Up @@ -225,6 +231,7 @@ export function getAdminMetaSchema({
}
return runMaybeFunction(sessionFunction, 'edit', {
session: context.session,
context,
item,
});
});
Expand Down Expand Up @@ -274,7 +281,10 @@ export function getAdminMetaSchema({
);
}
const listConfig = config.lists[rootVal.key];
return runMaybeFunction(listConfig.ui?.hideCreate, false, { session: context.session });
return runMaybeFunction(listConfig.ui?.hideCreate, false, {
session: context.session,
context,
});
},
}),
hideDelete: graphql.field({
Expand All @@ -286,7 +296,10 @@ export function getAdminMetaSchema({
);
}
const listConfig = config.lists[rootVal.key];
return runMaybeFunction(listConfig.ui?.hideDelete, false, { session: context.session });
return runMaybeFunction(listConfig.ui?.hideDelete, false, {
session: context.session,
context,
});
},
}),
path: graphql.field({ type: graphql.nonNull(graphql.String) }),
Expand All @@ -312,7 +325,10 @@ export function getAdminMetaSchema({
);
}
const listConfig = config.lists[rootVal.key];
return runMaybeFunction(listConfig.ui?.isHidden, false, { session: context.session });
return runMaybeFunction(listConfig.ui?.isHidden, false, {
session: context.session,
context,
});
},
}),
},
Expand Down
4 changes: 3 additions & 1 deletion packages/keystone/src/types/config/lists.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CacheHint } from 'apollo-server-types';
import type { BaseGeneratedListTypes, MaybePromise } from '../utils';
import type { KeystoneContext } from '../context';
import type { ListHooks } from './hooks';
import type { ListAccessControl } from './access-control';
import type { BaseFields, FilterOrderArgs } from './fields';
Expand Down Expand Up @@ -172,12 +173,13 @@ export type ListAdminUIConfig<

export type MaybeSessionFunction<T extends string | boolean> =
| T
| ((args: { session: any }) => MaybePromise<T>);
| ((args: { session: any; context: KeystoneContext }) => MaybePromise<T>);

export type MaybeItemFunction<T> =
| T
| ((args: {
session: any;
context: KeystoneContext;
item: { id: string | number; [path: string]: any };
}) => MaybePromise<T>);

Expand Down

0 comments on commit d64bd4a

Please sign in to comment.