Skip to content

Commit

Permalink
Adds itemValue prop for custom field view components to allow value…
Browse files Browse the repository at this point in the history
…-driven field conditions (#8619)

Co-authored-by: Daniel Cousens <[email protected]>
  • Loading branch information
jim-lake and dcousens authored Jul 31, 2023
1 parent 935d9fa commit 6a8f1dd
Show file tree
Hide file tree
Showing 18 changed files with 188 additions and 224 deletions.
5 changes: 5 additions & 0 deletions .changeset/add-condition-fields.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': minor
---

Adds `itemValue` prop for custom field view components to allow value-driven field conditions
20 changes: 1 addition & 19 deletions docs/scripts/replace-show-next-release/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
import fs from 'fs/promises';
import { ValidateError } from '@markdoc/markdoc';
import { globby } from 'globby';
import { loadAllMarkdoc } from '../../markdoc/load-all';
import { printValidationError } from '../../markdoc';
import { removeNextReleaseConditions } from './markdoc';
import { replaceShowNextRelease } from './typescript';

async function updateTsFiles() {
const paths = await globby('pages/**/*.{ts,tsx}');

console.log(`updating ${paths.length} Typescript files`);
await Promise.all(
paths.map(async path => {
const source = await fs.readFile(path, 'utf8');

if (!source.includes('process.env.SHOW_NEXT_RELEASE')) return;

console.log(` updating ${path}`);
await fs.writeFile(path, await replaceShowNextRelease(path, source));
})
);
}

async function updateMarkdocFiles() {
const docs = await loadAllMarkdoc();
Expand Down Expand Up @@ -48,5 +30,5 @@ async function updateMarkdocFiles() {
}

(async () => {
await Promise.all([updateTsFiles(), updateMarkdocFiles()]);
await updateMarkdocFiles();
})();
112 changes: 0 additions & 112 deletions docs/scripts/replace-show-next-release/typescript.test.ts

This file was deleted.

75 changes: 0 additions & 75 deletions docs/scripts/replace-show-next-release/typescript.ts

This file was deleted.

7 changes: 3 additions & 4 deletions examples/custom-field/1-text-field/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import {
} from '@keystone-6/core/types';
import { graphql } from '@keystone-6/core';

export type TextFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
CommonFieldConfig<ListTypeInfo> & {
isIndexed?: boolean | 'unique';
};
type TextFieldConfig<ListTypeInfo extends BaseListTypeInfo> = CommonFieldConfig<ListTypeInfo> & {
isIndexed?: boolean | 'unique';
};

export function text<ListTypeInfo extends BaseListTypeInfo>({
isIndexed,
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-field/1-text-field/views.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function Field({ field, value, onChange, autoFocus }: FieldProps<typeof c
}

export const Cell: CellComponent = ({ item, field, linkTo }) => {
let value = item[field.path] + '';
const value = item[field.path] + '';
return linkTo ? <CellLink {...linkTo}>{value}</CellLink> : <CellContainer>{value}</CellContainer>;
};
Cell.supportsLinkTo = true;
Expand Down
9 changes: 4 additions & 5 deletions examples/custom-field/2-stars-field/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import { graphql } from '@keystone-6/core';
// and a different input in the Admin UI
// https://github.com/keystonejs/keystone/tree/main/packages/core/src/fields/types/integer

export type StarsFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
CommonFieldConfig<ListTypeInfo> & {
isIndexed?: boolean | 'unique';
maxStars?: number;
};
type StarsFieldConfig<ListTypeInfo extends BaseListTypeInfo> = CommonFieldConfig<ListTypeInfo> & {
isIndexed?: boolean | 'unique';
maxStars?: number;
};

export const stars =
<ListTypeInfo extends BaseListTypeInfo>({
Expand Down
3 changes: 1 addition & 2 deletions examples/custom-field/3-pair-field-json/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
} from '@keystone-6/core/types';
import { graphql } from '@keystone-6/core';

export type PairFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
CommonFieldConfig<ListTypeInfo>;
type PairFieldConfig<ListTypeInfo extends BaseListTypeInfo> = CommonFieldConfig<ListTypeInfo>;

type PairInput = {
left: string | null | undefined;
Expand Down
3 changes: 1 addition & 2 deletions examples/custom-field/3-pair-field-nested/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
} from '@keystone-6/core/types';
import { graphql } from '@keystone-6/core';

export type PairFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
CommonFieldConfig<ListTypeInfo>;
type PairFieldConfig<ListTypeInfo extends BaseListTypeInfo> = CommonFieldConfig<ListTypeInfo>;

type PairInput = {
left: string | null | undefined;
Expand Down
3 changes: 1 addition & 2 deletions examples/custom-field/3-pair-field/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
} from '@keystone-6/core/types';
import { graphql } from '@keystone-6/core';

export type PairFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
CommonFieldConfig<ListTypeInfo>;
type PairFieldConfig<ListTypeInfo extends BaseListTypeInfo> = CommonFieldConfig<ListTypeInfo>;

type PairInput = string;
type PairOutput = string;
Expand Down
56 changes: 56 additions & 0 deletions examples/custom-field/4-conditional-field/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
BaseListTypeInfo,
fieldType,
FieldTypeFunc,
CommonFieldConfig,
orderDirectionEnum,
} from '@keystone-6/core/types';
import { graphql } from '@keystone-6/core';

type TextFieldConfig<ListTypeInfo extends BaseListTypeInfo> = CommonFieldConfig<ListTypeInfo> & {
isIndexed?: boolean | 'unique';
dependency: {
field: string;
minimumValue: number;
};
};

export function feedback<ListTypeInfo extends BaseListTypeInfo>({
isIndexed,
dependency,
...config
}: TextFieldConfig<ListTypeInfo>): FieldTypeFunc<ListTypeInfo> {
return meta =>
fieldType({
kind: 'scalar',
mode: 'optional',
scalar: 'String',
index: isIndexed === true ? 'index' : isIndexed || undefined,
})({
...config,
input: {
create: {
arg: graphql.arg({ type: graphql.String }),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
resolve(value, context) {
return value;
},
},
update: { arg: graphql.arg({ type: graphql.String }) },
orderBy: { arg: graphql.arg({ type: orderDirectionEnum }) },
},
output: graphql.field({
type: graphql.String,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
resolve({ value, item }, args, context, info) {
return value;
},
}),
views: './4-conditional-field/views',
getAdminMeta() {
return {
dependency,
};
},
});
}
Loading

0 comments on commit 6a8f1dd

Please sign in to comment.