squiggle/packages/components/src/SquigglePlayground.tsx

130 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-04-04 06:58:05 +00:00
import React, { FC, useState } from "react";
import ReactDOM from "react-dom";
import { SquiggleChart } from "./SquiggleChart";
import CodeEditor from "./CodeEditor";
import { Form, Input, Card, Row, Col } from "antd";
import "antd/dist/antd.css";
2022-03-02 23:22:26 +00:00
interface FieldFloatProps {
2022-04-04 06:58:05 +00:00
label: string;
className?: string;
value: number;
onChange: (value: number) => void;
2022-03-02 23:22:26 +00:00
}
function FieldFloat(Props: FieldFloatProps) {
let [contents, setContents] = useState(Props.value + "");
2022-04-04 06:58:05 +00:00
return (
<Form.Item label={Props.label}>
<Input
value={contents}
className={Props.className ? Props.className : ""}
onChange={(e) => setContents(e.target.value)}
onBlur={(_) => {
let result = parseFloat(contents);
if (result != NaN) {
Props.onChange(result);
}
}}
2022-03-02 23:22:26 +00:00
/>
2022-04-04 06:58:05 +00:00
</Form.Item>
);
2022-03-02 23:22:26 +00:00
}
2022-04-01 00:26:26 +00:00
interface Props {
2022-04-04 06:58:05 +00:00
initialSquiggleString: string;
2022-04-01 00:26:26 +00:00
}
2022-04-04 06:58:05 +00:00
let SquigglePlayground: FC<Props> = (props) => {
let [squiggleString, setSquiggleString] = useState(
props.initialSquiggleString
);
let [sampleCount, setSampleCount] = useState(1000);
let [outputXYPoints, setOutputXYPoints] = useState(1000);
let [pointDistLength, setPointDistLength] = useState(1000);
let [diagramStart, setDiagramStart] = useState(0);
let [diagramStop, setDiagramStop] = useState(10);
let [diagramCount, setDiagramCount] = useState(20);
var demoDist = (
2022-03-02 23:22:26 +00:00
<SquiggleChart
squiggleString={squiggleString}
sampleCount={sampleCount}
outputXYPoints={outputXYPoints}
diagramStart={diagramStart}
diagramStop={diagramStop}
diagramCount={diagramCount}
pointDistLength={pointDistLength}
2022-04-04 06:58:05 +00:00
/>
);
2022-03-02 23:22:26 +00:00
return (
2022-04-01 00:26:26 +00:00
<Row>
<Col span={12}>
2022-04-04 06:58:05 +00:00
<Card title="Distribution Form">
2022-03-02 23:22:26 +00:00
<Form>
<Row gutter={16}>
2022-04-04 06:58:05 +00:00
<Col span={24}>
<CodeEditor
value={squiggleString}
onChange={setSquiggleString}
oneLine={false}
/>{" "}
</Col>
2022-03-02 23:22:26 +00:00
</Row>
<Row gutter={16}>
2022-04-04 06:58:05 +00:00
<Col span={12}>
<FieldFloat
value={sampleCount}
2022-03-02 23:22:26 +00:00
label="Sample Count"
onChange={setSampleCount}
2022-04-04 06:58:05 +00:00
/>{" "}
</Col>
2022-03-02 23:22:26 +00:00
<Col span={12}>
<FieldFloat
value={outputXYPoints}
onChange={setOutputXYPoints}
2022-04-04 06:58:05 +00:00
label="Output XY-points"
/>
2022-03-02 23:22:26 +00:00
</Col>
<Col span={12}>
2022-04-04 06:58:05 +00:00
<FieldFloat
value={pointDistLength}
onChange={setPointDistLength}
label="Downsample To"
/>
2022-03-02 23:22:26 +00:00
</Col>
<Col span={12}>
2022-04-04 06:58:05 +00:00
<FieldFloat
2022-03-02 23:22:26 +00:00
value={diagramStart}
onChange={setDiagramStart}
label="Diagram Start"
2022-04-04 06:58:05 +00:00
/>
2022-03-02 23:22:26 +00:00
</Col>
2022-04-04 06:58:05 +00:00
<Col span={12}>
<FieldFloat
2022-03-02 23:22:26 +00:00
value={diagramStop}
onChange={setDiagramStop}
label="Diagram Stop"
2022-04-04 06:58:05 +00:00
/>{" "}
</Col>
2022-03-02 23:22:26 +00:00
<Col span={12}>
2022-04-04 06:58:05 +00:00
<FieldFloat
2022-03-02 23:22:26 +00:00
value={diagramCount}
onChange={setDiagramCount}
2022-04-04 06:58:05 +00:00
label="Diagram Count"
/>
2022-03-02 23:22:26 +00:00
</Col>
</Row>
</Form>
</Card>
2022-04-01 00:26:26 +00:00
</Col>
2022-04-04 06:58:05 +00:00
<Col span={12}>{demoDist}</Col>
2022-04-01 00:26:26 +00:00
</Row>
2022-04-04 06:58:05 +00:00
);
};
2022-04-01 00:26:26 +00:00
export default SquigglePlayground;
2022-04-04 06:58:05 +00:00
export function renderSquigglePlayground(props: Props) {
let parent = document.createElement("div");
2022-04-04 06:58:05 +00:00
ReactDOM.render(<SquigglePlayground {...props} />, parent);
return parent;
}