-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
DataViews: Type the ViewActions component #61729
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. |
@@ -190,12 +190,12 @@ export interface ViewTable extends ViewBase { | |||
/** | |||
* The field to use as the primary field. | |||
*/ | |||
primaryField: string; | |||
primaryField?: string; |
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.
When switching view "type", I had to reset the "layout" object because that object is supposed to be different from one view type to another. This forced me to mark all the layout subkeys as optional.
I'm guessing the other options is to have some kind of "defaultLayout" provided per view type but I believe this approach is simpler and does the job for now.
@@ -58,10 +98,15 @@ function ViewTypeMenu( { view, onChangeView, supportedLayouts } ) { | |||
name="view-actions-available-view" | |||
checked={ availableView.type === view.type } | |||
hideOnClick | |||
onChange={ ( e ) => { | |||
onChange={ ( e: ChangeEvent< HTMLInputElement > ) => { |
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 main challenge of this PR is this function: Basically how to keep the "view" object valid (correct type) when switching view types.
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.
Transitional states are difficult, this seems to highlight complexity that wasn't apparent, although it seems like the complexity isn't related to types as much as it is to application states. What should that layout look like?
If the layout properties are truly optional then this makes sense and may be better than the previous behavior where dataviews would maintain layout properties when switching views.
I know this isn't your question, but I try to avoid type assertions like these and would typically do something like this here:
onChange={ ( e: ChangeEvent< HTMLInputElement > ) => {
switch ( e.target.value ) {
case 'list':
case 'grid':
case 'table':
return onChangeView( {
...view,
type: e.target.value,
layout: {},
} );
}
throw new Error( 'Invalid dataview' );
} }
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 agree the complexity is on the app level: "how to switch view types when the view types can be different" and we don't have a good answer yet.
Size Change: +1.61 kB (+0.09%) Total Size: 1.75 MB
ℹ️ View Unchanged
|
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 have a lot of relevant feedback here, it would be good to get review from someone more familiar with dataviews.
@@ -58,10 +98,15 @@ function ViewTypeMenu( { view, onChangeView, supportedLayouts } ) { | |||
name="view-actions-available-view" | |||
checked={ availableView.type === view.type } | |||
hideOnClick | |||
onChange={ ( e ) => { | |||
onChange={ ( e: ChangeEvent< HTMLInputElement > ) => { |
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.
Transitional states are difficult, this seems to highlight complexity that wasn't apparent, although it seems like the complexity isn't related to types as much as it is to application states. What should that layout look like?
If the layout properties are truly optional then this makes sense and may be better than the previous behavior where dataviews would maintain layout properties when switching views.
I know this isn't your question, but I try to avoid type assertions like these and would typically do something like this here:
onChange={ ( e: ChangeEvent< HTMLInputElement > ) => {
switch ( e.target.value ) {
case 'list':
case 'grid':
case 'table':
return onChangeView( {
...view,
type: e.target.value,
layout: {},
} );
}
throw new Error( 'Invalid dataview' );
} }
Co-authored-by: youknowriad <youknowriad@git.wordpress.org> Co-authored-by: sirreal <jonsurrell@git.wordpress.org>
Related #55083
Follow-up to #61185
What?
The DataViews package is a types heavy package. There's a lot of structures that need to be documented properly. So this continues the effort to add explicit types to the package. This PR focuses on typing the ViewActions component.
There's some interesting challenges here, more inline.
Testing instructions
There's a small functional change here (layout is reset when switching view types) but it shouldn't have impacts on the dataviews. You can check the existing usage.