From 5b450d4912dc8282aa111c6dd1e856a1e322f2c3 Mon Sep 17 00:00:00 2001 From: Axel Bocciarelli Date: Fri, 12 Mar 2021 09:27:20 +0100 Subject: [PATCH] Make errors sticky in domain tooltip --- .../controls/DomainSlider/BoundEditor.tsx | 13 ++++++----- .../DomainSlider/BoundErrorMessage.tsx | 22 +++++++++++++++++-- .../DomainSlider/DomainTooltip.module.css | 2 ++ .../controls/DomainSlider/DomainTooltip.tsx | 10 ++++----- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/h5web/toolbar/controls/DomainSlider/BoundEditor.tsx b/src/h5web/toolbar/controls/DomainSlider/BoundEditor.tsx index 4659a8968..456cb7000 100644 --- a/src/h5web/toolbar/controls/DomainSlider/BoundEditor.tsx +++ b/src/h5web/toolbar/controls/DomainSlider/BoundEditor.tsx @@ -1,10 +1,11 @@ import { ReactElement, useEffect, useRef, useState } from 'react'; import { FiCheck, FiSlash } from 'react-icons/fi'; import { formatPreciseValue } from '../../../utils'; +import type { Bound } from '../../../vis-packs/core/models'; import styles from './BoundEditor.module.css'; interface Props { - label: string; + bound: Bound; value: number; isEditing: boolean; hasError: boolean; @@ -13,9 +14,9 @@ interface Props { } function BoundEditor(props: Props): ReactElement { - const { label, value, isEditing, hasError, onEditToggle, onChange } = props; + const { bound, value, isEditing, hasError, onEditToggle, onChange } = props; - const id = `${label}-bound`; + const id = `${bound}-bound`; const inputRef = useRef(null); const [inputValue, setInputValue] = useState(''); @@ -30,11 +31,11 @@ function BoundEditor(props: Props): ReactElement { inputRef.current?.blur(); } - if (isEditing && label === 'Min') { + if (isEditing && bound === 'min') { // Give focus to min field when opening tooltip in edit mode inputRef.current?.focus(); } - }, [isEditing, label]); + }, [isEditing, bound]); return (
{ + if (error) { + toggleSticky(true); + } + }, [error, toggleSticky]); + + if (!error && isSticky) { + // Maintain space occupied by error when it becomes `undefined` + // (so the tooltip doesn't shrink and closes) + return

; + } + + if (!error) { + return <>; + } // eslint-disable-next-line sonarjs/no-small-switch switch (error) { diff --git a/src/h5web/toolbar/controls/DomainSlider/DomainTooltip.module.css b/src/h5web/toolbar/controls/DomainSlider/DomainTooltip.module.css index 433c68c69..948e2d99c 100644 --- a/src/h5web/toolbar/controls/DomainSlider/DomainTooltip.module.css +++ b/src/h5web/toolbar/controls/DomainSlider/DomainTooltip.module.css @@ -39,6 +39,8 @@ } .error { + min-height: 2rem; + margin-top: -0.25rem; font-size: 0.9375em; font-weight: 600; color: var(--warn); diff --git a/src/h5web/toolbar/controls/DomainSlider/DomainTooltip.tsx b/src/h5web/toolbar/controls/DomainSlider/DomainTooltip.tsx index 1a2b83915..5aebb2bd8 100644 --- a/src/h5web/toolbar/controls/DomainSlider/DomainTooltip.tsx +++ b/src/h5web/toolbar/controls/DomainSlider/DomainTooltip.tsx @@ -43,21 +43,24 @@ function DomainTooltip(props: Props): ReactElement {

);