Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/components/misc/integer-slider.tsx
Views: 687
import { Button, Slider, InputNumber, Row, Col, Tag } from "antd";1import { useState } from "react";2import { COLORS as COLORS_THEME } from "@cocalc/util/theme";34interface Props {5min: number;6max: number;7maxText?: number;8value?: number;9defaultValue?: number;10initialValue?: number;11onChange: (number) => void;12units?: string;13presets?: number[];14disabled?: boolean;15step?: number;16}1718export default function IntegerSlider({19value,20onChange,21min,22max,23maxText,24defaultValue,25initialValue,26units,27presets,28disabled = false,29step = 1,30}: Props) {31function toNumber(x) {32return typeof x === "number" ? x : min;33}34const [val, setVal] = useState<number>(35value ?? initialValue ?? defaultValue ?? min36);3738return (39<>40<Row justify="space-between">41<Col xs={24} sm={12} flex="auto">42<Slider43disabled={disabled}44style={{ width: "100%" }}45min={min}46max={max}47onChange={(x) => {48onChange(x);49setVal(x);50}}51value={value != null ? toNumber(value) : val}52defaultValue={initialValue}53step={step}54/>55</Col>56<Col xs={24} sm={12} md={8}>57<InputNumber58min={min}59max={maxText ?? max}60step={step}61disabled={disabled}62style={{63marginLeft: "16px",64marginBottom: "5px",65minWidth: "8ex",66width: "20ex",67}}68defaultValue={initialValue}69value={value ?? val}70onChange={(value) => {71let val = toNumber(value);72if (step !== 1) {73// quantize val to a multiple of step74val = Math.round(val / step) * step;75}76onChange(val);77setVal(val);78}}79addonAfter={units}80/>81{defaultValue != null && (82<Button83type="dashed"84disabled={disabled || (value ?? val) == defaultValue}85style={{ marginLeft: "5px" }}86onClick={() => {87onChange(defaultValue);88setVal(defaultValue);89}}90>91Default: {defaultValue}92</Button>93)}94</Col>95</Row>96{presets && (97<div>98{presets.map((number) => (99<Tag100key={number}101color={disabled ? COLORS_THEME.GRAY_LL : "blue"}102style={{ cursor: disabled ? "not-allowed" : "pointer" }}103onClick={() => {104if (disabled) return;105onChange(number);106setVal(number);107}}108>109{number}110</Tag>111))}112</div>113)}114</>115);116}117118119