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/frontend/components/number-input.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { useEffect, useState } from "react";6import { Col, Row, Input } from "antd";78interface Props {9number: number;10min?: number;11max?: number;12on_change: (n: number) => void;13unit?: string;14disabled?: boolean;15}1617export function NumberInput(props: Props) {18const [number, setNumber] = useState<string>(`${props.number}`);1920useEffect(() => {21setNumber(`${number}`);22}, [props.number]);2324function saveChange(e?) {25e?.preventDefault();26let m = parseInt(number);27if (!isFinite(m)) {28m = props.number;29}30if (props.min != null && m < props.min) {31m = props.min;32} else if (props.max != null && m > props.max) {33m = props.max;34}35setNumber(`${m}`);36props.on_change(m);37}3839return (40<Row gutter={16}>41<Col xs={16}>42<Input43type="text"44value={number}45onChange={(e) => setNumber(e.target.value)}46onBlur={saveChange}47onKeyDown={(e) => {48if (e.keyCode === 27) {49// reset back to its original value.50setNumber(`${props.number}`);51} else if (e.keyCode === 13) {52saveChange();53}54}}55disabled={props.disabled}56/>57</Col>58<Col xs={8} className="lighten">59{props.unit ? `${props.unit}` : ""}60</Col>61</Row>62);63}646566