-
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 the AnyItem type #62856
Merged
+119
−115
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,17 +21,18 @@ import { VIEW_LAYOUTS } from './layouts'; | |
import BulkActions from './bulk-actions'; | ||
import { normalizeFields } from './normalize-fields'; | ||
import BulkActionsToolbar from './bulk-actions-toolbar'; | ||
import type { Action, AnyItem, Field, View, ViewBaseProps } from './types'; | ||
import type { Action, Field, View, ViewBaseProps } from './types'; | ||
|
||
interface DataViewsProps< Item extends AnyItem > { | ||
type ItemWithId = { id: string }; | ||
|
||
type DataViewsProps< Item > = { | ||
view: View; | ||
onChangeView: ( view: View ) => void; | ||
fields: Field< Item >[]; | ||
search?: boolean; | ||
searchLabel?: string; | ||
actions?: Action< Item >[]; | ||
data: Item[]; | ||
getItemId?: ( item: Item ) => string; | ||
isLoading?: boolean; | ||
paginationInfo: { | ||
totalItems: number; | ||
|
@@ -41,12 +42,15 @@ interface DataViewsProps< Item extends AnyItem > { | |
selection?: string[]; | ||
setSelection?: ( selection: string[] ) => void; | ||
onSelectionChange?: ( items: Item[] ) => void; | ||
} | ||
} & ( Item extends ItemWithId | ||
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. TIL conditions wow :P 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. Yes, with this the type system is Turing complete and you can build any program with it 😄 |
||
? { getItemId?: ( item: Item ) => string } | ||
: { getItemId: ( item: Item ) => string } ); | ||
|
||
const defaultGetItemId = ( item: ItemWithId ) => item.id; | ||
|
||
const defaultGetItemId = ( item: AnyItem ) => item.id; | ||
const defaultOnSelectionChange = () => {}; | ||
|
||
function useSomeItemHasAPossibleBulkAction< Item extends AnyItem >( | ||
function useSomeItemHasAPossibleBulkAction< Item >( | ||
actions: Action< Item >[], | ||
data: Item[] | ||
) { | ||
|
@@ -62,7 +66,7 @@ function useSomeItemHasAPossibleBulkAction< Item extends AnyItem >( | |
}, [ actions, data ] ); | ||
} | ||
|
||
export default function DataViews< Item extends AnyItem >( { | ||
export default function DataViews< Item >( { | ||
view, | ||
onChangeView, | ||
fields, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 not really familiar with any of this. Where is
Item
defined? Below inItem extends ItemWithId
right? I thought anItem
was an item in the data array? Those don't have agetItemId
function?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 guess I'm not getting this part
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.
Item
is a type parameter, it's completely generic.DataViews
can work with any data type whatsoever, you can have a dataview ofnumber
s. As long as you provide functions to extract IDs and field values from the item.The
Item extends ItemWithId
part says that if theItem
is an object that has anid
field, thegetItemId
prop is optional, because we have a suitable default for it.But if
Item
is not compatible withItemWithId
, thengetItemId
is mandatory. Because the default (item => item.id
) is not going to work with such a type.