Skip to content

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

Chore/replace boolean input with select (#30189)

Joshen Lim395408c136e8
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx
index d4f5db3b2ec..6305af0fd15 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/InputField.tsx
@@ -8,13 +8,20 @@ import {
DropdownMenuItem,
DropdownMenuTrigger,
Input,
- Listbox,
Select,
+ Select_Shadcn_,
+ SelectContent_Shadcn_,
+ SelectGroup_Shadcn_,
+ SelectItem_Shadcn_,
+ SelectTrigger_Shadcn_,
+ SelectValue_Shadcn_,
} from 'ui'
+import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { DATETIME_TYPES, JSON_TYPES, TEXT_TYPES } from '../SidePanelEditor.constants'
import { DateTimeInput } from './DateTimeInput'
import type { EditValue, RowField } from './RowEditor.types'
import { isValueTruncated } from './RowEditor.utils'
+import { checkDomainOfScale } from 'recharts/types/util/ChartUtils'
export interface InputFieldProps {
field: RowField
@@ -250,41 +257,35 @@ const InputField = ({
...(field.isNullable ? [{ value: 'null', label: 'NULL' }] : []),
]
- // Ivan: The value coming in from backend is processed (NULL converted to 'null' string) so that
- // it's properly selected in the listbox. The issue is with the internal implementation of the
- // Listbox where the default column value is only considered when field.value is null
- // (the JS kind). Since we're converting that null into 'null', defaultValue isn't used as it
- // should. To fix this, we're only setting the defaultValue of the listbox and not setting the
- // value in the next renders. This makes the ListBox an uncontrolled component but it works.
- // PS: This is the third time we're fixing this in a month. If you have to fix this again, just
- // use Input for booleans.
- const defaultValue = field.value === 'null' ? field.defaultValue : field.value
+ const defaultValue = field.value === null ? undefined : field.value
return (
- <Listbox
- size="small"
+ <FormItemLayout
+ isReactForm={false}
layout="horizontal"
- name={field.name}
label={field.name}
labelOptional={field.format}
- descriptionText={field.comment}
- value={defaultValue === null ? 'null' : defaultValue}
- onChange={(value: string) => {
- if (value === 'null') onUpdateField({ [field.name]: null })
- else onUpdateField({ [field.name]: value })
- }}
+ description={field.comment}
+ className="[&>div:first-child>span]:text-foreground-lighter"
>
- {options.map((option) => (
- <Listbox.Option
- id={option.value}
- key={option.value}
- label={option.label}
- value={option.value}
- >
- {option.label}
- </Listbox.Option>
- ))}
- </Listbox>
+ <Select_Shadcn_
+ value={defaultValue === null ? 'null' : defaultValue}
+ onValueChange={(value: string) => onUpdateField({ [field.name]: value })}
+ >
+ <SelectTrigger_Shadcn_>
+ <SelectValue_Shadcn_ placeholder="Select a value" />
+ </SelectTrigger_Shadcn_>
+ <SelectContent_Shadcn_>
+ <SelectGroup_Shadcn_>
+ {options.map((option) => (
+ <SelectItem_Shadcn_ key={option.value} value={option.value}>
+ {option.label}
+ </SelectItem_Shadcn_>
+ ))}
+ </SelectGroup_Shadcn_>
+ </SelectContent_Shadcn_>
+ </Select_Shadcn_>
+ </FormItemLayout>
)
}
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts
index 050e0e5435f..72a7e4941a7 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils.ts
@@ -1,10 +1,11 @@
-import type { PostgresRelationship, PostgresTable } from '@supabase/postgres-meta'
+import type { PostgresColumn, PostgresRelationship, PostgresTable } from '@supabase/postgres-meta'
import dayjs from 'dayjs'
-import { compact, find, isEqual, isNull, isString, isUndefined, omitBy } from 'lodash'
+import { compact, isEqual, isNull, isString, omitBy } from 'lodash'
import type { Dictionary } from 'types'
import { MAX_CHARACTERS } from 'data/table-rows/table-rows-query'
import { minifyJSON, tryParseJson } from 'lib/helpers'
+import { ForeignKey } from '../ForeignKeySelector/ForeignKeySelector.types'
import {
DATETIME_TYPES,
JSON_TYPES,
@@ -13,7 +14,32 @@ import {
TIME_TYPES,
} from '../SidePanelEditor.constants'
import type { RowField } from './RowEditor.types'
-import { ForeignKey } from '../ForeignKeySelector/ForeignKeySelector.types'
+
+const getRowValue = ({ column, row }: { column: PostgresColumn; row?: Dictionary<any> }) => {
+ const isNewRow = row === undefined
+
+ if (isNewRow) {
+ if (TEXT_TYPES.includes(column.format)) {
+ return null
+ } else if (column.format === 'bool') {
+ if (column.default_value) {
+ return column.default_value
+ } else if (column.is_nullable) {
+ return 'null'
+ } else return null
+ } else {
+ return ''
+ }
+ } else {
+ if (column.format === 'bool' && row[column.name] === null) {
+ return 'null'
+ }
+
+ return DATETIME_TYPES.includes(column.format)
+ ? convertPostgresDatetimeToInputDatetime(column.format, row[column.name])
+ : parseValue(row[column.name], column.format)
+ }
+}
export const generateRowFields = (
row: Dictionary<any> | undefined,
@@ -24,19 +50,7 @@ export const generateRowFields = (
const primaryKeyColumns = primary_keys.map((key) => key.name)
return table.columns!.map((column) => {
- const value =
- isUndefined(row) && TEXT_TYPES.includes(column.format)
- ? null
- : isUndefined(row) && column.format === 'bool' && !column.is_nullable
- ? column.default_value
- : isUndefined(row) && column.format === 'bool' && column.is_nullable
- ? 'null'
- : isUndefined(row)
- ? ''
- : DATETIME_TYPES.includes(column.format)
- ? convertPostgresDatetimeToInputDatetime(column.format, row[column.name])
- : parseValue(row[column.name], column.format)
-
+ const value = getRowValue({ column, row })
const foreignKey = foreignKeys.find((fk) => {
return fk.columns.map((x) => x.source).includes(column.name)
})
@@ -166,6 +180,11 @@ const convertInputDatetimeToPostgresDatetime = (format: string, value: string |
}
}
+// [Joshen] JFYI this presents a small problem in particular when creating a new row
+// given that we don't include null properties. Because of that if the column has a default
+// value, the column value will then always be the default value, instead of null
+// which may be considered a bug if e.g for a boolean column the user specifically selects "NULL" option
+// This would probably also apply to other column types like numbers (e.g user specifically wants a null value)
export const generateRowObjectFromFields = (
fields: RowField[],
includeNullProperties = false
@@ -191,6 +210,7 @@ export const generateRowObjectFromFields = (
rowObject[field.name] = tryParseJson(value)
}
} else if (field.format === 'bool' && value) {
+ if (field.name === 'bool_default_true') console.log(field)
if (value === 'null') rowObject[field.name] = null
else rowObject[field.name] = value === 'true'
} else if (DATETIME_TYPES.includes(field.format)) {