Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Automatically focus new Key field when adding user attributes (#77715)

Rodrigo López Dato31b35fb4526a
diff --git a/enterprise/frontend/src/metabase-enterprise/sandboxes/components/LoginAttributesWidget/LoginAttributeMappingEditor.tsx b/enterprise/frontend/src/metabase-enterprise/sandboxes/components/LoginAttributesWidget/LoginAttributeMappingEditor.tsx
index f9ca1a07303..d387afd4c20 100644
--- a/enterprise/frontend/src/metabase-enterprise/sandboxes/components/LoginAttributesWidget/LoginAttributeMappingEditor.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/sandboxes/components/LoginAttributesWidget/LoginAttributeMappingEditor.tsx
@@ -103,6 +103,7 @@ export const LoginAttributeMappingEditor = ({
const [entries, setEntries] = useState<MappingEditorEntry[]>(
structuredAttributes ? [] : buildEntries(simpleAttributes),
);
+ const [autoFocusIndex, setAutoFocusIndex] = useState<number | null>(null);
useEffect(() => {
// structuredAttributes can change if a different tenant is selected
@@ -121,6 +122,7 @@ export const LoginAttributeMappingEditor = ({
onError(hasError(newEntries));
} else {
onChange(buildMapping(newEntries));
+ setAutoFocusIndex(newEntries.length - 1);
}
};
@@ -143,6 +145,7 @@ export const LoginAttributeMappingEditor = ({
}}
keyOpts={keyOpts}
error={entryError(entries, key)}
+ autoFocus={index === autoFocusIndex}
/>
</Flex>
<Flex gap="sm" w="50%">
@@ -175,7 +178,9 @@ export const LoginAttributeMappingEditor = ({
variant="light"
size="xs"
leftSection={<Icon name="add" />}
- onClick={() => handleChange(addEntry(entries))}
+ onClick={() => {
+ handleChange(addEntry(entries));
+ }}
mt="md"
disabled={_.some(
entries,
@@ -193,11 +198,13 @@ const KeyInput = ({
value,
onChange,
error,
+ autoFocus,
}: {
value: string;
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
keyOpts: MappingEditorEntry["keyOpts"];
error?: string | boolean;
+ autoFocus?: boolean;
}) => {
if (keyOpts?.disabled) {
return (
@@ -215,6 +222,7 @@ const KeyInput = ({
w="100%"
onChange={onChange}
error={error}
+ autoFocus={autoFocus}
/>
);
};
diff --git a/enterprise/frontend/src/metabase-enterprise/sandboxes/components/LoginAttributesWidget/LoginAttributeMappingEditor.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/sandboxes/components/LoginAttributesWidget/LoginAttributeMappingEditor.unit.spec.tsx
index dc5b2479f86..06dff062907 100644
--- a/enterprise/frontend/src/metabase-enterprise/sandboxes/components/LoginAttributesWidget/LoginAttributeMappingEditor.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/sandboxes/components/LoginAttributesWidget/LoginAttributeMappingEditor.unit.spec.tsx
@@ -162,6 +162,20 @@ describe("LoginAttributeMappingEditor", () => {
});
});
+ it("focuses the key input of a newly added attribute", async () => {
+ setup({ simpleAttributes });
+
+ const keyInputs = await screen.findAllByPlaceholderText("Key");
+ keyInputs.forEach((input) => expect(input).not.toHaveFocus());
+
+ await userEvent.click(
+ await screen.findByRole("button", { name: /Add an attribute/ }),
+ );
+
+ const newKeyInput = (await screen.findAllByPlaceholderText("Key")).at(-1);
+ expect(newKeyInput).toHaveFocus();
+ });
+
it("can remove an attribute", async () => {
const { onChange } = setup({ simpleAttributes });