Skip to content
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

feat: allow overriding props of components #202

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions cmdk/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type GroupProps = Children &
/** Whether this group is forcibly rendered regardless of filtering. */
forceMount?: boolean
}
type InputProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, 'value' | 'onChange' | 'type'> & {
type InputProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, 'value' | 'onChange'> & {
/**
* Optional controlled state for the value of the search input.
*/
Expand Down Expand Up @@ -514,8 +514,8 @@ const Command = React.forwardRef<HTMLDivElement, CommandProps>((props, forwarded
return (
<div
ref={mergeRefs([ref, forwardedRef])}
{...etc}
cmdk-root=""
{...etc}
onKeyDown={(e) => {
etc.onKeyDown?.(e)

Expand Down Expand Up @@ -633,21 +633,35 @@ const Item = React.forwardRef<HTMLDivElement, ItemProps>((props, forwardedRef) =

if (!render) return null

const { disabled, value: _, onSelect: __, ...etc } = props
const { disabled, value: _, onSelect: __, onClick, onPointerMove, ...etc } = props

return (
<div
ref={mergeRefs([ref, forwardedRef])}
{...etc}
id={id}
cmdk-item=""
role="option"
aria-disabled={disabled || undefined}
aria-selected={selected || undefined}
data-disabled={disabled || undefined}
data-selected={selected || undefined}
onPointerMove={disabled ? undefined : select}
onClick={disabled ? undefined : onSelect}
{...etc}
onPointerMove={
disabled
? onPointerMove
: (event) => {
onPointerMove?.(event)
select()
}
}
onClick={
disabled
? onClick
: (event) => {
onClick?.(event)
onSelect()
}
}
>
{props.children}
</div>
Expand Down Expand Up @@ -681,10 +695,10 @@ const Group = React.forwardRef<HTMLDivElement, GroupProps>((props, forwardedRef)
return (
<div
ref={mergeRefs([ref, forwardedRef])}
{...etc}
cmdk-group=""
role="presentation"
hidden={render ? undefined : true}
{...etc}
>
{heading && (
<div ref={headingRef} cmdk-group-heading="" aria-hidden id={headingId}>
Expand All @@ -708,7 +722,7 @@ const Separator = React.forwardRef<HTMLDivElement, SeparatorProps>((props, forwa
const render = useCmdk((state) => !state.search)

if (!alwaysRender && !render) return null
return <div ref={mergeRefs([ref, forwardedRef])} {...etc} cmdk-separator="" role="separator" />
return <div ref={mergeRefs([ref, forwardedRef])} cmdk-separator="" role="separator" {...etc} />
})

/**
Expand Down Expand Up @@ -737,7 +751,6 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, forwardedRe
return (
<input
ref={forwardedRef}
{...etc}
cmdk-input=""
autoComplete="off"
autoCorrect="off"
Expand All @@ -750,6 +763,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, forwardedRe
aria-activedescendant={selectedItemId}
id={context.inputId}
type="text"
{...etc}
value={isControlled ? props.value : search}
onChange={(e) => {
if (!isControlled) {
Expand Down Expand Up @@ -794,12 +808,12 @@ const List = React.forwardRef<HTMLDivElement, ListProps>((props, forwardedRef) =
return (
<div
ref={mergeRefs([ref, forwardedRef])}
{...etc}
cmdk-list=""
role="listbox"
aria-label="Suggestions"
id={context.listId}
aria-labelledby={context.inputId}
{...etc}
>
<div ref={height} cmdk-list-sizer="">
{children}
Expand Down Expand Up @@ -837,7 +851,7 @@ const Empty = React.forwardRef<HTMLDivElement, EmptyProps>((props, forwardedRef)
}, [])

if (isFirstRender.current || !render) return null
return <div ref={forwardedRef} {...props} cmdk-empty="" role="presentation" />
return <div ref={forwardedRef} cmdk-empty="" role="presentation" {...props} />
})

/**
Expand All @@ -849,13 +863,13 @@ const Loading = React.forwardRef<HTMLDivElement, LoadingProps>((props, forwarded
return (
<div
ref={forwardedRef}
{...etc}
cmdk-loading=""
role="progressbar"
aria-valuenow={progress}
aria-valuemin={0}
aria-valuemax={100}
aria-label="Loading..."
{...etc}
>
<div aria-hidden>{children}</div>
</div>
Expand Down
Loading