1
- import React , { useCallback , useLayoutEffect , useMemo , useState } from 'react' ;
1
+ import React , { useMemo } from 'react' ;
2
2
import { connect } from 'react-redux' ;
3
3
import toNS from 'mongodb-ns' ;
4
4
import type { Relationship } from '../../services/data-model-storage' ;
@@ -116,8 +116,21 @@ const CollectionDrawerContent: React.FunctionComponent<
116
116
onNoteChange,
117
117
onRenameCollection,
118
118
} ) => {
119
- const [ collectionName , setCollectionName ] = useState (
120
- ( ) => toNS ( namespace ) . collection
119
+ const { value : collectionName , ...nameInputProps } = useChangeOnBlur (
120
+ toNS ( namespace ) . collection ,
121
+ ( collectionName ) => {
122
+ const trimmedName = collectionName . trim ( ) ;
123
+ if ( trimmedName === toNS ( namespace ) . collection ) {
124
+ return ;
125
+ }
126
+ if ( ! isCollectionNameValid ) {
127
+ return ;
128
+ }
129
+ onRenameCollection (
130
+ namespace ,
131
+ `${ toNS ( namespace ) . database } .${ trimmedName } `
132
+ ) ;
133
+ }
121
134
) ;
122
135
123
136
const {
@@ -128,23 +141,6 @@ const CollectionDrawerContent: React.FunctionComponent<
128
141
[ collectionName , namespaces , namespace ]
129
142
) ;
130
143
131
- useLayoutEffect ( ( ) => {
132
- setCollectionName ( toNS ( namespace ) . collection ) ;
133
- } , [ namespace ] ) ;
134
-
135
- const onBlurCollectionName = useCallback ( ( ) => {
136
- const trimmedName = collectionName . trim ( ) ;
137
- if ( trimmedName === toNS ( namespace ) . collection ) {
138
- return ;
139
- }
140
-
141
- if ( ! isCollectionNameValid ) {
142
- return ;
143
- }
144
-
145
- onRenameCollection ( namespace , `${ toNS ( namespace ) . database } .${ trimmedName } ` ) ;
146
- } , [ collectionName , namespace , onRenameCollection , isCollectionNameValid ] ) ;
147
-
148
144
const noteInputProps = useChangeOnBlur ( note , ( newNote ) => {
149
145
onNoteChange ( namespace , newNote ) ;
150
146
} ) ;
@@ -157,12 +153,9 @@ const CollectionDrawerContent: React.FunctionComponent<
157
153
label = "Name"
158
154
sizeVariant = "small"
159
155
value = { collectionName }
156
+ { ...nameInputProps }
160
157
state = { isCollectionNameValid ? undefined : 'error' }
161
158
errorMessage = { collectionNameEditErrorMessage }
162
- onChange = { ( e ) => {
163
- setCollectionName ( e . target . value ) ;
164
- } }
165
- onBlur = { onBlurCollectionName }
166
159
/>
167
160
</ DMFormFieldContainer >
168
161
</ DMDrawerSection >
0 commit comments