Commit 6f468c67 by Erik Sundell Committed by GitHub

UI: Segment Input change (#20925)

* Add input segment

* Rename story

* Cleanup

* Fix lint error

* More cleanup

* Use measure text util

* Keep value in local state
parent 413be3a6
......@@ -12,18 +12,21 @@ export interface SegmentInputProps<T> extends SegmentProps<T> {
const FONT_SIZE = 14;
export function SegmentInput<T>({
value,
value: initialValue,
onChange,
Component,
className,
}: React.PropsWithChildren<SegmentInputProps<T>>) {
const ref = useRef(null);
const [inputWidth, setInputWidth] = useState<number>(measureText(value.toString(), FONT_SIZE).width);
const [value, setValue] = useState<number | string>(initialValue);
const [inputWidth, setInputWidth] = useState<number>(measureText(initialValue.toString(), FONT_SIZE).width);
const [Label, , expanded, setExpanded] = useExpandableLabel(false);
useClickAway(ref, () => setExpanded(false));
if (!expanded) {
return <Label Component={Component || <a className={cx('gf-form-label', 'query-part', className)}>{value}</a>} />;
return (
<Label Component={Component || <a className={cx('gf-form-label', 'query-part', className)}>{initialValue}</a>} />
);
}
const inputWidthStyle = css`
......@@ -39,10 +42,18 @@ export function SegmentInput<T>({
onChange={item => {
const { width } = measureText(item.target.value, FONT_SIZE);
setInputWidth(width);
onChange(item.target.value);
setValue(item.target.value);
}}
onBlur={() => {
setExpanded(false);
onChange(value);
}}
onKeyDown={e => {
if ([13, 27].includes(e.keyCode)) {
setExpanded(false);
onChange(value);
}
}}
onBlur={() => setExpanded(false)}
onKeyDown={e => [13, 27].includes(e.keyCode) && setExpanded(false)}
/>
);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment