@@ -53,13 +53,14 @@ interface KanbanProviderContextValue<T, C> {
53
53
columnData : C [ ] ;
54
54
activeId : UniqueIdentifier | null ;
55
55
setActiveId : ( id : UniqueIdentifier | null ) => void ;
56
- getItemValue : ( item : T ) => UniqueIdentifier ;
57
56
getColumnValue : ( column : C ) => UniqueIdentifier ;
57
+ getItemValue : ( item : T ) => UniqueIdentifier ;
58
58
}
59
59
60
- type KanbanRootContextValue = KanbanProviderContextValue < unknown , unknown > ;
61
-
62
- const KanbanRoot = React . createContext < KanbanRootContextValue | null > ( null ) ;
60
+ const KanbanRoot = React . createContext < KanbanProviderContextValue <
61
+ unknown ,
62
+ unknown
63
+ > | null > ( null ) ;
63
64
KanbanRoot . displayName = ROOT_NAME ;
64
65
65
66
function useKanbanRoot ( ) {
@@ -72,8 +73,8 @@ function useKanbanRoot() {
72
73
73
74
type KanbanProps < T , C > = DndContextProps & {
74
75
columns : Record < UniqueIdentifier , T [ ] > ;
76
+ onColumnsChange ?: ( columns : Record < UniqueIdentifier , T [ ] > ) => void ;
75
77
columnData : C [ ] ;
76
- onValueChange ?: ( columns : Record < UniqueIdentifier , T [ ] > ) => void ;
77
78
onMove ?: ( event : DragEndEvent ) => void ;
78
79
sensors ?: DndContextProps [ "sensors" ] ;
79
80
} & ( T extends object
@@ -87,7 +88,7 @@ function Kanban<T, C>(props: KanbanProps<T, C>) {
87
88
const {
88
89
columns,
89
90
columnData,
90
- onValueChange ,
91
+ onColumnsChange ,
91
92
sensors : sensorsProp ,
92
93
onMove,
93
94
getItemValue : getItemValueProp ,
@@ -132,7 +133,7 @@ function Kanban<T, C>(props: KanbanProps<T, C>) {
132
133
[ getColumnValueProp ] ,
133
134
) ;
134
135
135
- const contextValue = React . useMemo (
136
+ const contextValue = React . useMemo < KanbanProviderContextValue < T , C > > (
136
137
( ) => ( {
137
138
id,
138
139
columns,
@@ -145,20 +146,25 @@ function Kanban<T, C>(props: KanbanProps<T, C>) {
145
146
[ id , columns , columnData , activeId , getItemValue , getColumnValue ] ,
146
147
) ;
147
148
148
- const findContainer = ( id : UniqueIdentifier ) => {
149
- if ( id in columns ) return id ;
149
+ const getContainer = React . useCallback (
150
+ ( id : UniqueIdentifier ) => {
151
+ if ( id in columns ) return id ;
150
152
151
- for ( const [ columnId , items ] of Object . entries ( columns ) ) {
152
- if ( items . some ( ( item ) => getItemValue ( item ) === id ) ) {
153
- return columnId ;
153
+ for ( const [ columnId , items ] of Object . entries ( columns ) ) {
154
+ if ( items . some ( ( item ) => getItemValue ( item ) === id ) ) {
155
+ return columnId ;
156
+ }
154
157
}
155
- }
156
158
157
- return null ;
158
- } ;
159
+ return null ;
160
+ } ,
161
+ [ columns , getItemValue ] ,
162
+ ) ;
159
163
160
164
return (
161
- < KanbanRoot . Provider value = { contextValue as KanbanRootContextValue } >
165
+ < KanbanRoot . Provider
166
+ value = { contextValue as KanbanProviderContextValue < unknown , unknown > }
167
+ >
162
168
< DndContext
163
169
id = { id }
164
170
sensors = { sensorsProp ?? sensors }
@@ -174,8 +180,8 @@ function Kanban<T, C>(props: KanbanProps<T, C>) {
174
180
return ;
175
181
}
176
182
177
- const activeContainer = findContainer ( active . id ) ;
178
- const overContainer = findContainer ( over . id ) ;
183
+ const activeContainer = getContainer ( active . id ) ;
184
+ const overContainer = getContainer ( over . id ) ;
179
185
180
186
if ( ! activeContainer || ! overContainer ) {
181
187
setActiveId ( null ) ;
@@ -217,7 +223,7 @@ function Kanban<T, C>(props: KanbanProps<T, C>) {
217
223
}
218
224
219
225
overColumn . splice ( overIndex , 0 , movedItem ) ;
220
- onValueChange ?.( newColumns ) ;
226
+ onColumnsChange ?.( newColumns ) ;
221
227
}
222
228
} else {
223
229
const items = columns [ activeContainer ] ;
@@ -248,7 +254,7 @@ function Kanban<T, C>(props: KanbanProps<T, C>) {
248
254
activeIndex ,
249
255
overIndex ,
250
256
) ;
251
- onValueChange ?.( newColumns ) ;
257
+ onColumnsChange ?.( newColumns ) ;
252
258
}
253
259
}
254
260
}
0 commit comments