-
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: Remove redundant setSelection prop #63648
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. |
For clarity, this would set an array of items or an array of ids? |
An array of ids, I didn't change the type. |
Size Change: -17 B (0%) Total Size: 1.75 MB
ℹ️ View Unchanged
|
Oh you're right I did change the types :P |
setSelectionState( newValue ); | ||
} | ||
if ( onChangeSelection ) { | ||
onChangeSelection( newValue ); |
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't we just do something like this now:
const setSelectionWithChange = onChangeSelection || setSelectionState
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.
no we can't, because sometimes you want the selection to be uncontrolled (you don't pass any selection prop) but you want to be notified about selection changes so you provide a onChangeSelection
prop.
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 do we want this? Can't we always force it to be controlled if you want to be notified?
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 me, it's a good idea to support this, just like regular inputs or any raw form element in React. You can pass the value to make it controlled otherwise you can use onChange
to be notified.
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.
Doesn't it log a warning in that case about an uncontrolled value?
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.
No, it logs a warning only when you switch from controlled to uncontrolled (or the opposite)
const [ openedFilter, setOpenedFilter ] = useState< string | null >( null ); | ||
|
||
function setSelectionWithChange( value: SelectionOrUpdater ) { | ||
const newValue = | ||
typeof value === 'function' ? value( selection ) : value; | ||
onChangeSelection( | ||
data.filter( ( item ) => newValue.includes( getItemId( item ) ) ) | ||
); |
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.
Aren't we now passing the items? Previously we very extracting the ids and now we don't?
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.
No, it's the opposite, we're now passing "strings" while we previously were passing "items".
The extra logic here is to filter items that are in the selection but not in the data. The problem though is that this filtering doesn't really make sense when calling onChange
since we're calling it from the "inside" meaning we know the ids are in the data already.
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.
Oh right, I actually changed it internally to IDs before, I forgot :D
Co-authored-by: youknowriad <youknowriad@git.wordpress.org> Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Follow-up to #63087
What?
Just removes a redundant
setSelection
prop from the DataViews component. We can do the same thing in theonChangeSelection
callback.Testing Instructions
Check that you can select items properly in the different dataviews.