Skip to content

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

refactor: unify `TextInputBlurChange` component (#66796)

Gabriel Moreno81cd9980b6bb
diff --git a/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecretKeyWidget.tsx b/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecretKeyWidget.tsx
index f938c05e5f5..3165c1c800a 100644
--- a/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecretKeyWidget.tsx
+++ b/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecretKeyWidget.tsx
@@ -5,9 +5,8 @@ import { SetByEnvVarWrapper } from "metabase/admin/settings/components/widgets/A
import { useAdminSetting } from "metabase/api/utils";
import { ConfirmModal } from "metabase/common/components/ConfirmModal";
import { CopyButton } from "metabase/common/components/CopyButton";
-import InputBlurChange from "metabase/common/components/InputBlurChange";
import { UtilApi } from "metabase/services";
-import { Box, Button, Flex } from "metabase/ui";
+import { Box, Button, Flex, TextInputBlurChange } from "metabase/ui";
import { SettingHeader } from "../SettingHeader";
@@ -44,7 +43,7 @@ export const EmbeddingSecretKeyWidget = () => {
settingKey="embedding-secret-key"
>
<Flex gap="md" w="100%">
- <InputBlurChange
+ <TextInputBlurChange
value={value}
onBlurChange={(e) => handleChange(e.target.value)}
rightSection={value && <CopyButton value={value} />}
diff --git a/frontend/src/metabase/common/components/InputBlurChange/InputBlurChange.tsx b/frontend/src/metabase/common/components/InputBlurChange/InputBlurChange.tsx
deleted file mode 100644
index 1b98d5d979a..00000000000
--- a/frontend/src/metabase/common/components/InputBlurChange/InputBlurChange.tsx
+++ /dev/null
@@ -1,89 +0,0 @@
-import type * as React from "react";
-import { useCallback, useLayoutEffect, useRef, useState } from "react";
-import _ from "underscore";
-
-import { useUnmountLayout } from "metabase/common/hooks/use-unmount-layout";
-import { TextInput, type TextInputProps } from "metabase/ui";
-
-/**
- * A small wrapper around <input>, primarily should be used for the
- * `onBlurChange` feature, otherwise you should use <input> directly
- */
-
-type Value = string | number | null;
-type HTMLInputValue = string | number | undefined;
-
-export interface InputBlurChangeProps
- extends Omit<TextInputProps, "inputRef" | "value" | "onBlur"> {
- value: string | undefined;
- onBlurChange?: (event: { target: HTMLInputElement }) => void;
- normalize?: (value: Value) => Value;
-}
-
-/** see also: metabase/ui/components/inputs/TextInputBlurChange 🤦‍♀️ */
-const InputBlurChange = (props: InputBlurChangeProps) => {
- const {
- value,
- onChange,
- onBlurChange,
- normalize = (value) => value,
- ...restProps
- } = props;
- const [internalValue, setInternalValue] = useState<HTMLInputValue>(value);
- const inputRef = useRef<HTMLInputElement>(null);
-
- useLayoutEffect(() => {
- setInternalValue(value);
- }, [value]);
-
- const handleChange = useCallback(
- (event: React.ChangeEvent<HTMLInputElement>) => {
- setInternalValue(event.target.value);
-
- if (onChange) {
- onChange(event);
- setInternalValue(normalize(event.target.value) ?? undefined);
- }
- },
- [normalize, onChange],
- );
-
- const handleBlur = useCallback(
- (event: React.ChangeEvent<HTMLInputElement>) => {
- if (onBlurChange && (value || "") !== event.target.value) {
- onBlurChange(event);
- setInternalValue(normalize(event.target.value) ?? undefined);
- }
- },
- [normalize, onBlurChange, value],
- );
-
- useUnmountLayout(() => {
- const lastPropsValue = value || "";
- const currentValue = inputRef.current?.value || "";
-
- if (onBlurChange && inputRef.current && lastPropsValue !== currentValue) {
- onBlurChange({
- target: inputRef.current,
- });
- }
- });
-
- const inputProps = _.omit(restProps, "onBlur", "onBlurChange", "onChange");
-
- return (
- <TextInput
- {...inputProps}
- ref={inputRef}
- value={internalValue}
- onBlur={handleBlur}
- onChange={handleChange}
- styles={{
- input: { width: "100%" },
- }}
- />
- );
-};
-
-// eslint-disable-next-line import/no-default-export -- deprecated usage
-export default InputBlurChange;
diff --git a/frontend/src/metabase/common/components/InputBlurChange/InputBlurChange.unit.spec.tsx b/frontend/src/metabase/common/components/InputBlurChange/InputBlurChange.unit.spec.tsx
deleted file mode 100644
index b5123d08528..00000000000
--- a/frontend/src/metabase/common/components/InputBlurChange/InputBlurChange.unit.spec.tsx
+++ /dev/null
@@ -1,81 +0,0 @@
-import userEvent from "@testing-library/user-event";
-
-import { cleanup, render, screen } from "__support__/ui";
-
-import type { InputBlurChangeProps } from "./InputBlurChange";
-import InputBlurChange from "./InputBlurChange";
-
-describe("InputBlurChange", () => {
- beforeEach(() => {
- jest.clearAllMocks();
- });
-
- it('should trigger "onBlurChange" on input blur', async () => {
- const {
- props: { placeholder },
- mocks: { onBlurChange },
- } = setup();
-
- const inputEl = screen.getByPlaceholderText(placeholder);
- inputEl.focus();
- inputEl.blur();
-
- // should not be triggered if value hasn't changed
- expect(onBlurChange).toHaveBeenCalledTimes(0);
-
- await userEvent.type(inputEl, "test");
- inputEl.blur();
-
- expect(onBlurChange).toHaveBeenCalledTimes(1);
- expect(onBlurChange.mock.results[0].value).toBe("test");
- });
-
- it('should trigger "onBlurChange" on component unmount', async () => {
- const {
- props: { placeholder },
- mocks: { onBlurChange },
- } = setup();
-
- await userEvent.type(screen.getByPlaceholderText(placeholder), "test");
-
- cleanup();
-
- expect(onBlurChange).toHaveBeenCalledTimes(1);
- expect(onBlurChange.mock.results[0].value).toBe("test");
- });
-
- it("should set `internalValue` to the normalized value even if the normalized value is the same as the previous one", async () => {
- const value = "/";
- setup({ value, normalize: (value) => (value as string).trim() });
- const input = screen.getByDisplayValue(value) as HTMLInputElement;
- await userEvent.clear(input);
- await userEvent.type(input, " / ");
-
- const normalizedValue = "/";
- expect(input.value).toEqual(normalizedValue);
- });
-});
-
-function setup({
- value = "",
- placeholder = "Type some texto",
- normalize,
-}: Partial<InputBlurChangeProps> = {}) {
- const onChange = jest.fn();
- const onBlurChange = jest.fn((e) => e.target.value);
-
- render(
- <InputBlurChange
- value={value}
- placeholder={placeholder}
- onChange={onChange}
- onBlurChange={onBlurChange}
- normalize={normalize}
- />,
- );
-
- return {
- props: { value, placeholder },
- mocks: { onChange, onBlurChange },
- };
-}
diff --git a/frontend/src/metabase/common/components/InputBlurChange/index.tsx b/frontend/src/metabase/common/components/InputBlurChange/index.tsx
deleted file mode 100644
index 66cfd1ddb4d..00000000000
--- a/frontend/src/metabase/common/components/InputBlurChange/index.tsx
+++ /dev/null
@@ -1,2 +0,0 @@
-// eslint-disable-next-line import/no-default-export -- deprecated usage
-export { default } from "./InputBlurChange";
diff --git a/frontend/src/metabase/common/components/NumericInput/NumericInput.styled.tsx b/frontend/src/metabase/common/components/NumericInput/NumericInput.styled.tsx
index ecaf8f7544d..67a34a9b428 100644
--- a/frontend/src/metabase/common/components/NumericInput/NumericInput.styled.tsx
+++ b/frontend/src/metabase/common/components/NumericInput/NumericInput.styled.tsx
@@ -1,8 +1,8 @@
// eslint-disable-next-line no-restricted-imports
import styled from "@emotion/styled";
-import InputBlurChange from "metabase/common/components/InputBlurChange";
+import { TextInputBlurChange } from "metabase/ui";
-export const NumericInputBlurChange = styled(InputBlurChange)`
+export const NumericInputBlurChange = styled(TextInputBlurChange)`
width: auto;
`;
diff --git a/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/CustomLinkText.tsx b/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/CustomLinkText.tsx
index 9c5f6fa8f23..5b105256139 100644
--- a/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/CustomLinkText.tsx
+++ b/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/CustomLinkText.tsx
@@ -2,8 +2,8 @@ import cx from "classnames";
import { useCallback } from "react";
import { t } from "ttag";
-import InputBlurChange from "metabase/common/components/InputBlurChange";
import CS from "metabase/css/core/index.css";
+import { TextInputBlurChange } from "metabase/ui";
import type {
ArbitraryCustomDestinationClickBehavior,
ClickBehavior,
@@ -33,7 +33,7 @@ export const CustomLinkText = ({ clickBehavior, updateSettings }: Props) => {
className={S.Label}
htmlFor="link-text-template"
>{t`Customize link text (optional)`}</label>
- <InputBlurChange
+ <TextInputBlurChange
id="link-text-template"
className={cx(CS.block, CS.full)}
placeholder={t`E.x. Details for {{Column Name}}`}
diff --git a/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/CustomURLPicker.tsx b/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/CustomURLPicker.tsx
index caaa6653ac4..3948ec7c586 100644
--- a/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/CustomURLPicker.tsx
+++ b/frontend/src/metabase/dashboard/components/ClickBehaviorSidebar/LinkOptions/CustomURLPicker.tsx
@@ -3,10 +3,16 @@ import cx from "classnames";
import { useCallback, useState } from "react";
import { t } from "ttag";
-import InputBlurChange from "metabase/common/components/InputBlurChange";
import CS from "metabase/css/core/index.css";
import { isTableDisplay } from "metabase/lib/click-behavior";
-import { Button, Flex, Icon, Modal, Text } from "metabase/ui";
+import {
+ Button,
+ Flex,
+ Icon,
+ Modal,
+ Text,
+ TextInputBlurChange,
+} from "metabase/ui";
import type { UiParameter } from "metabase-lib/v1/parameters/types";
import { clickBehaviorIsValid } from "metabase-lib/v1/parameters/utils/click-behavior";
import type {
@@ -100,7 +106,7 @@ export function CustomURLPicker({
<Text>
{t`You can insert the value of a column or dashboard filter using its name, like this: {{some_column}}`}
</Text>
- <InputBlurChange
+ <TextInputBlurChange
autoFocus
value={url}
placeholder={t`e.g. http://acme.com/id/\{\{user_id\}\}`}
diff --git a/frontend/src/metabase/reference/components/EditableReferenceHeader.jsx b/frontend/src/metabase/reference/components/EditableReferenceHeader.jsx
index 5c3d57f4227..b6c43303af5 100644
--- a/frontend/src/metabase/reference/components/EditableReferenceHeader.jsx
+++ b/frontend/src/metabase/reference/components/EditableReferenceHeader.jsx
@@ -6,10 +6,9 @@ import { t } from "ttag";
import Button from "metabase/common/components/Button";
import { Ellipsified } from "metabase/common/components/Ellipsified";
-import InputBlurChange from "metabase/common/components/InputBlurChange";
import L from "metabase/common/components/List/List.module.css";
import CS from "metabase/css/core/index.css";
-import { Icon } from "metabase/ui";
+import { Icon, TextInputBlurChange } from "metabase/ui";
import S from "./ReferenceHeader.module.css";
@@ -45,7 +44,7 @@ const EditableReferenceHeader = ({
}
>
{isEditing && name === "Details" ? (
- <InputBlurChange
+ <TextInputBlurChange
className={S.headerTextInput}
type="text"
name={
diff --git a/frontend/src/metabase/ui/components/inputs/TextInputBlurChange/TextInputBlurChange.tsx b/frontend/src/metabase/ui/components/inputs/TextInputBlurChange/TextInputBlurChange.tsx
index aaf137cdee9..eb01d1d9362 100644
--- a/frontend/src/metabase/ui/components/inputs/TextInputBlurChange/TextInputBlurChange.tsx
+++ b/frontend/src/metabase/ui/components/inputs/TextInputBlurChange/TextInputBlurChange.tsx
@@ -14,7 +14,7 @@ export type TextInputBlurChangeProps<
normalize?: (value?: T["value"] | undefined) => T["value"] | undefined;
resetOnEsc?: boolean;
value: T["value"] | undefined;
- onBlurChange: (event: { target: HTMLInputElement }) => void;
+ onBlurChange?: (event: { target: HTMLInputElement }) => void;
};
/**
@@ -76,7 +76,7 @@ export function TextInputBlurChange<T extends TextInputProps = TextInputProps>({
const lastPropsValue = value || "";
const currentValue = ref.current?.value || "";
- if (ref.current && lastPropsValue !== currentValue) {
+ if (onBlurChange && ref.current && lastPropsValue !== currentValue) {
onBlurChange({
target: ref.current,
});
diff --git a/frontend/src/metabase/visualizations/components/settings/ChartNestedSettingSeries.styled.tsx b/frontend/src/metabase/visualizations/components/settings/ChartNestedSettingSeries.styled.tsx
index 2f3e02291f8..ae4004e005c 100644
--- a/frontend/src/metabase/visualizations/components/settings/ChartNestedSettingSeries.styled.tsx
+++ b/frontend/src/metabase/visualizations/components/settings/ChartNestedSettingSeries.styled.tsx
@@ -2,8 +2,7 @@
import styled from "@emotion/styled";
import Input from "metabase/common/components/Input";
-import InputBlurChange from "metabase/common/components/InputBlurChange";
-import { Icon } from "metabase/ui";
+import { Icon, TextInputBlurChange } from "metabase/ui";
export const OptionsIcon = styled(Icon)`
color: var(--mb-color-text-medium);
@@ -14,7 +13,7 @@ export const OptionsIcon = styled(Icon)`
}
`;
-export const SeriesNameInput = styled(InputBlurChange)`
+export const SeriesNameInput = styled(TextInputBlurChange)`
width: auto;
${({ description }) =>