The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Implement MultiSelectEditor for grid, for an enum array column
Joshen Lim105833d6221e
diff --git a/studio/components/grid/components/editor/MultiSelectEditor.tsx b/studio/components/grid/components/editor/MultiSelectEditor.tsxindex c8f316acd30..cf6d0eb9691 100644--- a/studio/components/grid/components/editor/MultiSelectEditor.tsx+++ b/studio/components/grid/components/editor/MultiSelectEditor.tsx@@ -1,14 +1,28 @@-import { Listbox } from '@supabase/ui'+import { useState } from 'react'+import { Popover, IconX, IconPlus } from '@supabase/ui'import { EditorProps } from '@supabase/react-data-grid'import { useTrackedState } from 'components/grid/store'-import MultiSelect from 'components/ui/MultiSelect'+import { tryParseJson } from 'lib/helpers'-interface MultiSelectEditorProps<TRow, TSummaryRow = unknown>- extends EditorProps<TRow, TSummaryRow> {+interface Props<TRow, TSummaryRow = unknown> extends EditorProps<TRow, TSummaryRow> {options?: { label: string; value: string }[]}+const convertPgArrayToJsArray = (value: string): string[] => {+ const valueArray = value+ .slice(1, value.length - 1)+ .split(',')+ .map((x) => `"${x}"`)+ const formattedValue = `[${valueArray.join(',')}]`+ return tryParseJson(formattedValue) ?? []+}++const convertJsArraytoPgArray = (value: string[]) => {+ if (value.length === 0) return null+ else return `{${value.join(',')}}`+}+// Mainly for arrays (specifically enums)// Array elements are not unique, so this supports selecting an option more than onceexport function MultiSelectEditor<TRow, TSummaryRow = unknown>({@@ -16,61 +30,89 @@ export function MultiSelectEditor<TRow, TSummaryRow = unknown>({column,onRowChange,onClose,-}: MultiSelectEditorProps<TRow, TSummaryRow>) {+ options = [],+}: Props<TRow, TSummaryRow>) {const state = useTrackedState()const gridColumn = state.gridColumns.find((x) => x.name == column.key)- // Hardcoded options for now- const options = [- { label: 'Apple', value: 'apple' },- { label: 'Orange', value: 'orange' },- ]-- const value = row[column.key as keyof TRow] as unknown as string- console.log('value', value)+ const originalValue = row[column.key as keyof TRow] as string+ const [selectedValues, setSelectedValues] = useState(convertPgArrayToJsArray(originalValue))- function onChange(value: string) {- if (!value || value == '') {- onRowChange({ ...row, [column.key]: null }, true)- } else {- onRowChange({ ...row, [column.key]: value }, true)+ const onOpenChange = (open: boolean) => {+ if (!open) {+ const updatedValue = convertJsArraytoPgArray(selectedValues)+ if (updatedValue === originalValue) {+ onClose()+ } else {+ onRowChange({ ...row, [column.key]: updatedValue }, true)+ }}}- function onBlur() {- onClose(false)+ const handleAdd = (option: { label: string; value: string }) => {+ const updatedValues = selectedValues.concat([option.value])+ setSelectedValues(updatedValues)}- // return (- // <MultiSelect- // options={options}- // value={selectedRoles}- // placeholder="Defaults to all roles if none selected"- // searchPlaceholder="Search for a role"- // onChange={onUpdateSelectedRoles}- // />- // )+ const handleRemove = (idx: number) => {+ const updatedValues = selectedValues.filter((x, index) => index !== idx)+ setSelectedValues(updatedValues)+ }return (- <Listbox- autoFocus- id="select-editor"- name="select-editor"- size="small"- defaultValue={value ?? ''}- className="sb-grid-select-editor !gap-2"- style={{ width: `${gridColumn?.width || column.width}px` }}- onChange={onChange}- onBlur={onBlur}- >- <Listbox.Option id="NULL" label="NULL" value="">- NULL- </Listbox.Option>- {options.map(({ label, value }) => (- <Listbox.Option key={value} label={label} value={value}>- {label}- </Listbox.Option>- ))}- </Listbox>+ <Popover+ open+ side="bottom"+ align="start"+ sideOffset={-35}+ className="rounded-none"+ onOpenChange={onOpenChange}+ overlay={+ <div style={{ width: `calc(${gridColumn?.width || column.width}px - 2px)` }}>+ <div className="flex gap-2 p-1 flex-wrap">+ {selectedValues.map((value, idx) => (+ <div+ className={[+ 'text-typography-body-light dark:text-typography-body-dark',+ 'flex items-center space-x-2 rounded bg-gray-500',+ 'py-0.5 px-2 text-xs',+ ].join(' ')}+ >+ <span>{value}</span>+ <IconX+ size={12}+ className="cursor-pointer opacity-50 transition hover:opacity-100"+ onClick={(e: any) => {+ e.preventDefault()+ handleRemove(idx)+ }}+ />+ </div>+ ))}+ </div>+ <div+ className="bg-scale-200 p-1 border-t border-scale-500 space-y-1 overflow-y-auto"+ style={{ maxHeight: '160px' }}+ >+ {options.map((option) => (+ <div+ key={option.value}+ className={[+ 'py-1 px-2 rounded group flex items-center justify-between',+ 'hover:bg-scale-400 cursor-pointer transition',+ ].join(' ')}+ onClick={() => handleAdd(option)}+ >+ <p className="text-xs">{option.label}</p>+ <div className="flex items-center space-x-1 opacity-0 group-hover:opacity-100">+ <IconPlus size={12} />+ <p className="text-xs text-scale-1000">Add value</p>+ </div>+ </div>+ ))}+ </div>+ </div>+ }+ />)}diff --git a/studio/components/grid/utils/gridColumns.tsx b/studio/components/grid/utils/gridColumns.tsxindex 33ed4bda265..7789f664774 100644--- a/studio/components/grid/utils/gridColumns.tsx+++ b/studio/components/grid/utils/gridColumns.tsx@@ -112,7 +112,14 @@ function getColumnEditor(columnDefinition: SupaColumn, columnType: ColumnType) {return (p: any) => <SelectEditor {...p} options={options} />}case 'array': {- return MultiSelectEditor+ if ((columnDefinition?.enum ?? []).length > 0) {+ const options = columnDefinition.enum!.map((x) => {+ return { label: x, value: x }+ })+ return (p: any) => <MultiSelectEditor {...p} options={options} />+ } else {+ return JsonEditor+ }}case 'json': {return JsonEditordiff --git a/studio/components/ui/MultiSelect/index.tsx b/studio/components/ui/MultiSelect/index.tsxindex 67c102f1e49..5a649240349 100644--- a/studio/components/ui/MultiSelect/index.tsx+++ b/studio/components/ui/MultiSelect/index.tsx@@ -8,7 +8,7 @@ export interface MultiSelectOption {id: string | numbervalue: stringname: string- disabled: boolean+ disabled?: boolean}interface Props {@@ -20,7 +20,6 @@ interface Props {descriptionText?: string | ReactNodeemptyMessage?: string | ReactNodedisabled?: boolean- allowDuplicateSelection?: booleanonChange?(x: string[]): void}