Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const fields = [
label: 'Date as options',
type: 'datetime' as const,
elements: [
{ value: '', label: 'Select a date' },
{ value: '1970-02-23T12:00:00', label: "Jane's birth date" },
{ value: '1950-02-23T12:00:00', label: "John's birth date" },
],
Expand Down
29 changes: 18 additions & 11 deletions packages/dataviews/src/dataform-controls/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,24 @@ export default function Select< Item >( {
[ id, onChange ]
);

const elements = [
/*
* Value can be undefined when:
*
* - the field is not required
* - in bulk editing
*
*/
{ label: __( 'Select item' ), value: '' },
...( field?.elements ?? [] ),
];
const fieldElements = field?.elements ?? [];
const hasEmptyValue = fieldElements.some(
( { value: elementValue } ) => elementValue === ''
);

const elements = hasEmptyValue
? fieldElements
: [
/*
* Value can be undefined when:
*
* - the field is not required
* - in bulk editing
*
*/
{ label: __( 'Select item' ), value: '' },

@oandregal oandregal Jul 30, 2025 •

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.

What if we customized the default empty state by using the existing field.label? If the field's label is "Date", the empty item could be "Select date" (lowercase and without an article to avoid grammar mistakes).

Providing your own custom element would be a minor use case (fine to still support it).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes that could also work but there may be issues with casing?.

Do you want me to go ahead with adding Select %s, or just merge this as is?

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.

I think using sentence case here would be fine, at least I can't come up with an example where lowercasing the field's label would be problematic. In any case, this would be just the default. The field authors would be able to override it by providing its own empty state, if it was wrong – so that'd be fine.

Given is a small change, I think I'd do it in this PR to have a better default.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@oandregal Thinking about it some more I think it might be a worse default. There is no guarantee the label will be the noun. For a simplistic label like "Country", it would work as "Select country" but labels could be sentences. For example I could label my input "Select a country". That would in term make the default "Select select a country". I think this is too unpredictable. We should keep the generic term and leave the default in the hands of the consumer.

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.

All the labels I've seen in practice are a noun and, after this PR, there's the ability to override, so I don't think it'd be an issue.

It's fine if you'd rather ship this PR as it is, we can revisit this idea in the future should it come to it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yup lets do that 👍🏻 Thanks

...fieldElements,
];

return (
<SelectControl
Expand Down