Skip to content

Commit

Permalink
feat: Add currencies controls in control panels (#24718)
Browse files Browse the repository at this point in the history
  • Loading branch information
kgabryje authored Aug 2, 2023
1 parent 1a9c559 commit f7e76d0
Show file tree
Hide file tree
Showing 63 changed files with 697 additions and 306 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@
* under the License.
*/
import RadioButtonControl from './RadioButtonControl';
import ColumnConfigControl from './ColumnConfigControl';

export * from './RadioButtonControl';
export * from './ColumnConfigControl';

/**
* Shared chart controls. Can be referred via string shortcuts in chart control
* configs.
*/
export default {
RadioButtonControl,
ColumnConfigControl,
};
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ const y_axis_format: SharedControlConfig<'SelectControl', SelectDefaultOption> =
},
};

const currency_format: SharedControlConfig<'CurrencyControl'> = {
type: 'CurrencyControl',
label: t('Currency format'),
renderTrigger: true,
};

const x_axis_time_format: SharedControlConfig<
'SelectControl',
SelectDefaultOption
Expand Down Expand Up @@ -406,4 +412,5 @@ export default {
x_axis: dndXAxisControl,
show_empty_columns,
temporal_columns_lookup,
currency_format,
};
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import type {
import { sharedControls, sharedControlComponents } from './shared-controls';

export type { Metric } from '@superset-ui/core';
export type { ControlFormItemSpec } from './components/ControlForm';
export type { ControlComponentProps } from './shared-controls/components/types';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -167,6 +166,12 @@ export type InternalControlType =
| 'DndColumnSelect'
| 'DndFilterSelect'
| 'DndMetricSelect'
| 'CurrencyControl'
| 'InputNumber'
| 'Checkbox'
| 'Select'
| 'Slider'
| 'Input'
| keyof SharedControlComponents; // expanded in `expandControlConfig`

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -495,3 +500,60 @@ export type SortSeriesData = {
sort_series_type: SortSeriesType;
sort_series_ascending: boolean;
};

export type ControlFormValueValidator<V> = (value: V) => string | false;

export type ControlFormItemSpec<T extends ControlType = ControlType> = {
controlType: T;
label: ReactNode;
description: ReactNode;
placeholder?: string;
validators?: ControlFormValueValidator<any>[];
width?: number | string;
/**
* Time to delay change propagation.
*/
debounceDelay?: number;
} & (T extends 'Select'
? {
options: any;
value?: string;
defaultValue?: string;
creatable?: boolean;
minWidth?: number | string;
validators?: ControlFormValueValidator<string>[];
}
: T extends 'RadioButtonControl'
? {
options: [string, ReactNode][];
value?: string;
defaultValue?: string;
}
: T extends 'Checkbox'
? {
value?: boolean;
defaultValue?: boolean;
}
: T extends 'InputNumber' | 'Slider'
? {
min?: number;
max?: number;
step?: number;
value?: number;
defaultValue?: number;
validators?: ControlFormValueValidator<number>[];
}
: T extends 'Input'
? {
controlType: 'Input';
value?: string;
defaultValue?: string;
validators?: ControlFormValueValidator<string>[];
}
: T extends 'CurrencyControl'
? {
controlType: 'CurrencyControl';
value?: Currency;
defaultValue?: Currency;
}
: {});
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface CurrencyFormatter {
(value: number | null | undefined): string;
}

export const getCurrencySymbol = (currency: Currency) =>
export const getCurrencySymbol = (currency: Partial<Currency>) =>
new Intl.NumberFormat('en-US', {
style: 'currency',
currency: currency.symbol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,24 @@ import {

export const buildCustomFormatters = (
metrics: QueryFormMetric | QueryFormMetric[] | undefined,
currencyFormats: Record<string, Currency>,
columnFormats: Record<string, string>,
savedCurrencyFormats: Record<string, Currency>,
savedColumnFormats: Record<string, string>,
d3Format: string | undefined,
currencyFormat: Currency | undefined,
) => {
const metricsArray = ensureIsArray(metrics);
return metricsArray.reduce((acc, metric) => {
if (isSavedMetric(metric)) {
const actualD3Format = d3Format ?? columnFormats[metric];
return currencyFormats[metric]
const actualD3Format = d3Format ?? savedColumnFormats[metric];
const actualCurrencyFormat = currencyFormat?.symbol
? currencyFormat
: savedCurrencyFormats[metric];
return actualCurrencyFormat
? {
...acc,
[metric]: new CurrencyFormatter({
d3Format: actualD3Format,
currency: currencyFormats[metric],
currency: actualCurrencyFormat,
}),
}
: {
Expand All @@ -67,13 +71,29 @@ export const getCustomFormatter = (

export const getValueFormatter = (
metrics: QueryFormMetric | QueryFormMetric[] | undefined,
currencyFormats: Record<string, Currency>,
columnFormats: Record<string, string>,
savedCurrencyFormats: Record<string, Currency>,
savedColumnFormats: Record<string, string>,
d3Format: string | undefined,
currencyFormat: Currency | undefined,
key?: string,
) =>
getCustomFormatter(
buildCustomFormatters(metrics, currencyFormats, columnFormats, d3Format),
) => {
const customFormatter = getCustomFormatter(
buildCustomFormatters(
metrics,
savedCurrencyFormats,
savedColumnFormats,
d3Format,
currencyFormat,
),
metrics,
key,
) ?? getNumberFormatter(d3Format);
);

if (customFormatter) {
return customFormatter;
}
if (currencyFormat?.symbol) {
return new CurrencyFormatter({ currency: currencyFormat, d3Format });
}
return getNumberFormatter(d3Format);
};
Loading

0 comments on commit f7e76d0

Please sign in to comment.