Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions packages/keystatic/src/app/CollectionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -565,17 +565,42 @@ function CollectionTable(
...(hideStatusColumn ? [] : [statusCell]),
nameCell,
...collection.columns.map(column => {
let val;
val = item.data?.[column];
const raw = item.data?.[column];
const field = collection.schema[column];
const FieldCell =
field &&
'kind' in field &&
field.kind === 'form' &&
'Cell' in field &&
field.Cell;

if (raw == null) {
return (
<Cell
key={column + item.name}
textValue=""
>
<Text weight="medium">{undefined}</Text>
</Cell>
);
}

if (val == null) {
val = undefined;
} else {
val = val + '';
const strVal = raw + '';

if (FieldCell) {
return (
<Cell
key={column + item.name}
textValue={strVal}
>
<FieldCell value={raw} />
</Cell>
);
}

return (
<Cell key={column + item.name} textValue={val}>
<Text weight="medium">{val}</Text>
<Cell key={column + item.name} textValue={strVal}>
<Text weight="medium">{strVal}</Text>
</Cell>
);
}),
Expand All @@ -597,6 +622,7 @@ function CollectionTable(
);
}


function getItemPath(
basePath: string,
collection: string,
Expand Down
5 changes: 4 additions & 1 deletion packages/keystatic/src/form/api.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactElement, ReactNode } from 'react';
import { ReactElement, ReactNode, ComponentType } from 'react';
import { Glob } from '../config';

import { ChildField } from './fields/child';
Expand Down Expand Up @@ -48,6 +48,7 @@ export type BasicFormField<
parse(value: FormFieldStoredValue): ReaderValue;
};
label?: string;
Cell?: ComponentType<{ value: any }>;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

value should be ParsedValue here and etc.

};

export type SlugFormField<
Expand Down Expand Up @@ -85,6 +86,7 @@ export type SlugFormField<
): ReaderValueAsSlugField;
};
label?: string;
Cell?: ComponentType<{ value: any }>;
};

export type AssetFormField<
Expand Down Expand Up @@ -127,6 +129,7 @@ export type AssetFormField<
parse(value: FormFieldStoredValue): ReaderValue;
};
label?: string;
Cell?: ComponentType<{ value: any }>;
};

export type AssetsFormField<
Expand Down
1 change: 1 addition & 0 deletions packages/keystatic/src/form/fields/empty-field-ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export let SlugFieldInput = empty,
IntegerFieldInput = empty,
NumberFieldInput = empty,
ImageFieldInput = empty,
ImageCell = empty,
FileFieldInput = empty,
DatetimeFieldInput = empty,
DateFieldInput = empty,
Expand Down
3 changes: 2 additions & 1 deletion packages/keystatic/src/form/fields/image/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AssetFormField } from '../../api';
import { FieldDataError } from '../error';
import { RequiredValidation, assertRequired } from '../utils';
import { getSrcPrefix } from './getSrcPrefix';
import { ImageFieldInput } from '#field-ui/image';
import { ImageFieldInput, ImageCell } from '#field-ui/image';

export function image<IsRequired extends boolean | undefined>({
label,
Expand Down Expand Up @@ -34,6 +34,7 @@ export function image<IsRequired extends boolean | undefined>({
kind: 'form',
formKind: 'asset',
label,
Cell: ImageCell,
Input(props) {
return (
<ImageFieldInput
Expand Down
29 changes: 28 additions & 1 deletion packages/keystatic/src/form/fields/image/ui.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ButtonGroup, ActionButton } from '@keystar/ui/button';
import { FieldDescription, FieldLabel, FieldMessage } from '@keystar/ui/field';
import { Flex, Box } from '@keystar/ui/layout';
import { tokenSchema } from '@keystar/ui/style';
import { css, tokenSchema } from '@keystar/ui/style';
import { Text } from '@keystar/ui/typography';
import { TextField } from '@keystar/ui/text-field';

import { useIsInDocumentEditor } from '../document/DocumentEditor';
Expand Down Expand Up @@ -171,3 +172,29 @@ export function ImageFieldInput(
</Flex>
);
}

export function ImageCell({ value }: { value: string | null }) {
const [errored, setErrored] = useState(false);
if (!value) return null;
if (errored) {
return (
<Text color="neutralTertiary" size="small">
{value}
</Text>
);
}
return (
<img
src={value}
alt=""
onError={() => setErrored(true)}
className={css({
width: tokenSchema.size.scale[400],
height: tokenSchema.size.scale[400],
objectFit: 'cover',
borderRadius: tokenSchema.size.radius.small,
flexShrink: 0,
})}
/>
);
}