1- import { Dispatch , SetStateAction , useCallback } from "react" ;
1+ import { Dispatch , SetStateAction , useCallback , useMemo } from "react" ;
22import {
33 DropdownMenu ,
44 DropdownMenuContent ,
@@ -42,11 +42,16 @@ import {
4242import { restrictToVerticalAxis } from "@dnd-kit/modifiers" ;
4343import { useDatabaseDriver } from "@/context/driver-provider" ;
4444import ColumnTypeSelector from "./column-type-selector" ;
45+ import ColumnCollation from "./column-collation" ;
4546
4647export type ColumnChangeEvent = (
4748 newValue : Partial < DatabaseTableColumn > | null
4849) => void ;
4950
51+ export interface SchemaEditorOptions {
52+ collations : string [ ] ;
53+ }
54+
5055function changeColumnOnIndex (
5156 idx : number ,
5257 value : Partial < DatabaseTableColumn > | null ,
@@ -135,13 +140,15 @@ function ColumnItem({
135140 idx,
136141 schemaName,
137142 onChange,
143+ options,
138144 disabledEditExistingColumn,
139145} : {
140146 value : DatabaseTableColumnChange ;
141147 idx : number ;
142148 schemaName ?: string ;
143149 onChange : Dispatch < SetStateAction < DatabaseTableSchemaChange > > ;
144150 disabledEditExistingColumn ?: boolean ;
151+ options : SchemaEditorOptions ;
145152} ) {
146153 const {
147154 setNodeRef,
@@ -344,6 +351,21 @@ function ColumnItem({
344351 </ DropdownMenu >
345352 </ div >
346353 </ td >
354+ { options . collations . length > 0 && (
355+ < td className = "border" >
356+ < ColumnCollation
357+ value = { column . constraint ?. collate }
358+ onChange = { ( value ) => {
359+ change ( {
360+ constraint : {
361+ ...column . constraint ,
362+ collate : value || undefined ,
363+ } ,
364+ } ) ;
365+ } }
366+ />
367+ </ td >
368+ ) }
347369 < td className = "px-1 border" >
348370 < button
349371 className = "p-1"
@@ -364,12 +386,14 @@ export default function SchemaEditorColumnList({
364386 schemaName,
365387 onAddColumn,
366388 disabledEditExistingColumn,
389+ options,
367390} : Readonly < {
368391 columns : DatabaseTableColumnChange [ ] ;
369392 onChange : Dispatch < SetStateAction < DatabaseTableSchemaChange > > ;
370393 schemaName ?: string ;
371394 onAddColumn : ( ) => void ;
372395 disabledEditExistingColumn ?: boolean ;
396+ options : SchemaEditorOptions ;
373397} > ) {
374398 const headerStyle = "text-xs p-2 text-left bg-secondary border" ;
375399
@@ -394,25 +418,47 @@ export default function SchemaEditorColumnList({
394418 [ columns , onChange ]
395419 ) ;
396420
421+ const headerCounter = useMemo ( ( ) => {
422+ let initialCounter = 7 ;
423+ if ( options . collations . length > 0 ) {
424+ initialCounter ++ ;
425+ }
426+
427+ return initialCounter ;
428+ } , [ options ] ) ;
429+
397430 return (
398431 < div className = "p-4" >
399- < table className = "w-full rounded overflow-hidden" >
400- < thead >
401- < tr >
402- < td className = { cn ( headerStyle , "w-[20px]" ) } > </ td >
403- < th className = { cn ( headerStyle , "w-[100px]" ) } > Name</ th >
404- < th className = { cn ( headerStyle , "w-[150px]" ) } > Type</ th >
405- < th className = { cn ( headerStyle , "w-[150px]" ) } > Default</ th >
406- < th className = { cn ( headerStyle , "w-[50px]" ) } > Null</ th >
407- < th className = { cn ( headerStyle ) } > Constraint</ th >
408- < th className = { cn ( headerStyle , "w-[30px]" ) } > </ th >
409- </ tr >
410- </ thead >
411- < tbody >
412- < DndContext
413- onDragEnd = { handleDragEnd }
414- modifiers = { [ restrictToVerticalAxis ] }
415- >
432+ { options . collations . length > 0 && (
433+ < datalist id = "collation-list" className = "hidden" >
434+ { options . collations . map ( ( collation ) => (
435+ < option key = { collation } value = { collation } />
436+ ) ) }
437+ </ datalist >
438+ ) }
439+
440+ < DndContext
441+ onDragEnd = { handleDragEnd }
442+ modifiers = { [ restrictToVerticalAxis ] }
443+ >
444+ < table className = "w-full rounded overflow-hidden" >
445+ < thead >
446+ < tr >
447+ < td className = { cn ( headerStyle , "w-[20px]" ) } > </ td >
448+ < th className = { cn ( headerStyle , "w-[100px]" ) } > Name</ th >
449+ < th className = { cn ( headerStyle , "w-[150px]" ) } > Type</ th >
450+ < th className = { cn ( headerStyle , "w-[150px]" ) } > Default</ th >
451+ < th className = { cn ( headerStyle , "w-[50px]" ) } > Null</ th >
452+ < th className = { cn ( headerStyle ) } > Constraint</ th >
453+
454+ { options . collations . length > 0 && (
455+ < th className = { cn ( headerStyle , "w-[160px]" ) } > Collation</ th >
456+ ) }
457+
458+ < th className = { cn ( headerStyle , "w-[30px]" ) } > </ th >
459+ </ tr >
460+ </ thead >
461+ < tbody >
416462 < SortableContext
417463 items = { columns . map ( ( c ) => c . key ) }
418464 strategy = { verticalListSortingStrategy }
@@ -425,21 +471,22 @@ export default function SchemaEditorColumnList({
425471 onChange = { onChange }
426472 schemaName = { schemaName }
427473 disabledEditExistingColumn = { disabledEditExistingColumn }
474+ options = { options }
428475 />
429476 ) ) }
430477 </ SortableContext >
431- </ DndContext >
432- </ tbody >
433- < tfoot >
434- < tr >
435- < td colSpan = { 7 } className = "px-4 py-2 border" >
436- < Button size = "sm" onClick = { onAddColumn } >
437- < LucidePlus className = "w-4 h-4 mr-1" /> Add Column
438- </ Button >
439- </ td >
440- </ tr >
441- </ tfoot >
442- </ table >
478+ </ tbody >
479+ < tfoot >
480+ < tr >
481+ < td colSpan = { headerCounter } className = "px-4 py-2 border" >
482+ < Button size = "sm" onClick = { onAddColumn } >
483+ < LucidePlus className = "w-4 h-4 mr-1" /> Add Column
484+ </ Button >
485+ </ td >
486+ </ tr >
487+ </ tfoot >
488+ </ table >
489+ </ DndContext >
443490 </ div >
444491 ) ;
445492}
0 commit comments