import "@potzblitz/styles/lib/components/InputField.scss"; import React, { useState, useEffect } from "react"; import { uniqueName, InputFieldProps, NumberInputFieldProps, TextInputFieldProps, } from "@potzblitz/components/src/api"; export interface ReactInputFieldProps extends InptuFieldProps { value: string | number; } function NumberInput(props: NumberInputFieldProps) { return ( <> ); } function TextInput(props: TextInputFieldProps) { return ( ); } export function InputField(props: ReactInputFieldProps) { const [idStr, _id] = uniqueName(props.label); const [vO, setVO] = useState( parseFloat(props.value as string) || props.value ); useEffect(() => { setVO(props.value); }, [props.value]); const handleInput = (v: number | string) => { setVO(v); return v; }; const handleChange = (newV: number | string) => { props.onInput && props.onInput(newV); }; const handleBlur = () => { props.onBlur && props.onBlur(0); }; const clamp = (v: number, min: number, max: number): number => Math.min(Math.max(v, min), max); return (
{props.label && } {typeof vO === "string" ? ( { const target = e.target as HTMLInputElement; handleInput(String(target.value)); }} /> ) : ( { const target = e.target as HTMLInputElement; handleChange(handleInput(parseFloat(target.value) as number)); }} handleBlur={handleBlur} handleIncrease={() => { const newVO = clamp( (vO as number) + (props.step || 1), props.min || -Infinity, props.max || +Infinity ); setVO(newVO); props.onInput && props.onInput(newVO); }} handleDecrease={() => { const newVO = clamp( (vO as number) - (props.step || 1), props.min || -Infinity, props.max || +Infinity ); setVO(newVO); props.onInput && props.onInput(newVO); }} /> )} {props.showCopyButton && }
); }