Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: Keyboard interactions with the new filter experience in the table editor (#42431)

Ali Waseeme1f37312d23c
diff --git a/apps/studio/components/grid/hooks/useFilterLifeCycle.ts b/apps/studio/components/grid/hooks/useFilterLifeCycle.ts
index d8ba6257096..bb35dbd5db6 100644
--- a/apps/studio/components/grid/hooks/useFilterLifeCycle.ts
+++ b/apps/studio/components/grid/hooks/useFilterLifeCycle.ts
@@ -44,8 +44,13 @@ export function useSyncFiltersToUrl() {
// Debounce URL updates by 500ms
timeoutRef.current = setTimeout(() => {
+ const completeFilters = snap.filters.filter((filter) => {
+ const value = filter.value
+ return value !== '' && value !== null && value !== undefined
+ })
+
// Convert filters to URL format
- const urlFilters = filtersToUrlParams(snap.filters)
+ const urlFilters = filtersToUrlParams(completeFilters)
// Update URL params
setParams((prev) => ({
diff --git a/packages/ui-patterns/src/FilterBar/FilterBar.test.tsx b/packages/ui-patterns/src/FilterBar/FilterBar.test.tsx
index 407d92b60ee..0c1841e919e 100644
--- a/packages/ui-patterns/src/FilterBar/FilterBar.test.tsx
+++ b/packages/ui-patterns/src/FilterBar/FilterBar.test.tsx
@@ -1,8 +1,9 @@
-import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+
import { FilterBar } from './FilterBar'
-import { FilterProperty, FilterGroup } from './types'
+import { FilterGroup, FilterProperty } from './types'
const mockFilterProperties: FilterProperty[] = [
{
@@ -50,7 +51,11 @@ describe('FilterBar', () => {
/>
)
- expect(screen.getByPlaceholderText('Search or filter...')).toBeInTheDocument()
+ expect(
+ screen.getByPlaceholderText(
+ 'Ask AI for help (e.g. Find all users with name John) or filter...'
+ )
+ ).toBeInTheDocument()
})
it('renders with search input', () => {
@@ -64,7 +69,9 @@ describe('FilterBar', () => {
/>
)
- const input = screen.getByPlaceholderText('Search or filter...')
+ const input = screen.getByPlaceholderText(
+ 'Ask AI for help (e.g. Find all users with name John) or filter...'
+ )
expect(input).toBeInTheDocument()
})
@@ -85,7 +92,9 @@ describe('FilterBar', () => {
/>
)
- const freeform = screen.getByPlaceholderText('Search or filter...')
+ const freeform = screen.getByPlaceholderText(
+ 'Ask AI for help (e.g. Find all users with name John) or filter...'
+ )
freeform.focus()
await user.click(freeform)
@@ -135,7 +144,9 @@ describe('FilterBar', () => {
/>
)
- const freeform = screen.getByPlaceholderText('Search or filter...')
+ const freeform = screen.getByPlaceholderText(
+ 'Ask AI for help (e.g. Find all users with name John) or filter...'
+ )
await user.click(freeform)
await user.click(screen.getByText('Status'))
@@ -225,7 +236,9 @@ describe('FilterBar', () => {
/>
)
- const freeform = screen.getByPlaceholderText('Search or filter...')
+ const freeform = screen.getByPlaceholderText(
+ 'Ask AI for help (e.g. Find all users with name John) or filter...'
+ )
await user.click(freeform)
await user.click(screen.getByText('Tag'))
@@ -287,7 +300,9 @@ describe('FilterBar', () => {
/>
)
- const freeform = screen.getByPlaceholderText('Search or filter...')
+ const freeform = screen.getByPlaceholderText(
+ 'Ask AI for help (e.g. Find all users with name John) or filter...'
+ )
await user.click(freeform)
expect(await screen.findByText('Name')).toBeInTheDocument()
diff --git a/packages/ui-patterns/src/FilterBar/FilterCondition.tsx b/packages/ui-patterns/src/FilterBar/FilterCondition.tsx
index 89ede8d736f..c029a76a416 100644
--- a/packages/ui-patterns/src/FilterBar/FilterCondition.tsx
+++ b/packages/ui-patterns/src/FilterBar/FilterCondition.tsx
@@ -4,12 +4,13 @@ import { X } from 'lucide-react'
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import {
Button,
- cn,
Input_Shadcn_,
- Popover_Shadcn_,
PopoverAnchor_Shadcn_,
PopoverContent_Shadcn_,
+ Popover_Shadcn_,
+ cn,
} from 'ui'
+
import { DefaultCommandList } from './DefaultCommandList'
import { useFilterBar } from './FilterBarContext'
import { useDeferredBlur, useHighlightNavigation } from './hooks'
@@ -133,13 +134,28 @@ export function FilterCondition({
}
}, [isActive, isLoading, valueItems, showValueCustom])
+ const handleOperatorBackspace = useCallback(
+ (e: React.KeyboardEvent<HTMLInputElement>) => {
+ if (e.key === 'Backspace' && condition.operator === '') {
+ e.preventDefault()
+ handleRemoveCondition(path)
+ setActiveInput({ type: 'group', path: path.slice(0, -1) })
+ }
+ },
+ [condition.operator, setActiveInput, path, handleRemoveCondition]
+ )
+
const {
highlightedIndex: opHighlightedIndex,
handleKeyDown: handleOperatorKeyDown,
reset: resetOpHighlight,
- } = useHighlightNavigation(operatorItems.length, (index) => {
- if (operatorItems[index]) handleSelectMenuItem(operatorItems[index])
- })
+ } = useHighlightNavigation(
+ operatorItems.length,
+ (index) => {
+ if (operatorItems[index]) handleSelectMenuItem(operatorItems[index])
+ },
+ handleOperatorBackspace
+ )
const {
highlightedIndex: valHighlightedIndex,
@@ -317,6 +333,7 @@ export function FilterCondition({
onClick={onRemove}
className="group hover:text-foreground hover:!bg-surface-600 rounded-none px-1 h-auto py-0"
aria-label={`Remove ${property.label} filter`}
+ tabIndex={-1}
/>
</div>
)
diff --git a/packages/ui-patterns/src/FilterBar/FilterGroup.tsx b/packages/ui-patterns/src/FilterBar/FilterGroup.tsx
index 7ef2bbf5fd0..f11c6e80bb5 100644
--- a/packages/ui-patterns/src/FilterBar/FilterGroup.tsx
+++ b/packages/ui-patterns/src/FilterBar/FilterGroup.tsx
@@ -2,12 +2,13 @@
import React, { useEffect, useMemo, useRef, useState } from 'react'
import {
- cn,
Input_Shadcn_,
- Popover_Shadcn_,
PopoverAnchor_Shadcn_,
PopoverContent_Shadcn_,
+ Popover_Shadcn_,
+ cn,
} from 'ui'
+
import { DefaultCommandList } from './DefaultCommandList'
import { useFilterBar } from './FilterBarContext'
import { FilterCondition } from './FilterCondition'
@@ -173,7 +174,9 @@ export function FilterGroup({ group, path }: FilterGroupProps) {
onKeyDown={handleFreeformKeyDown}
className="border-none bg-transparent text-xs focus:outline-none focus:ring-0 focus:shadow-none focus-visible:ring-0 focus-visible:ring-offset-0 w-full flex-1 h-auto min-w-0 px-2 py-0"
placeholder={
- group.conditions.length === 0 ? 'Search or filter...' : '+ Search or filter...'
+ group.conditions.length === 0
+ ? 'Ask AI for help (e.g. Find all users with name John) or filter...'
+ : 'Add more filters...'
}
disabled={isLoading}
/>
diff --git a/packages/ui-patterns/src/FilterBar/hooks.ts b/packages/ui-patterns/src/FilterBar/hooks.ts
index e79e13ed886..4949933e4ec 100644
--- a/packages/ui-patterns/src/FilterBar/hooks.ts
+++ b/packages/ui-patterns/src/FilterBar/hooks.ts
@@ -1,7 +1,8 @@
'use client'
-import { useState, useRef, useCallback, useEffect } from 'react'
-import { FilterProperty, FilterOptionObject, AsyncOptionsFunction } from './types'
+import { useCallback, useEffect, useRef, useState } from 'react'
+
+import { AsyncOptionsFunction, FilterOptionObject, FilterProperty } from './types'
import { isAsyncOptionsFunction } from './utils'
export type ActiveInput =
@@ -143,9 +144,18 @@ export function useHighlightNavigation(
setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : 0))
return
}
- if (e.key === 'Enter') {
- e.preventDefault()
- onEnter(highlightedIndex)
+ if (e.key === 'Enter' || e.key === 'Tab') {
+ // Only select dropdown item if there are items available
+ if (itemsLength > 0) {
+ e.preventDefault()
+ onEnter(highlightedIndex)
+ return
+ }
+ if (fallbackKeyDown) fallbackKeyDown(e)
+ return
+ }
+ if (e.key === 'Backspace') {
+ if (fallbackKeyDown) fallbackKeyDown(e)
return
}
if (fallbackKeyDown) fallbackKeyDown(e)
diff --git a/packages/ui-patterns/src/FilterBar/menuItems.ts b/packages/ui-patterns/src/FilterBar/menuItems.ts
index 85d3d67e662..dffec3f65da 100644
--- a/packages/ui-patterns/src/FilterBar/menuItems.ts
+++ b/packages/ui-patterns/src/FilterBar/menuItems.ts
@@ -3,8 +3,8 @@ import { FilterBarAction, FilterGroup, FilterProperty } from './types'
import {
findConditionByPath,
isCustomOptionObject,
- isFilterOptionObject,
isFilterOperatorObject,
+ isFilterOptionObject,
} from './utils'
export type MenuItem = {
@@ -36,12 +36,17 @@ export function buildOperatorItems(
return availableOperators
.filter((op) => {
if (!shouldFilter) return true
- const searchText = isFilterOperatorObject(op) ? op.value : op
- return searchText.toUpperCase().includes(operatorValue)
+ if (isFilterOperatorObject(op)) {
+ return (
+ op.value.toUpperCase().includes(operatorValue) ||
+ op.label.toUpperCase().includes(operatorValue)
+ )
+ }
+ return op.toUpperCase().includes(operatorValue)
})
.map((op) => {
if (isFilterOperatorObject(op)) {
- return { value: op.value, label: op.label }
+ return { value: op.value, label: `${op.label} (${op.value})` }
}
return { value: op, label: op }
})
diff --git a/packages/ui-patterns/src/FilterBar/useCommandHandling.ts b/packages/ui-patterns/src/FilterBar/useCommandHandling.ts
index dbe7f6cbf55..3855cb1df6a 100644
--- a/packages/ui-patterns/src/FilterBar/useCommandHandling.ts
+++ b/packages/ui-patterns/src/FilterBar/useCommandHandling.ts
@@ -1,4 +1,5 @@
import { useCallback } from 'react'
+
import { ActiveInput } from './hooks'
import { MenuItem } from './menuItems'
import { FilterGroup, FilterProperty } from './types'
@@ -64,7 +65,7 @@ export function useCommandHandling({
const path = activeInput.path
handleOperatorChange(path, selectedValue)
- setActiveInput(null)
+ setActiveInput({ type: 'value', path })
},
[activeInput, handleOperatorChange, setActiveInput]
)
@@ -76,7 +77,7 @@ export function useCommandHandling({
const newPath = [...currentPath, group.conditions.length]
setTimeout(() => {
- setActiveInput({ type: 'value', path: newPath })
+ setActiveInput({ type: 'operator', path: newPath })
}, 0)
},
[activeFilters, onFilterChange, setActiveInput]
diff --git a/packages/ui-patterns/src/FilterBar/useKeyboardNavigation.ts b/packages/ui-patterns/src/FilterBar/useKeyboardNavigation.ts
index 2e50b9ecbb1..ded9d9343be 100644
--- a/packages/ui-patterns/src/FilterBar/useKeyboardNavigation.ts
+++ b/packages/ui-patterns/src/FilterBar/useKeyboardNavigation.ts
@@ -1,7 +1,8 @@
import { KeyboardEvent, useCallback } from 'react'
+
import { ActiveInput } from './hooks'
import { FilterGroup } from './types'
-import { findGroupByPath, findConditionByPath, removeFromGroup } from './utils'
+import { findConditionByPath, findGroupByPath, removeFromGroup } from './utils'
export function useKeyboardNavigation({
activeInput,
@@ -178,16 +179,11 @@ export function useKeyboardNavigation({
path: activeInput.path.slice(0, -1),
})
}
- // For root group with no conditions, do nothing - keep menu open
} else if (activeInput?.type === 'value' && isEmpty) {
const condition = findConditionByPath(activeFilters, activeInput.path)
if (condition && !condition.value) {
e.preventDefault()
- removeByPath(activeInput.path)
- setActiveInput({
- type: 'group',
- path: activeInput.path.slice(0, -1),
- })
+ setActiveInput({ type: 'operator', path: activeInput.path })
}
}
},
@@ -267,7 +263,19 @@ export function useKeyboardNavigation({
} else if (e.key === 'ArrowRight') {
handleArrowRight(e)
} else if (e.key === 'Escape') {
- setActiveInput(null)
+ const activeElement = document.activeElement as HTMLElement | null
+ if (activeElement && activeElement.blur) {
+ activeElement.blur()
+ }
+ } else if (e.key === 'Enter') {
+ if (activeInput?.type === 'value') {
+ e.preventDefault()
+ setActiveInput({ type: 'group', path: activeInput.path.slice(0, -1) })
+ } else if (activeInput?.type === 'operator') {
+ e.preventDefault()
+ const conditionPath = activeInput.path
+ setActiveInput({ type: 'value', path: conditionPath })
+ }
}
},
[activeInput, handleBackspace, handleArrowLeft, handleArrowRight, setActiveInput]
diff --git a/packages/ui-patterns/src/FilterBar/utils.test.ts b/packages/ui-patterns/src/FilterBar/utils.test.ts
index 27cdd8ebb84..ce05a39697c 100644
--- a/packages/ui-patterns/src/FilterBar/utils.test.ts
+++ b/packages/ui-patterns/src/FilterBar/utils.test.ts
@@ -1,20 +1,21 @@
-import { describe, it, expect } from 'vitest'
import * as React from 'react'
+import { describe, expect, it } from 'vitest'
+
+import { FilterGroup, FilterProperty } from './types'
import {
- findGroupByPath,
- findConditionByPath,
addFilterToGroup,
addGroupToGroup,
- removeFromGroup,
- updateNestedValue,
- updateNestedOperator,
- updateNestedLogicalOperator,
+ findConditionByPath,
+ findGroupByPath,
+ isAsyncOptionsFunction,
isCustomOptionObject,
isFilterOptionObject,
- isAsyncOptionsFunction,
isSyncOptionsFunction,
+ removeFromGroup,
+ updateNestedLogicalOperator,
+ updateNestedOperator,
+ updateNestedValue,
} from './utils'
-import { FilterGroup, FilterProperty } from './types'
const mockProperty: FilterProperty = {
label: 'Test Property',
@@ -108,7 +109,7 @@ describe('FilterBar Utils', () => {
expect(result.conditions[2]).toEqual({
propertyName: 'test',
value: '',
- operator: '=',
+ operator: '',
})
})
diff --git a/packages/ui-patterns/src/FilterBar/utils.ts b/packages/ui-patterns/src/FilterBar/utils.ts
index 62f8253562c..58035d7f15b 100644
--- a/packages/ui-patterns/src/FilterBar/utils.ts
+++ b/packages/ui-patterns/src/FilterBar/utils.ts
@@ -1,11 +1,11 @@
import {
- FilterGroup,
- FilterCondition,
- FilterProperty,
+ AsyncOptionsFunction,
CustomOptionObject,
- FilterOptionObject,
+ FilterCondition,
+ FilterGroup,
FilterOperatorObject,
- AsyncOptionsFunction,
+ FilterOptionObject,
+ FilterProperty,
SyncOptionsFunction,
isGroup,
} from './types'
@@ -133,17 +133,9 @@ export function addFilterToGroup(
property: FilterProperty
): FilterGroup {
if (path.length === 0) {
- const firstOperator = property.operators?.[0] || '='
- const operatorValue = isFilterOperatorObject(firstOperator)
- ? firstOperator.value
- : firstOperator
-
return {
...group,
- conditions: [
- ...group.conditions,
- { propertyName: property.name, value: '', operator: operatorValue },
- ],
+ conditions: [...group.conditions, { propertyName: property.name, value: '', operator: '' }],
}
}