Skip to content

Commit 1578cd2

Browse files
chore(ui): added selected option as a class to list table cell (#11750)
In the Cell component for a select field such as our `_status` fields it will now add a class eg. `selected--published` for the selected option so it can be easily targeted with CSS. --------- Co-authored-by: Dan Ribbens <[email protected]>
1 parent 5ae5255 commit 1578cd2

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

packages/ui/src/elements/Table/DefaultCell/index.tsx

+14
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,19 @@ export const DefaultCell: React.FC<DefaultCellComponentProps> = (props) => {
148148
}
149149
}
150150

151+
if ((field.type === 'select' || field.type === 'radio') && field.options.length && cellData) {
152+
const classes = Array.isArray(cellData)
153+
? cellData.map((value) => `selected--${value}`).join(' ')
154+
: `selected--${cellData}`
155+
156+
const className = [wrapElementProps.className, classes].filter(Boolean).join(' ')
157+
158+
return (
159+
<WrapElement {...wrapElementProps} className={className}>
160+
{CellComponent}
161+
</WrapElement>
162+
)
163+
}
164+
151165
return <WrapElement {...wrapElementProps}>{CellComponent}</WrapElement>
152166
}

test/admin/collections/Posts.ts

+31
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,37 @@ export const Posts: CollectionConfig = {
205205
position: 'sidebar',
206206
},
207207
},
208+
{
209+
type: 'radio',
210+
name: 'wavelengths',
211+
defaultValue: 'fm',
212+
options: [
213+
{
214+
label: 'FM',
215+
value: 'fm',
216+
},
217+
{
218+
label: 'AM',
219+
value: 'am',
220+
},
221+
],
222+
},
223+
{
224+
type: 'select',
225+
name: 'selectField',
226+
hasMany: true,
227+
defaultValue: ['option1', 'option2'],
228+
options: [
229+
{
230+
label: 'Option 1',
231+
value: 'option1',
232+
},
233+
{
234+
label: 'Option 2',
235+
value: 'option2',
236+
},
237+
],
238+
},
208239
],
209240
labels: {
210241
plural: slugPluralLabel,

test/admin/e2e/list-view/e2e.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ describe('List View', () => {
413413
await expect(whereBuilder.locator('.condition__value input')).toHaveValue('')
414414
})
415415

416+
// eslint-disable-next-line playwright/expect-expect
416417
test('should remove condition from URL when value is cleared', async () => {
417418
await page.goto(postsUrl.list)
418419

@@ -1285,6 +1286,31 @@ describe('List View', () => {
12851286
await expect(page.locator('#heading-_status')).toBeVisible()
12861287
await expect(page.locator('.cell-_status').first()).toBeVisible()
12871288

1289+
await toggleColumn(page, {
1290+
columnLabel: 'Wavelengths',
1291+
targetState: 'on',
1292+
columnName: 'wavelengths',
1293+
})
1294+
1295+
await toggleColumn(page, {
1296+
columnLabel: 'Select Field',
1297+
targetState: 'on',
1298+
columnName: 'selectField',
1299+
})
1300+
1301+
// check that the cells have the classes added per value selected
1302+
await expect(
1303+
page.locator('.cell-_status').first().locator("[class*='selected--']"),
1304+
).toBeVisible()
1305+
1306+
await expect(
1307+
page.locator('.cell-wavelengths').first().locator("[class*='selected--']"),
1308+
).toBeVisible()
1309+
1310+
await expect(
1311+
page.locator('.cell-selectField').first().locator("[class*='selected--']"),
1312+
).toBeVisible()
1313+
12881314
// sort by title again in descending order
12891315
await page.locator('#heading-title button.sort-column__desc').click()
12901316
await page.waitForURL(/sort=-title/)

0 commit comments

Comments
 (0)