CARVIEW |
Navigation Menu
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Dataforms: Add object configuration support for Edit property with prefix/suffix options #71582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The story's working nicely for me:

Just left a comment about the types, and whether we might want to make them more granular so that they're aware of the control being used and which props are available for that particular control. Also in the discussion on the linked issue, it sounds like folks were leaning toward using a control
key instead of type
?
Otherwise, I'm liking the direction of this, feels like a good path forward to me!
/** | ||
* Edit configuration object for advanced control options. | ||
*/ | ||
export type EditConfig = { | ||
/** | ||
* The type of control to use. | ||
*/ | ||
type: string; | ||
/** | ||
* Number of rows for textarea controls. | ||
*/ | ||
rows?: number; | ||
/** | ||
* Prefix component for text controls. | ||
*/ | ||
prefix?: React.ComponentType | React.ReactElement; | ||
/** | ||
* Suffix component for text controls. | ||
*/ | ||
suffix?: React.ReactElement; | ||
/** | ||
* Additional configuration properties. | ||
*/ | ||
[ key: string ]: any; | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the types, should we go more granular, as suggested in #71560 (comment)? For example, prefix
shouldn't be allowed on the textarea
control, and rows
should only be allowed on the textarea control.
packages/dataviews/src/types.ts
Outdated
/** | ||
* Additional configuration properties. | ||
*/ | ||
[ key: string ]: any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the conversation in the linked issue, I don’t think we want this, right? I believe we want to carefully consider adding new properties to avoid competing with the existing Field API.
type: 'text', | ||
Edit: 'textarea', | ||
}, | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for thinking of updating the examples. Can we move this to the field-types story instead? We can update the field type text
to include examples with all of this (as well as any other field type affected).
The textarea
doesn't have an equivalent field type, but we can add it to the field type text
as well.
Flaky tests detected in 079f1c5. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/17760639707
|
prefix, | ||
suffix, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The render
has a config object that includes extra information for certain fields (e.g., when a field is marked as "media" it gets the sizes from the layout). Can we follow suit here as well?
createConfiguredControl
would pass the proper config
object to the underlying control depending on its type (all the EditConfig
properties but type
).
export type EditConfigGeneric = { | ||
type: Exclude< FieldType, 'text' | 'textarea' >; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes sense to have the exclusion and be speciic so IDE show the correct values when we are over EditConfigGeneric.
packages/dataviews/src/types.ts
Outdated
* Edit configuration for textarea controls. | ||
*/ | ||
export type EditConfigTextarea = { | ||
type: 'textarea'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What others suggested: just rename from type
to control
to avoid overusing type
.
icon: prefix, | ||
suffix, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we normalize the ValidatedText
component (and the EditConfig type) so it exposes prefix and suffix props and both are the same type?
Both should work the same (receive an icon component or an element). I've noticed prefix receives an icon, but suffix receives an element. I'm leaning to allow an element through any prefix/suffix, but I'd like to gather @mirka thoughts on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The advantage of being an element is that it can be interactive. For example, a consumer could pass a prefix/suffix that opens a modal on click to do something, ala units control. This is something that could help address some use cases, but I wonder if it's a pattern we want to promote/support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @oandregal the code was updated now we have simple suffix and prefix props.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm leaning to allow an element through any prefix/suffix
👍
I believe you've noticed how we want different paddings depending on the slot content — icons and controls look good with 8px padding, plain text with 12px, and then sometimes you could want a full-bleed background color in the slot, which will call for 0px padding (not that common though).
So there will be a few ways to deal with that here. Either:
- Bake that into the DataForm as another configuration (e.g.
slotType: 'control'
). - Recommend consumers to use the wrapper components.
- Document the suggested padding values in the DataForm documentation and expect the consumers to apply those values manually in CSS.
In either case I think DataForm itself now needs to have sufficient documentation on how to apply standard paddings according to the slot content. If that sounds like too much, I think it's fair to draw a line and say we're not going to support that level of customization out of the box 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And as a heads up, separately from this InputControl
case, similar responsibility boundary issues are likely to pop up in custom rendered select controls and combobox controls, where there will be a lot of possible customization options at the component level. I know we have concerns about back compat and feature bloat in the DataForm system, so we'll definitely need to draw a hard line somewhere in exactly how much the DataForm is going to support in its own API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good points @mirka, I guess for this initial version it is better to keep it simple and allow prefix and suffix but don't expose the different padding configurations, and leave it to the consumers to use InputControl...Wrapper explicitly as it is the most flexible and simple approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the context, Lena!
Agreed to using the slots without further Edit config. @jorgefilipecosta when you add the README for this Edit config, could you add a note about this? I think just what the input component uses would be fine (copy/paste from the input's stories for prefix and suffix):
By default, the prefix/suffix is aligned with the edge of the input border, with no padding. If you want to apply standard padding in accordance with the size variant, wrap the element in the provided component.
@mirka, I don't see that we explain the differences between the variants (icon vs control), and, as far as I read, it doesn't make any difference. Is that correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see that we explain the differences between the variants (icon vs control), and, as far as I read, it doesn't make any difference. Is that correct?
We do explain variants in the story and in the prop docs, if that's what you're referring to.
And yes the spacings for icon
and control
are the same. I just separated the variant names for ergonomics. (In v2, they may be merged into a single variant with a more generic name like minimal
.)
Hi @oandregal thank you for the reviews, I think I addressed everything. |
const DollarPrefix = () => ( | ||
<InputControlPrefixWrapper variant="control"> | ||
<span>$</span> | ||
</InputControlPrefixWrapper> | ||
); | ||
const PercentSuffix = () => ( | ||
<InputControlSuffixWrapper variant="control"> | ||
<span>%</span> | ||
</InputControlSuffixWrapper> | ||
); | ||
const USDSuffix = () => ( | ||
<InputControlSuffixWrapper variant="control"> | ||
<span>USD</span> | ||
</InputControlSuffixWrapper> | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: These are text content, not controls, so they would use the "default" variant. Otherwise they will look a bit off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, it was fixed
04812af
to
daeb535
Compare
<span>$</span> | ||
</InputControlPrefixWrapper> | ||
); | ||
const PercentSuffix = () => ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make one of these use an icon to demonstrate the usage of the icon variant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer if we move the story examples to the FieldTypes story.
Other than that, this is working great. Thanks, Jorge!
b9d80d1
to
a9620ed
Compare
There's a CI failure because every DataViews PR needs a changelog entry, with a link to the PR. |
); | ||
}; | ||
|
||
export const TextWithPrefixSuffix = ( { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this story? The new fields are going to be picked up by the text fields story. I'd prefer to keep the stories organized by field type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was removed 👍
4f4844a
to
079f1c5
Compare
This PR extends the DataForm
Edit
property to support object configuration, enabling advanced control options like configuring textarea rows, text input prefixes, and suffixes. This provides a more flexible and declarative API for customizing form controls.Fixes: #71560
Changes Made
Core Infrastructure:
EditConfig
type: Added support for object-based Edit configuration withtype
,rows
,prefix
,suffix
, and extensible propertiesgetControl()
function to handle object configurations via newcreateConfiguredControl()
HOC patternControl Enhancements:
rows
configuration support for customizable textarea heightprefix
andsuffix
support for input decorationsDocumentation & Examples:
Usage Examples
Technical Details
isEditConfig()
type guard for runtime type checkingEditConfig
type supports additional properties via index signatureTesting
Go to https://localhost:50240/?path=/story/dataviews-dataform--default, and verify the fields Long Description, Price with Prefix, Percentage with Suffix, Price with Prefix and Suffix, work as expected.