-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Makes number inputs always clearable when in focus irrespective…
… of upstream rules (#946)
- Loading branch information
1 parent
7740c43
commit 9542838
Showing
7 changed files
with
199 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@autoguru/overdrive': patch | ||
--- | ||
|
||
NumberInput gets to be cleared when upstream enforeces values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
packages/overdrive/lib/components/NumberInput/useNumberInputBehaviours.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { | ||
FocusEventHandler, | ||
FormEventHandler, | ||
RefObject, | ||
useCallback, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from 'react'; | ||
|
||
import { EnhanceInputPrimitiveProps } from '../private/InputBase/withEnhancedInput'; | ||
|
||
interface Props { | ||
value: EnhanceInputPrimitiveProps['value']; | ||
ref: RefObject<HTMLInputElement>; | ||
preventMouseWheel: boolean; | ||
onFocus?: FocusEventHandler<HTMLInputElement>; | ||
onBlur?: FocusEventHandler<HTMLInputElement>; | ||
onChange?: FormEventHandler<HTMLInputElement>; | ||
} | ||
|
||
interface Returns { | ||
value: EnhanceInputPrimitiveProps['value']; | ||
inputRef: RefObject<HTMLInputElement>; | ||
onFocus: FocusEventHandler<HTMLInputElement>; | ||
onBlur: FocusEventHandler<HTMLInputElement>; | ||
onChange: FormEventHandler<HTMLInputElement>; | ||
} | ||
|
||
export const useNumberInputBehaviours = ({ | ||
ref, | ||
preventMouseWheel, | ||
onBlur: incomingOnBlur, | ||
onFocus: incomingOnFocus, | ||
onChange: incomingOnChange, | ||
value, | ||
}: Props): Returns => { | ||
const inputRef = useRef<HTMLInputElement>(ref?.current); | ||
const [isFocused, setIsFocused] = useState(false); | ||
const [displayValue, setDisplayValue] = useState<string | undefined>(value); | ||
|
||
const onBlur = useCallback<FocusEventHandler<HTMLInputElement>>( | ||
(e) => { | ||
if (typeof incomingOnBlur === 'function') incomingOnBlur(e); | ||
setIsFocused(false); | ||
setDisplayValue(void 0); | ||
}, | ||
[incomingOnBlur], | ||
); | ||
const onFocus = useCallback<FocusEventHandler<HTMLInputElement>>( | ||
(e) => { | ||
if (typeof incomingOnFocus === 'function') incomingOnFocus(e); | ||
if (typeof value === 'string' && displayValue !== value) | ||
setDisplayValue(value); | ||
setIsFocused(true); | ||
}, | ||
[incomingOnFocus, value, displayValue], | ||
); | ||
const onChange = useCallback<FormEventHandler<HTMLInputElement>>( | ||
(e) => { | ||
if (isFocused) { | ||
setDisplayValue(e.currentTarget.value); | ||
} | ||
if (typeof incomingOnChange === 'function') incomingOnChange(e); | ||
}, | ||
[incomingOnChange, isFocused, value, displayValue], | ||
); | ||
|
||
const preventWheel = useCallback<EventListener>((e) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
}, []); | ||
|
||
useEffect(() => { | ||
let mouseWheelListener; | ||
let onWheelListener; | ||
let wheelListener; | ||
if (preventMouseWheel && inputRef?.current) { | ||
mouseWheelListener = inputRef.current.addEventListener( | ||
'mousewheel', | ||
preventWheel, | ||
{ passive: false }, | ||
); | ||
onWheelListener = inputRef.current.addEventListener( | ||
'onwheel', | ||
preventWheel, | ||
{ passive: false }, | ||
); | ||
wheelListener = inputRef.current.addEventListener( | ||
'wheel', | ||
preventWheel, | ||
{ passive: false }, | ||
); | ||
} | ||
|
||
return () => { | ||
if (mouseWheelListener && inputRef.current) | ||
// Standard | ||
inputRef.current.removeEventListener( | ||
'mousewheel', | ||
mouseWheelListener, | ||
); | ||
if (onWheelListener && inputRef.current) | ||
// Chrome | ||
inputRef.current.removeEventListener( | ||
'onwheel', | ||
onWheelListener, | ||
); | ||
if (wheelListener && inputRef.current) | ||
// Safari | ||
inputRef.current.removeEventListener('wheel', wheelListener); | ||
}; | ||
}, [preventMouseWheel, inputRef.current]); | ||
|
||
return { | ||
inputRef, | ||
onBlur, | ||
onFocus, | ||
onChange, | ||
value: typeof displayValue === 'string' ? displayValue : value, | ||
}; | ||
}; |