Created
October 30, 2023 12:37
-
-
Save tyteen4a03/cdfc519aceb11eb95307a94e26f24625 to your computer and use it in GitHub Desktop.
PayloadCMS ColoredSelectCell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
name: "jobStatus", | |
type: "select", | |
required: true, | |
defaultValue: "toBeAssigned", | |
options: [ | |
{ | |
label: "To Be Assigned", | |
value: "toBeAssigned", | |
}, | |
{ | |
label: "Ready To Go", | |
value: "readyToGo", | |
}, | |
{ | |
label: "In Progress", | |
value: "inProgress", | |
}, | |
{ | |
label: "Completed", | |
value: "completed", | |
}, | |
{ | |
label: "Cancelled", | |
value: "cancelled", | |
}, | |
], | |
custom: { | |
colors: { | |
toBeAssigned: { | |
pillStyle: "dark", | |
}, | |
readyToGo: { | |
pillStyle: "error", | |
}, | |
inProgress: { | |
pillStyle: "warning", | |
}, | |
completed: { | |
pillStyle: "success", | |
}, | |
cancelled: { | |
pillStyle: "light", | |
}, | |
}, | |
}, | |
admin: { | |
components: { | |
Cell: ColoredSelectCell, | |
}, | |
}, | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.colored-select-cell { | |
display: flex; | |
flex-direction: row; | |
gap: 0.25rem; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Props as PillProps } from "payload/dist/admin/components/elements/Pill/types"; | |
import { Props } from "payload/dist/admin/components/views/collections/List/Cell/types"; | |
import React from "react"; | |
import { useTranslation } from "react-i18next"; | |
import { OptionObject, SelectField, optionsAreObjects } from "payload/types"; | |
import { getTranslation } from "payload/utilities"; | |
import { Pill } from "payload/components"; | |
import "./index.scss"; | |
interface ColoredSelectValue { | |
label: string; | |
value: string; | |
pillStyle: PillProps["pillStyle"]; | |
} | |
const Index: React.FC<Props & { field: SelectField; cellData: string | string[] }> = (props) => { | |
const { field, cellData } = props; | |
const { i18n } = useTranslation(); | |
const findLabel = (items: string[]): ColoredSelectValue[] => | |
items.map((i) => { | |
const colorConfig = field.custom?.colors[i]; | |
const found = (field.options as OptionObject[]).filter((f: OptionObject) => f.value === i)?.[0]?.label; | |
return { | |
label: getTranslation(found, i18n), | |
value: i, | |
pillStyle: colorConfig?.pillStyle ?? "light-gray", | |
}; | |
}); | |
let content = []; | |
if (optionsAreObjects(field.options)) { | |
content = Array.isArray(cellData) | |
? findLabel(cellData) // hasMany | |
: findLabel([cellData]); | |
} else { | |
content = Array.isArray(cellData) ? cellData : [cellData]; | |
} | |
return ( | |
<div className="colored-status-cell"> | |
{content.map(({ label, value, pillStyle }) => ( | |
<Pill key={value} draggable={false} pillStyle={pillStyle}> | |
{label} | |
</Pill> | |
))} | |
</div> | |
); | |
}; | |
export default Index; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: