Skip to content

Commit 3b79fd9

Browse files
authored
chore(data-modeling): use change on blur hook for collection name for consistency (#7190)
1 parent f2f82cc commit 3b79fd9

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

packages/compass-data-modeling/src/components/drawer/collection-drawer-content.tsx

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useLayoutEffect, useMemo, useState } from 'react';
1+
import React, { useMemo } from 'react';
22
import { connect } from 'react-redux';
33
import toNS from 'mongodb-ns';
44
import type { Relationship } from '../../services/data-model-storage';
@@ -116,8 +116,21 @@ const CollectionDrawerContent: React.FunctionComponent<
116116
onNoteChange,
117117
onRenameCollection,
118118
}) => {
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+
}
121134
);
122135

123136
const {
@@ -128,23 +141,6 @@ const CollectionDrawerContent: React.FunctionComponent<
128141
[collectionName, namespaces, namespace]
129142
);
130143

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-
148144
const noteInputProps = useChangeOnBlur(note, (newNote) => {
149145
onNoteChange(namespace, newNote);
150146
});
@@ -157,12 +153,9 @@ const CollectionDrawerContent: React.FunctionComponent<
157153
label="Name"
158154
sizeVariant="small"
159155
value={collectionName}
156+
{...nameInputProps}
160157
state={isCollectionNameValid ? undefined : 'error'}
161158
errorMessage={collectionNameEditErrorMessage}
162-
onChange={(e) => {
163-
setCollectionName(e.target.value);
164-
}}
165-
onBlur={onBlurCollectionName}
166159
/>
167160
</DMFormFieldContainer>
168161
</DMDrawerSection>

0 commit comments

Comments
 (0)