Skip to content

Commit

Permalink
fix(payload): properly handles 0 value number fields in list view (#…
Browse files Browse the repository at this point in the history
…7364)

## Description

Fixes #5510 

- [x] I have read and understand the
[CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md)
document in this repository.

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

## Checklist:

- [x] Existing test suite passes locally with my changes
  • Loading branch information
PatrikKozak committed Jul 25, 2024
1 parent c57591b commit 5321098
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const DefaultCell: React.FC<Props> = (props) => {
<WrapElement {...wrapElementProps}>
<CodeCell
collection={collection}
data={`ID: ${cellData}`}
data={`ID: ${String(cellData)}`}
field={field as CodeField}
nowrap
rowData={rowData}
Expand All @@ -68,13 +68,22 @@ const DefaultCell: React.FC<Props> = (props) => {
)
}

let CellComponent: React.FC<CellComponentProps> = cellData && cellComponents[field.type]
let CellComponent: React.FC<CellComponentProps> | false =
(cellData || typeof cellData === 'boolean') &&
cellData !== null &&
typeof cellData !== 'undefined' &&
cellComponents[field.type]

if (!CellComponent) {
if (collection.upload && fieldAffectsData(field) && field.name === 'filename') {
CellComponent = cellComponents.File
} else {
if (!cellData && 'label' in field) {
if (
(cellData === undefined ||
cellData === null ||
(typeof cellData === 'string' && cellData.trim() === '')) &&
'label' in field
) {
return (
<WrapElement {...wrapElementProps}>
{t('noLabel', {
Expand All @@ -85,7 +94,7 @@ const DefaultCell: React.FC<Props> = (props) => {
})}
</WrapElement>
)
} else if (typeof cellData === 'string' || typeof cellData === 'number') {
} else if (['number', 'string'].includes(typeof cellData)) {
return <WrapElement {...wrapElementProps}>{cellData}</WrapElement>
} else if (typeof cellData === 'object') {
return <WrapElement {...wrapElementProps}>{JSON.stringify(cellData)}</WrapElement>
Expand All @@ -95,7 +104,9 @@ const DefaultCell: React.FC<Props> = (props) => {

return (
<WrapElement {...wrapElementProps}>
<CellComponent collection={collection} data={cellData} field={field} rowData={rowData} />
{CellComponent ? (
<CellComponent collection={collection} data={cellData} field={field} rowData={rowData} />
) : null}
</WrapElement>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type Props = {
onClick?: (Props) => void
rowData: {
[path: string]: unknown
id: number | string
}
}

Expand Down

0 comments on commit 5321098

Please sign in to comment.