Skip to content

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

Fix progress Goal input styles / Convert ChartSettingFieldPicker to typescript (#75916)

Anderson6ea899e4b098
diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingFieldPicker.styled.tsx b/frontend/src/metabase/visualizations/components/settings/ChartSettingFieldPicker.styled.tsx
deleted file mode 100644
index da0f2fcfddd..00000000000
--- a/frontend/src/metabase/visualizations/components/settings/ChartSettingFieldPicker.styled.tsx
+++ /dev/null
@@ -1,24 +0,0 @@
-import isPropValid from "@emotion/is-prop-valid";
-// eslint-disable-next-line no-restricted-imports
-import styled from "@emotion/styled";
-
-import { Group, Icon } from "metabase/ui";
-
-export const GrabberHandle = styled(Icon, { shouldForwardProp: isPropValid })`
- color: var(--mb-color-text-secondary);
- cursor: grab;
-`;
-
-export const ChartSettingFieldPickerRoot = styled(Group, {
- shouldForwardProp: isPropValid,
-})`
- border: 1px solid var(--mb-color-border);
- border-radius: ${({ theme }) => theme.radius.md};
- overflow: hidden;
-
- &:hover {
- ${GrabberHandle} {
- color: var(--mb-color-core-brand);
- }
- }
-`;
diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingFieldPicker/ChartSettingFieldPicker.module.css b/frontend/src/metabase/visualizations/components/settings/ChartSettingFieldPicker/ChartSettingFieldPicker.module.css
new file mode 100644
index 00000000000..a21dde04d37
--- /dev/null
+++ b/frontend/src/metabase/visualizations/components/settings/ChartSettingFieldPicker/ChartSettingFieldPicker.module.css
@@ -0,0 +1,26 @@
+.root {
+ border: 1px solid var(--mb-color-border);
+ border-radius: var(--mantine-radius-md);
+ overflow: hidden;
+
+ &:hover {
+ .grabberHandle {
+ color: var(--mb-color-core-brand);
+ }
+ }
+}
+
+.grabberHandle {
+ color: var(--mb-color-text-secondary);
+ cursor: grab;
+}
+
+.chevronTarget {
+ align-items: center;
+ color: var(--mb-color-text-secondary);
+ cursor: pointer;
+ display: flex;
+ height: 100%;
+ justify-content: center;
+ width: 100%;
+}
diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingFieldPicker.jsx b/frontend/src/metabase/visualizations/components/settings/ChartSettingFieldPicker/ChartSettingFieldPicker.tsx
similarity index 64%
rename from frontend/src/metabase/visualizations/components/settings/ChartSettingFieldPicker.jsx
rename to frontend/src/metabase/visualizations/components/settings/ChartSettingFieldPicker/ChartSettingFieldPicker.tsx
index b6402a0ab06..ac4013410ca 100644
--- a/frontend/src/metabase/visualizations/components/settings/ChartSettingFieldPicker.jsx
+++ b/frontend/src/metabase/visualizations/components/settings/ChartSettingFieldPicker/ChartSettingFieldPicker.tsx
@@ -1,50 +1,76 @@
-/* eslint-disable react/prop-types */
+import cx from "classnames";
+import type { ComponentPropsWithoutRef, Ref } from "react";
import { useMemo } from "react";
import { t } from "ttag";
import _ from "underscore";
import CS from "metabase/css/core/index.css";
-import { ActionIcon, Group, Icon, useMantineTheme } from "metabase/ui";
+import { ActionIcon, Group, Icon } from "metabase/ui";
import { keyForSingleSeries } from "metabase/visualizations/lib/settings/series";
import { getColumnKey } from "metabase-lib/v1/queries/utils/column-key";
+import type { DatasetColumn, Series } from "metabase-types/api";
-import { ChartSettingActionIcon } from "./ChartSettingActionIcon";
-import { ChartSettingColorPicker } from "./ChartSettingColorPicker";
-import {
- ChartSettingFieldPickerRoot,
- GrabberHandle,
-} from "./ChartSettingFieldPicker.styled";
-import { ChartSettingSelect } from "./ChartSettingSelect";
+import { ChartSettingActionIcon } from "../ChartSettingActionIcon";
+import { ChartSettingColorPicker } from "../ChartSettingColorPicker";
+import { ChartSettingSelect } from "../ChartSettingSelect";
+
+import S from "./ChartSettingFieldPicker.module.css";
const RIGHT_SECTION_PADDING = 16;
const RIGHT_SECTION_BUTTON_WIDTH = 22;
+type MenuWidgetInfo = {
+ id: string;
+ props?: {
+ initialKey: string;
+ };
+};
+
+type ChartSettingFieldPickerProps = {
+ autoOpenWhenUnset?: boolean;
+ className?: string;
+ colors?: Record<string, string>;
+ columnHasSettings?: (column: DatasetColumn) => boolean;
+ columns?: DatasetColumn[];
+ dragHandleListeners?: Partial<ComponentPropsWithoutRef<typeof Icon>>;
+ dragHandleRef?: Ref<HTMLElement>;
+ fieldSettingWidget?: string | null;
+ onChange?: (value: string) => void;
+ onChangeSeriesColor?: (seriesKey: string, value: string) => void;
+ onRemove?: () => void;
+ onShowWidget?: (widget: MenuWidgetInfo, target: HTMLElement) => void;
+ options?: Array<{ name: string; value: string }>;
+ series?: Series;
+ showColorPicker?: boolean;
+ showColumnSetting?: boolean;
+ showDragHandle?: boolean;
+ value?: string;
+};
+
export const ChartSettingFieldPicker = ({
- value,
- options,
+ autoOpenWhenUnset = true,
+ className,
+ colors = {},
+ columnHasSettings,
+ columns,
+ dragHandleListeners,
+ dragHandleRef,
+ fieldSettingWidget = null,
onChange,
+ onChangeSeriesColor,
onRemove,
onShowWidget,
- className,
- columns,
+ options = [],
+ series,
+ showColorPicker,
showColumnSetting,
showDragHandle,
- dragHandleRef,
- dragHandleListeners,
- columnHasSettings,
- showColorPicker,
- colors,
- series,
- onChangeSeriesColor,
- autoOpenWhenUnset = true,
- fieldSettingWidget = null,
-}) => {
- const theme = useMantineTheme();
-
- let columnKey;
- if (value && showColumnSetting && columns) {
+ value,
+}: ChartSettingFieldPickerProps) => {
+ let columnKey: string | undefined;
+ if (typeof value === "string" && showColumnSetting && columns) {
const column = _.findWhere(columns, { name: value });
- if (column && columnHasSettings(column)) {
+ if (column && columnHasSettings?.(column)) {
columnKey = getColumnKey(column);
}
}
@@ -83,7 +109,9 @@ export const ChartSettingFieldPicker = ({
options.length === 0 ||
(options.length === 1 && options[0].value === value);
- const hasLeftSection = showDragHandle || (showColorPicker && seriesKey);
+ const hasLeftSection = Boolean(
+ showDragHandle || (showColorPicker && seriesKey),
+ );
const rightSectionWidth =
[!disabled, !!menuWidgetInfo, !!onRemove].filter(Boolean).length *
@@ -91,8 +119,8 @@ export const ChartSettingFieldPicker = ({
RIGHT_SECTION_PADDING;
return (
- <ChartSettingFieldPickerRoot
- className={className}
+ <Group
+ className={cx(S.root, className)}
data-testid="chartsettings-field-picker"
bg="background-primary"
align="center"
@@ -103,19 +131,18 @@ export const ChartSettingFieldPicker = ({
defaultDropdownOpened={autoOpenWhenUnset && value === undefined}
options={options}
value={value}
- onChange={onChange}
+ onChange={(value) => onChange?.(String(value))}
leftSection={
hasLeftSection ? (
<Group wrap="nowrap" gap="xs" p="xs" ml="sm" mr="md" align="center">
{showDragHandle && (
- <GrabberHandle
- ref={dragHandleRef}
+ <Icon
+ ref={dragHandleRef as Ref<SVGSVGElement>}
name="grabber"
- noMargin
{...dragHandleListeners}
onClick={(e) => e.stopPropagation()}
c="text-secondary"
- className={CS.pointerEventsAll}
+ className={cx(S.grabberHandle, CS.pointerEventsAll)}
data-testid="drag-handle"
/>
)}
@@ -124,7 +151,7 @@ export const ChartSettingFieldPicker = ({
pillSize="small"
value={colors[seriesKey]}
onChange={(value) => {
- onChangeSeriesColor(seriesKey, value);
+ onChangeSeriesColor?.(seriesKey, value);
}}
className={CS.pointerEventsAll}
/>
@@ -135,6 +162,7 @@ export const ChartSettingFieldPicker = ({
placeholderNoOptions={t`No valid fields`}
placeholder={t`Select a field`}
rightSectionWidth={`${rightSectionWidth}px`}
+ hasLeftSection={hasLeftSection}
rightSection={
<>
{!disabled && (
@@ -146,7 +174,7 @@ export const ChartSettingFieldPicker = ({
<ChartSettingActionIcon
icon="ellipsis"
data-testid={`settings-${value}`}
- onClick={(e) => onShowWidget(menuWidgetInfo, e.currentTarget)}
+ onClick={(e) => onShowWidget?.(menuWidgetInfo, e.currentTarget)}
/>
)}
{onRemove && (
@@ -158,36 +186,7 @@ export const ChartSettingFieldPicker = ({
)}
</>
}
- styles={{
- root: {
- overflow: "visible",
- padding: "0px",
- },
- wrapper: {
- marginTop: "0px",
- },
- section: {
- backgroundColor: "unset",
- zIndex: "initial",
- },
- input: {
- marginLeft: hasLeftSection ? theme.spacing.xs : 0,
- textOverflow: "ellipsis",
- fontWeight: "bold",
-
- backgroundColor: disabled
- ? "var(--mb-color-background-primary)"
- : "inherit",
-
- border: "none",
- width: "100%",
- color: "var(--mb-color-text-primary)",
- cursor: "pointer",
- pointerEvents: "unset",
- paddingRight: `${rightSectionWidth + 8}px`,
- },
- }}
/>
- </ChartSettingFieldPickerRoot>
+ </Group>
);
};
diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingFieldPicker/index.ts b/frontend/src/metabase/visualizations/components/settings/ChartSettingFieldPicker/index.ts
new file mode 100644
index 00000000000..8e94b8987f6
--- /dev/null
+++ b/frontend/src/metabase/visualizations/components/settings/ChartSettingFieldPicker/index.ts
@@ -0,0 +1 @@
+export { ChartSettingFieldPicker } from "./ChartSettingFieldPicker";
diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingGoalInput.tsx b/frontend/src/metabase/visualizations/components/settings/ChartSettingGoalInput.tsx
index 6441ec01130..78f6f0b5cc7 100644
--- a/frontend/src/metabase/visualizations/components/settings/ChartSettingGoalInput.tsx
+++ b/frontend/src/metabase/visualizations/components/settings/ChartSettingGoalInput.tsx
@@ -1,10 +1,27 @@
import { useMemo, useRef, useState } from "react";
import { t } from "ttag";
-import { ActionIcon, Icon, Menu, NumberInput, TextInput } from "metabase/ui";
+import { Box, Group, Icon, Menu, NumberInput, TextInput } from "metabase/ui";
import { isNumeric } from "metabase-lib/v1/types/utils/isa";
import type { DatasetColumn } from "metabase-types/api";
+import S from "./ChartSettingFieldPicker/ChartSettingFieldPicker.module.css";
+
+const RIGHT_SECTION_WIDTH = "38px";
+
+const inputStyles = {
+ input: {
+ border: "none",
+ fontWeight: "bold",
+ cursor: "pointer",
+ width: "100%",
+ },
+ section: {
+ backgroundColor: "unset",
+ zIndex: "initial",
+ },
+};
+
export type ChartSettingGoalInputProps = {
id: string;
value: number | string;
@@ -63,12 +80,11 @@ export const ChartSettingGoalInput = ({
opened={isPopoverOpen}
onChange={setIsPopoverOpen}
position="bottom-end"
- withArrow
>
<Menu.Target>
- <ActionIcon c="text-secondary" size="sm" radius="xl" p={0}>
+ <Box component="span" className={S.chevronTarget}>
<Icon name="chevrondown" />
- </ActionIcon>
+ </Box>
</Menu.Target>
<Menu.Dropdown miw={320}>
<Menu.Item onClick={() => handleMenuItemSelect(numericValue)} fw="bold">
@@ -90,28 +106,36 @@ export const ChartSettingGoalInput = ({
if (isColumnReference) {
return (
- <TextInput
- id={id}
- value={selectedColumn?.label || value}
- readOnly
- placeholder={selectedColumn?.label || value}
- fw="bold"
- rightSection={rightSection}
- rightSectionPointerEvents="all"
- />
+ <Group className={S.root} bg="background-primary" align="center">
+ <TextInput
+ id={id}
+ value={selectedColumn?.label || value}
+ readOnly
+ placeholder={selectedColumn?.label || value}
+ rightSection={rightSection}
+ rightSectionPointerEvents="all"
+ rightSectionWidth={RIGHT_SECTION_WIDTH}
+ styles={inputStyles}
+ w="100%"
+ />
+ </Group>
);
}
return (
- <NumberInput
- ref={numberInputRef}
- id={id}
- value={numericValue}
- onChange={(val) => onChange(val ?? 0)}
- placeholder={t`Enter goal value`}
- fw="bold"
- rightSection={rightSection}
- rightSectionPointerEvents="all"
- />
+ <Group className={S.root} bg="background-primary" align="center">
+ <NumberInput
+ ref={numberInputRef}
+ id={id}
+ value={numericValue}
+ onChange={(val) => onChange(val ?? 0)}
+ placeholder={t`Enter goal value`}
+ rightSection={rightSection}
+ rightSectionPointerEvents="all"
+ rightSectionWidth={RIGHT_SECTION_WIDTH}
+ styles={inputStyles}
+ w="100%"
+ />
+ </Group>
);
};
diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingSelect.module.css b/frontend/src/metabase/visualizations/components/settings/ChartSettingSelect.module.css
new file mode 100644
index 00000000000..a9539936d2e
--- /dev/null
+++ b/frontend/src/metabase/visualizations/components/settings/ChartSettingSelect.module.css
@@ -0,0 +1,38 @@
+.root {
+ overflow: visible;
+ padding: 0;
+
+ --chart-setting-select-input-padding-right: var(--input-padding-inline-end);
+}
+
+.wrapper {
+ margin-top: 0;
+}
+
+.section {
+ background-color: unset;
+ z-index: initial;
+}
+
+.input {
+ border: none;
+ color: var(--mb-color-text-primary);
+ cursor: pointer;
+ font-weight: bold;
+ pointer-events: unset;
+ padding-right: var(--chart-setting-select-input-padding-right);
+ text-overflow: ellipsis;
+ width: 100%;
+
+ &:not([data-disabled]) {
+ background-color: inherit;
+ }
+
+ &[data-disabled] {
+ background-color: var(--mb-color-background-primary);
+ }
+}
+
+.inputWithLeftSection {
+ margin-left: var(--mantine-spacing-xs);
+}
diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingSelect.jsx b/frontend/src/metabase/visualizations/components/settings/ChartSettingSelect.tsx
similarity index 50%
rename from frontend/src/metabase/visualizations/components/settings/ChartSettingSelect.jsx
rename to frontend/src/metabase/visualizations/components/settings/ChartSettingSelect.tsx
index df56b1e4bd8..ed04d902847 100644
--- a/frontend/src/metabase/visualizations/components/settings/ChartSettingSelect.jsx
+++ b/frontend/src/metabase/visualizations/components/settings/ChartSettingSelect.tsx
@@ -1,37 +1,70 @@
-/* eslint-disable react/prop-types */
import cx from "classnames";
+import type { ReactNode } from "react";
import CS from "metabase/css/core/index.css";
-import { Select, Stack } from "metabase/ui";
+import { Select, type SelectProps, Stack } from "metabase/ui";
import {
decodeWidgetValue,
encodeWidgetValue,
} from "metabase/visualizations/lib/settings/widgets";
+import S from "./ChartSettingSelect.module.css";
+
+export type ChartSettingSelectValue = string | number | boolean | null;
+
+type ChartSettingSelectOption = {
+ name: string;
+ value: ChartSettingSelectValue;
+};
+
+type ChartSettingSelectProps = Pick<
+ SelectProps,
+ | "className"
+ | "defaultDropdownOpened"
+ | "id"
+ | "leftSection"
+ | "leftSectionWidth"
+ | "pl"
+ | "pr"
+ | "rightSection"
+ | "w"
+> & {
+ footer?: ReactNode;
+ hasLeftSection?: boolean;
+ icon?: SelectProps["leftSection"];
+ iconWidth?: SelectProps["leftSectionWidth"];
+ onChange: (value: ChartSettingSelectValue) => void;
+ options?: ChartSettingSelectOption[];
+ placeholder?: string;
+ placeholderNoOptions?: string;
+ rightSectionWidth?: string;
+ searchProp?: string;
+ value?: ChartSettingSelectValue;
+};
+
export const ChartSettingSelect = ({
// Use null if value is undefined. If we pass undefined, Select will create an
// uncontrolled component because it's wrapped with Uncontrollable.
- value = null,
- onChange,
- options = [],
- isInitiallyOpen,
className,
- placeholder,
- placeholderNoOptions,
- id,
- searchProp,
+ defaultDropdownOpened,
+ footer,
+ hasLeftSection,
icon,
iconWidth,
+ id,
+ leftSection,
+ onChange,
+ options = [],
pl,
+ placeholder,
+ placeholderNoOptions,
pr,
- leftSection,
rightSection,
rightSectionWidth,
- styles,
+ searchProp,
+ value = null,
w,
- footer,
- defaultDropdownOpened,
-}) => {
+}: ChartSettingSelectProps) => {
const disabled =
options.length === 0 ||
(options.length === 1 && options[0].value === value);
@@ -41,9 +74,15 @@ export const ChartSettingSelect = ({
value: encodeWidgetValue(value) || "",
}));
+ const inputPaddingRight = rightSectionWidth
+ ? `${parseInt(rightSectionWidth, 10) + 8}px`
+ : undefined;
+
+ const resolvedLeftSection = leftSection ?? icon;
+
const dropdownComponent =
footer &&
- (({ children }) => (
+ (({ children }: { children: ReactNode }) => (
<Stack p={0} w="100%" gap={0}>
{children}
{footer}
@@ -55,31 +94,43 @@ export const ChartSettingSelect = ({
id={id}
data-testid="chart-setting-select"
className={cx(className, CS.block)}
+ {...(dropdownComponent ? { dropdownComponent } : {})}
+ classNames={{
+ root: S.root,
+ wrapper: S.wrapper,
+ section: S.section,
+ input: cx(S.input, { [S.inputWithLeftSection]: hasLeftSection }),
+ }}
data={data}
- dropdownComponent={dropdownComponent}
disabled={disabled}
value={value === null ? value : encodeWidgetValue(value)}
//Mantine V7 select onChange has 2 arguments passed. This breaks the assumption in visualizations/lib/settings.js where the onChange function is defined
- onChange={(v) => onChange(decodeWidgetValue(v))}
+ onChange={(v) => {
+ onChange(
+ v == null ? null : (decodeWidgetValue(v) as ChartSettingSelectValue),
+ );
+ }}
placeholder={options.length === 0 ? placeholderNoOptions : placeholder}
- initiallyOpened={isInitiallyOpen}
searchable={!!searchProp}
comboboxProps={{
withinPortal: false,
floatingStrategy: "fixed",
}}
- icon={icon}
- iconWidth={iconWidth}
- pl={pl}
- pr={pr}
- leftSection={leftSection}
+ leftSection={resolvedLeftSection}
+ leftSectionWidth={icon != null ? iconWidth : undefined}
rightSection={rightSection}
rightSectionProps={
rightSectionWidth ? { style: { width: rightSectionWidth } } : undefined
}
- styles={styles}
- w={w}
+ style={
+ inputPaddingRight
+ ? { "--chart-setting-select-input-padding-right": inputPaddingRight }
+ : undefined
+ }
defaultDropdownOpened={defaultDropdownOpened}
+ pl={pl}
+ pr={pr}
+ w={w}
/>
);
};
diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingSelect.unit.spec.jsx b/frontend/src/metabase/visualizations/components/settings/ChartSettingSelect.unit.spec.tsx
similarity index 98%
rename from frontend/src/metabase/visualizations/components/settings/ChartSettingSelect.unit.spec.jsx
rename to frontend/src/metabase/visualizations/components/settings/ChartSettingSelect.unit.spec.tsx
index e0515261524..ae779ba6eea 100644
--- a/frontend/src/metabase/visualizations/components/settings/ChartSettingSelect.unit.spec.jsx
+++ b/frontend/src/metabase/visualizations/components/settings/ChartSettingSelect.unit.spec.tsx
@@ -12,7 +12,7 @@ const options = [
{ name: "No value", value: null },
];
-const selectOption = async (optionText) => {
+const selectOption = async (optionText: string) => {
const select = screen.getByTestId("chart-setting-select");
await userEvent.click(select);
const option = screen.getByText(optionText);