Skip to content

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

Convert CopyWidget to TypeScript (#69056)

Fraser85b82b52f6b8
diff --git a/frontend/src/metabase/common/components/CopyWidget/CopyWidget.jsx b/frontend/src/metabase/common/components/CopyWidget/CopyWidget.jsx
deleted file mode 100644
index cd05b7aac4d..00000000000
--- a/frontend/src/metabase/common/components/CopyWidget/CopyWidget.jsx
+++ /dev/null
@@ -1,36 +0,0 @@
-/* eslint-disable react/prop-types */
-import cx from "classnames";
-import { Component } from "react";
-
-import FormS from "metabase/css/components/form.module.css";
-import CS from "metabase/css/core/index.css";
-
-import { CopyWidgetButton } from "./CopyWidget.styled";
-
-export class CopyWidget extends Component {
- render() {
- const { value, onChange, style, ...props } = this.props;
- return (
- <div className={cx(CS.flex, CS.relative)} style={style}>
- <input
- className={cx(FormS.FormInput, CS.flexFull, {
- [FormS.noFocus]: !onChange,
- })}
- style={{
- paddingRight: 40,
- }}
- onClick={
- !onChange
- ? (e) => e.target.setSelectionRange(0, e.target.value.length)
- : null
- }
- value={value}
- onChange={onChange}
- readOnly={value && !onChange}
- {...props}
- />
- <CopyWidgetButton value={value} />
- </div>
- );
- }
-}
diff --git a/frontend/src/metabase/common/components/CopyWidget/CopyWidget.tsx b/frontend/src/metabase/common/components/CopyWidget/CopyWidget.tsx
new file mode 100644
index 00000000000..27eb31cacee
--- /dev/null
+++ b/frontend/src/metabase/common/components/CopyWidget/CopyWidget.tsx
@@ -0,0 +1,49 @@
+import cx from "classnames";
+import type { CSSProperties, ChangeEvent, InputHTMLAttributes } from "react";
+
+import FormS from "metabase/css/components/form.module.css";
+import CS from "metabase/css/core/index.css";
+
+import { CopyWidgetButton } from "./CopyWidget.styled";
+
+interface CopyWidgetProps
+ extends Omit<InputHTMLAttributes<HTMLInputElement>, "onChange" | "style"> {
+ value: string;
+ onChange?: (e: ChangeEvent<HTMLInputElement>) => void;
+ style?: CSSProperties;
+}
+
+export const CopyWidget = ({
+ value,
+ onChange,
+ style,
+ ...props
+}: CopyWidgetProps) => {
+ return (
+ <div className={cx(CS.flex, CS.relative)} style={style}>
+ <input
+ className={cx(FormS.FormInput, CS.flexFull, {
+ [FormS.noFocus]: !onChange,
+ })}
+ style={{
+ paddingRight: 40,
+ }}
+ onClick={
+ !onChange
+ ? (e) => {
+ e.currentTarget.setSelectionRange(
+ 0,
+ e.currentTarget.value.length,
+ );
+ }
+ : undefined
+ }
+ value={value}
+ onChange={onChange}
+ readOnly={!!value && !onChange}
+ {...props}
+ />
+ <CopyWidgetButton value={value} />
+ </div>
+ );
+};