Skip to content

Programmatically set selection without affected input value #243

Closed as not planned

Description

@mthines

Hi.

I would like to implement the PageUp/PageDown functionality which, but it doesn't look like the library supports setting the Selection programmatically - which renders the functionality of PageUp/PageDown useless as when you press up/down arrow, it scrolls back up because the selection remains the same.

I can see that Home/End works natively with the library and moves the selection.

I wish the library exposed the internal API and gave me the possibility to programmatically set the selection.

cmdk.mov

Thanks!

Activity

  1. DjordyKoert commented on May 29, 2024

    @DjordyKoert

    I think my question/feature is related to this.

    (We use the shadcn/ui Command component for this)

    I would like to integrate the item selection with tanstack router <Link> preloading to prefetch some data before selecting the command item.

    We currently partially fixed this by using a ref on the <Link> component to manage the onSelect (navigating) & onFocus (preloading) but we are still missing preloading whenever the user navigate with the arrow keys.

    const ActionItem: React.FC<action: TAction> = ({ action }) => {
    	const linkRef = useRef<HTMLAnchorElement>(null);
    
    	return (
                <Link {...action.linkOptions} ref={linkRef}>
                    <CommandItem 
    				   disabled={action.isDisabled} 
                        onFocus={() => linkRef.current?.focus()} 
                        onSelect={() => linkRef.current?.click()} 
                        keywords={action.keywords} 
                        value={`${action.action};${action.description}`}>
                        <p> {action.title} </p>
                    </CommandItem>
                </Link>
            );
    
    };
  2. rivamadan commented on Oct 7, 2024

    @rivamadan

    I would also like to be able to programmatically set selection. For my use case, I want "tab" keydown to move to a specific special item within our list of items.

    I saw that internally it is set via store.setState('value', value || undefined), if we could wrap a function around this and expose it, that'd be great.

  3. jayalfredprufrock commented on Oct 10, 2024

    @jayalfredprufrock

    I've seen several issues/feature requests that could have reasonable workarounds if the store was just exposed somehow. I understand the reluctance and I think that it would go without saying that directly messing with the store requires a "use at your own risk" caveat, but nevertheless such escape hatches can be extremely useful when you are just trying to ship code.

  4. came commented on Dec 11, 2024

    @came

    You can externally control the . Just set its value prop and the Command.Item having this value is selected. However, you probably lose some internal things (e.g., auto scrolling) ….at least that is what I am just struggling with ;)

  5. glekner commented on Jan 23, 2025

    @glekner

    You can externally control the . Just set its value prop and the Command.Item having this value is selected. However, you probably lose some internal things (e.g., auto scrolling) ….at least that is what I am just struggling with ;)

    Exactly..did you figure this out? or did u implement auto scroll yourself? @came

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions