-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ import type { | |
ViewBaseProps, | ||
SupportedLayouts, | ||
} from './types'; | ||
import type { SetSelection, SelectionOrUpdater } from './private-types'; | ||
import type { SelectionOrUpdater } from './private-types'; | ||
|
||
type ItemWithId = { id: string }; | ||
|
||
|
@@ -50,16 +50,13 @@ type DataViewsProps< Item > = { | |
}; | ||
defaultLayouts: SupportedLayouts; | ||
selection?: string[]; | ||
setSelection?: SetSelection; | ||
onChangeSelection?: ( items: Item[] ) => void; | ||
onChangeSelection?: ( items: string[] ) => void; | ||
} & ( Item extends ItemWithId | ||
? { getItemId?: ( item: Item ) => string } | ||
: { getItemId: ( item: Item ) => string } ); | ||
|
||
const defaultGetItemId = ( item: ItemWithId ) => item.id; | ||
|
||
const defaultOnChangeSelection = () => {}; | ||
|
||
export default function DataViews< Item >( { | ||
view, | ||
onChangeView, | ||
|
@@ -73,25 +70,23 @@ export default function DataViews< Item >( { | |
paginationInfo, | ||
defaultLayouts, | ||
selection: selectionProperty, | ||
setSelection: setSelectionProperty, | ||
onChangeSelection = defaultOnChangeSelection, | ||
onChangeSelection, | ||
}: DataViewsProps< Item > ) { | ||
const [ selectionState, setSelectionState ] = useState< string[] >( [] ); | ||
const isUncontrolled = | ||
selectionProperty === undefined || setSelectionProperty === undefined; | ||
selectionProperty === undefined || onChangeSelection === undefined; | ||
const selection = isUncontrolled ? selectionState : selectionProperty; | ||
const setSelection = isUncontrolled | ||
? setSelectionState | ||
: setSelectionProperty; | ||
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 ) ) ) | ||
); | ||
return setSelection( value ); | ||
if ( ! isUncontrolled ) { | ||
setSelectionState( newValue ); | ||
} | ||
if ( onChangeSelection ) { | ||
onChangeSelection( newValue ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 ViewComponent = VIEW_LAYOUTS.find( ( v ) => v.type === view.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.
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