import React, { useRef, useState } from 'react'; import { cx, css } from 'emotion'; import useClickAway from 'react-use/lib/useClickAway'; import { measureText } from '../../utils/measureText'; import { useExpandableLabel, SegmentProps } from '.'; export interface SegmentInputProps extends SegmentProps { value: string | number; onChange: (text: string | number) => void; autofocus?: boolean; } const FONT_SIZE = 14; export function SegmentInput({ value: initialValue, onChange, Component, className, placeholder, autofocus = false, }: React.PropsWithChildren>) { const ref = useRef(null); const [value, setValue] = useState(initialValue); const [inputWidth, setInputWidth] = useState(measureText((initialValue || '').toString(), FONT_SIZE).width); const [Label, , expanded, setExpanded] = useExpandableLabel(autofocus); useClickAway(ref, () => { setExpanded(false); onChange(value); }); if (!expanded) { return (