From 491dd97c601e7ad35a75958d3e15899ebfe7f650 Mon Sep 17 00:00:00 2001 From: Simao Rodrigues Date: Wed, 19 Jan 2022 13:00:44 +0000 Subject: [PATCH 1/2] Better differenting visually when Target and SPF is being set on all features --- .../features/target-spf-item/component.tsx | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/app/components/features/target-spf-item/component.tsx b/app/components/features/target-spf-item/component.tsx index 51acf4a56d..3c9472f4e1 100644 --- a/app/components/features/target-spf-item/component.tsx +++ b/app/components/features/target-spf-item/component.tsx @@ -1,9 +1,11 @@ import React, { useEffect, useRef, useState } from 'react'; + import cx from 'classnames'; + import Button from 'components/button'; -import Slider from 'components/forms/slider'; -import Label from 'components/forms/label'; import Input from 'components/forms/input'; +import Label from 'components/forms/label'; +import Slider from 'components/forms/slider'; import { TargetSPFItemProps, Type } from './types'; @@ -23,6 +25,8 @@ export const TargetSPFItem: React.FC = ({ }: TargetSPFItemProps) => { const [targetValue, setTargetValue] = useState((target || defaultTarget) / 100); const [FPFValue, setFPFValue] = useState(fpf || defaultFPF); + const [inputFPFValue, setInputFPFValue] = useState(String(FPFValue)); + const sliderLabelRef = useRef(null); useEffect(() => { @@ -33,12 +37,18 @@ export const TargetSPFItem: React.FC = ({ if (typeof fpf !== 'undefined') setFPFValue(fpf); }, [fpf]); + useEffect(() => { + setInputFPFValue(String(FPFValue)); + }, [FPFValue]); + return (
= ({ 100%
-
+
{isAllTargets ? 'ALL SPF' : 'SPF'}
{ - setFPFValue(Number(inputValue)); - if (onChangeFPF) onChangeFPF(Number(inputValue)); + setInputFPFValue(inputValue); + }} + onBlur={() => { + // If user leaves the input empty, we'll revert to the original targetValue + if (!inputFPFValue) { + setInputFPFValue(String(FPFValue)); + return; + } + // Prevent changing all targets if user didn't actually change it + // (despite clicking on the input) + if (FPFValue === Number(inputFPFValue)) return; + setFPFValue(Number(inputFPFValue)); + if (onChangeFPF) onChangeFPF(Number(inputFPFValue)); }} />
From 47aa7687dc1f7552c4dd878f6ef091e2020fa05d Mon Sep 17 00:00:00 2001 From: Simao Rodrigues Date: Tue, 18 Jan 2022 13:07:02 +0000 Subject: [PATCH 2/2] Inputs to support focus related callbacks --- app/components/forms/input/component.tsx | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/components/forms/input/component.tsx b/app/components/forms/input/component.tsx index bc39bdf119..e26e38f757 100644 --- a/app/components/forms/input/component.tsx +++ b/app/components/forms/input/component.tsx @@ -1,7 +1,10 @@ import React, { InputHTMLAttributes } from 'react'; -import Icon from 'components/icon'; + +import { useFocus } from '@react-aria/interactions'; import cx from 'classnames'; +import Icon from 'components/icon'; + const THEME = { dark: { base: @@ -43,6 +46,9 @@ export interface InputProps extends InputHTMLAttributes { id: string; viewBox: string; }; + onFocus?: () => void; + onBlur?: () => void; + onFocusChange?: (isFocused: boolean) => void; } export const Input: React.FC = ({ @@ -52,10 +58,19 @@ export const Input: React.FC = ({ disabled = false, icon, className, + onFocus, + onBlur, + onFocusChange, ...props }: InputProps) => { const st = disabled ? 'disabled' : status; + const { focusProps: inputFocusProps } = useFocus({ + onFocus, + onBlur, + onFocusChange, + }); + return (
{icon && ( @@ -79,6 +94,7 @@ export const Input: React.FC = ({ 'pl-10': icon, [className]: !!className, })} + {...inputFocusProps} />
);