Skip to content

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

Convert ExpandableString to TypeScript (#69084)

Fraserdadf04298257
diff --git a/frontend/src/metabase/query_builder/components/ExpandableString.jsx b/frontend/src/metabase/query_builder/components/ExpandableString.jsx
deleted file mode 100644
index f68366e58dc..00000000000
--- a/frontend/src/metabase/query_builder/components/ExpandableString.jsx
+++ /dev/null
@@ -1,69 +0,0 @@
-/* eslint-disable react/prop-types */
-import cx from "classnames";
-import Humanize from "humanize-plus";
-import { Component } from "react";
-import { t } from "ttag";
-
-import CS from "metabase/css/core/index.css";
-
-export class ExpandableString extends Component {
- constructor(props, context) {
- super(props, context);
- this.toggleExpansion = this.toggleExpansion.bind(this);
-
- this.state = {
- expanded: false,
- };
- }
-
- static defaultProps = {
- length: 140,
- };
-
- UNSAFE_componentWillReceiveProps(newProps) {
- if (newProps.expanded !== undefined) {
- this.setState({
- expanded: newProps.expanded,
- });
- }
- }
-
- toggleExpansion(event) {
- event.stopPropagation();
- this.setState({
- expanded: !this.state.expanded,
- });
- }
-
- render() {
- if (!this.props.str) {
- return false;
- }
-
- const truncated = Humanize.truncate(this.props.str || "", 140);
-
- if (this.state.expanded) {
- return (
- <span>
- {this.props.str}{" "}
- <span
- className={cx(CS.block, CS.mt1, CS.link)}
- onClick={this.toggleExpansion}
- >{t`View less`}</span>
- </span>
- );
- } else if (truncated !== this.props.str) {
- return (
- <span>
- {truncated}{" "}
- <span
- className={cx(CS.block, CS.mt1, CS.link)}
- onClick={this.toggleExpansion}
- >{t`View more`}</span>
- </span>
- );
- } else {
- return <span>{this.props.str}</span>;
- }
- }
-}
diff --git a/frontend/src/metabase/query_builder/components/ExpandableString.tsx b/frontend/src/metabase/query_builder/components/ExpandableString.tsx
new file mode 100644
index 00000000000..55edfeb8d1d
--- /dev/null
+++ b/frontend/src/metabase/query_builder/components/ExpandableString.tsx
@@ -0,0 +1,50 @@
+import cx from "classnames";
+import Humanize from "humanize-plus";
+import { type MouseEvent, useState } from "react";
+import { t } from "ttag";
+
+import CS from "metabase/css/core/index.css";
+
+interface ExpandableStringProps {
+ str?: string;
+ length?: number;
+}
+
+export function ExpandableString({ str, length = 140 }: ExpandableStringProps) {
+ const [expanded, setExpanded] = useState(false);
+
+ if (!str) {
+ return null;
+ }
+
+ const truncated = Humanize.truncate(str, length);
+
+ const toggleExpansion = (event: MouseEvent) => {
+ event.stopPropagation();
+ setExpanded((prev) => !prev);
+ };
+
+ if (expanded) {
+ return (
+ <span>
+ {str}{" "}
+ <span
+ className={cx(CS.block, CS.mt1, CS.link)}
+ onClick={toggleExpansion}
+ >{t`View less`}</span>
+ </span>
+ );
+ } else if (truncated !== str) {
+ return (
+ <span>
+ {truncated}{" "}
+ <span
+ className={cx(CS.block, CS.mt1, CS.link)}
+ onClick={toggleExpansion}
+ >{t`View more`}</span>
+ </span>
+ );
+ } else {
+ return <span>{str}</span>;
+ }
+}