Expose number precision setting

This commit is contained in:
Sam Nolan 2022-10-12 14:45:08 +11:00
parent 1ea3c975d5
commit 387f28c1c9
6 changed files with 39 additions and 3 deletions

View File

@ -46,6 +46,8 @@ export type SquiggleChartProps = {
/** Whether to show vega actions to the user, so they can copy the chart spec */ /** Whether to show vega actions to the user, so they can copy the chart spec */
distributionChartActions?: boolean; distributionChartActions?: boolean;
enableLocalSettings?: boolean; enableLocalSettings?: boolean;
/** Precision to show numbers */
numberPrecision?: number;
} & (StandaloneExecutionProps | ProjectExecutionProps); } & (StandaloneExecutionProps | ProjectExecutionProps);
// Props needed for a standalone execution // Props needed for a standalone execution
@ -121,6 +123,7 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = React.memo(
height = 200, height = 200,
enableLocalSettings = false, enableLocalSettings = false,
continues = defaultContinues, continues = defaultContinues,
numberPrecision,
} = props; } = props;
const p = React.useMemo(() => { const p = React.useMemo(() => {
@ -151,6 +154,7 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = React.memo(
result={valueToRender} result={valueToRender}
width={width} width={width}
height={height} height={height}
numberPrecision={numberPrecision}
distributionPlotSettings={distributionPlotSettings} distributionPlotSettings={distributionPlotSettings}
chartSettings={chartSettings} chartSettings={chartSettings}
environment={p.getEnvironment()} environment={p.getEnvironment()}

View File

@ -68,9 +68,12 @@ export const ExpressionViewer: React.FC<Props> = ({ value, width }) => {
case SqValueTag.Number: case SqValueTag.Number:
return ( return (
<VariableBox value={value} heading="Number"> <VariableBox value={value} heading="Number">
{() => ( {(settings) => (
<div className="font-semibold text-slate-600"> <div className="font-semibold text-slate-600">
<NumberShower precision={3} number={value.value} /> <NumberShower
precision={settings.numberPrecision}
number={value.value}
/>
</div> </div>
)} )}
</VariableBox> </VariableBox>

View File

@ -29,6 +29,7 @@ export const ViewerContext = React.createContext<ViewerContextShape>({
}, },
environment: defaultEnvironment, environment: defaultEnvironment,
height: 150, height: 150,
numberPrecision: 3,
}), }),
setSettings() {}, setSettings() {},
enableLocalSettings: false, enableLocalSettings: false,

View File

@ -23,6 +23,7 @@ type Props = {
/** Environment for further function executions */ /** Environment for further function executions */
environment: environment; environment: environment;
enableLocalSettings?: boolean; enableLocalSettings?: boolean;
numberPrecision?: number;
}; };
type Settings = { type Settings = {
@ -38,6 +39,7 @@ export const SquiggleViewer: React.FC<Props> = ({
distributionPlotSettings, distributionPlotSettings,
chartSettings, chartSettings,
environment, environment,
numberPrecision = 3,
enableLocalSettings = false, enableLocalSettings = false,
}) => { }) => {
// can't store settings in the state because we don't want to rerender the entire tree on every change // can't store settings in the state because we don't want to rerender the entire tree on every change
@ -74,10 +76,18 @@ export const SquiggleViewer: React.FC<Props> = ({
...(localSettings.environment || {}), ...(localSettings.environment || {}),
}, },
height: localSettings.height || height, height: localSettings.height || height,
numberPrecision: localSettings.numberPrecision || numberPrecision,
}; };
return result; return result;
}, },
[distributionPlotSettings, chartSettings, environment, height, getSettings] [
distributionPlotSettings,
chartSettings,
environment,
height,
getSettings,
numberPrecision,
]
); );
return ( return (

View File

@ -8,6 +8,7 @@ export type LocalItemSettings = {
chartSettings?: Partial<FunctionChartSettings>; chartSettings?: Partial<FunctionChartSettings>;
height?: number; height?: number;
environment?: Partial<environment>; environment?: Partial<environment>;
numberPrecision?: number;
}; };
export type MergedItemSettings = { export type MergedItemSettings = {
@ -15,6 +16,7 @@ export type MergedItemSettings = {
chartSettings: FunctionChartSettings; chartSettings: FunctionChartSettings;
height: number; height: number;
environment: environment; environment: environment;
numberPrecision: number;
}; };
export const locationAsString = (location: SqValueLocation) => export const locationAsString = (location: SqValueLocation) =>

View File

@ -0,0 +1,16 @@
import { render } from "@testing-library/react";
import React from "react";
import "@testing-library/jest-dom";
import { SquiggleChart } from "../src/index";
test("Precision", async () => {
const { container: highPrecision } = render(
<SquiggleChart code={"1.25476"} numberPrecision={5} />
);
expect(highPrecision).toHaveTextContent("1.2548");
const { container: lowPrecision } = render(
<SquiggleChart code={"1.25476"} numberPrecision={2} />
);
expect(lowPrecision).toHaveTextContent("1.3");
});