-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
ref(cmdk): Merge CMDKGroup and CMDKAction into single CMDKAction component #112563
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
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 |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ interface CMDKActionDataBase { | |
| } | ||
|
|
||
| interface CMDKActionDataTo extends CMDKActionDataBase { | ||
| to: string; | ||
| to: LocationDescriptor; | ||
| } | ||
|
|
||
| interface CMDKActionDataOnAction extends CMDKActionDataBase { | ||
|
|
@@ -50,7 +50,7 @@ export const CMDKCollection = makeCollection<CMDKActionData>(); | |
|
|
||
| /** | ||
| * Root provider for the command palette. Wrap the component tree that | ||
| * contains CMDKGroup/CMDKAction registrations and the CommandPalette UI. | ||
| * contains CMDKAction registrations and the CommandPalette UI. | ||
| */ | ||
| export function CommandPaletteProvider({children}: {children: React.ReactNode}) { | ||
| return ( | ||
|
|
@@ -62,25 +62,39 @@ export function CommandPaletteProvider({children}: {children: React.ReactNode}) | |
| ); | ||
| } | ||
|
|
||
| interface CMDKGroupProps { | ||
| interface CMDKActionProps { | ||
| display: DisplayProps; | ||
| children?: React.ReactNode | ((data: CommandPaletteAsyncResult[]) => React.ReactNode); | ||
| keywords?: string[]; | ||
| onAction?: () => void; | ||
| resource?: (query: string) => CMDKQueryOptions; | ||
| to?: LocationDescriptor; | ||
| } | ||
|
|
||
| type CMDKActionProps = | ||
| | {display: DisplayProps; to: LocationDescriptor; keywords?: string[]} | ||
| | {display: DisplayProps; onAction: () => void; keywords?: string[]}; | ||
|
|
||
| /** | ||
| * Registers a node in the collection and propagates its key to children via | ||
| * GroupContext. When a `resource` prop is provided, fetches data using the | ||
| * current query and passes results to a render-prop children function. | ||
| * Registers a node in the collection. A node becomes a group when it has | ||
| * children — they register under this node as their parent. Provide `to` for | ||
| * navigation, `onAction` for a callback, or `resource` with a render-prop | ||
| * children function to fetch and populate async results. | ||
| */ | ||
| export function CMDKGroup({display, keywords, resource, children}: CMDKGroupProps) { | ||
| export function CMDKAction({ | ||
| display, | ||
| keywords, | ||
| children, | ||
| to, | ||
| onAction, | ||
| resource, | ||
| }: CMDKActionProps) { | ||
| const ref = CommandPaletteSlot.useSlotOutletRef(); | ||
| const key = CMDKCollection.useRegisterNode({display, keywords, resource, ref}); | ||
|
|
||
| const nodeData: CMDKActionData = | ||
| to === undefined | ||
| ? onAction === undefined | ||
| ? {display, keywords, ref, resource} | ||
| : {display, keywords, ref, onAction} | ||
| : {display, keywords, ref, to}; | ||
|
|
||
| const key = CMDKCollection.useRegisterNode(nodeData); | ||
| const {query} = useCommandPaletteState(); | ||
|
|
||
| const resourceOptions = resource | ||
|
|
@@ -91,6 +105,10 @@ export function CMDKGroup({display, keywords, resource, children}: CMDKGroupProp | |
| enabled: !!resource && (resourceOptions.enabled ?? true), | ||
| }); | ||
|
|
||
| if (!children) { | ||
| return null; | ||
| } | ||
|
|
||
| const resolvedChildren = | ||
| typeof children === 'function' ? (data ? children(data) : null) : children; | ||
|
|
||
|
|
@@ -100,12 +118,3 @@ export function CMDKGroup({display, keywords, resource, children}: CMDKGroupProp | |
| </CMDKCollection.Context.Provider> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Registers a leaf action node in the collection. | ||
| */ | ||
|
Contributor
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. Leaf actions unnecessarily subscribe to query stateMedium Severity The merged Reviewed by Cursor Bugbot for commit c85059a. Configure here. |
||
| export function CMDKAction(props: CMDKActionProps) { | ||
| const ref = CommandPaletteSlot.useSlotOutletRef(); | ||
| CMDKCollection.useRegisterNode({...props, ref}); | ||
| return null; | ||
| } | ||


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.
Bug: The
CMDKActioncomponent silently ignores theonActionprop if thetoprop is also provided, due to a loss of type-level mutual exclusivity.Severity: MEDIUM
Suggested Fix
Restore the type-level safety by using a discriminated union for the
CMDKActionPropstype. This will enforce that eithertooronActioncan be provided, but not both, preventing this issue at compile time. This was the behavior in the previous version of the component.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.