-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite _commons to Typescript (#730)
* Working with typescript * Take two with combined refs * ♻️ Rewrite code to avoid ts errors. * ♻️ Get rid of ts lint error * ♻️ Fix eslint error * ♻️ Fix eslint error and warning * ♻️ Change elements to fix ts error * 🐛 Fix ts error * ♻️ More specific type
- Loading branch information
Showing
13 changed files
with
85 additions
and
65 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
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
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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,20 @@ | ||
import { useEffect, MutableRefObject } from 'react' | ||
|
||
export const useOutsideClick = ( | ||
ref: MutableRefObject<HTMLElement>, | ||
callback: () => void, | ||
): void => { | ||
const handleClick = (e: MouseEvent) => { | ||
if (ref.current && !ref.current.contains(e.target as Node)) { | ||
callback() | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
document.addEventListener('click', handleClick) | ||
|
||
return () => { | ||
document.removeEventListener('click', handleClick) | ||
} | ||
}) | ||
} |
1 change: 0 additions & 1 deletion
1
libraries/core-react/src/_common/index.js → libraries/core-react/src/_common/index.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
This file was deleted.
Oops, something went wrong.
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,21 @@ | ||
// Workaround | ||
// React ignores 'dispathEvent' on input/textarea, see https://github.com/facebook/react/issues/10135 | ||
type ReactInternalHack = { _valueTracker?: { setValue: (a: string) => void } } | ||
|
||
export const setReactInputValue = ( | ||
input: HTMLInputElement & ReactInternalHack, | ||
value: string, | ||
): void => { | ||
const previousValue = input.value | ||
|
||
input.value = value | ||
|
||
const tracker = input._valueTracker | ||
|
||
if (typeof tracker !== 'undefined') { | ||
tracker.setValue(previousValue) | ||
} | ||
|
||
//'change' instead of 'input', see https://github.com/facebook/react/issues/11488#issuecomment-381590324 | ||
input.dispatchEvent(new Event('change', { bubbles: true })) | ||
} |
This file was deleted.
Oops, something went wrong.
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,21 @@ | ||
import { useRef, useEffect, MutableRefObject } from 'react' | ||
|
||
type Ref<T> = MutableRefObject<T> | ||
type Props<T, P> = T | ((a: P) => void) | ||
|
||
export const useCombinedRefs = <T extends HTMLElement>( | ||
...refs: Props<Ref<T>, T>[] | ||
): Ref<T> => { | ||
const targetRef = useRef<T>(null) | ||
useEffect(() => { | ||
refs.forEach((ref) => { | ||
if (!ref) return | ||
if (typeof ref === 'function') { | ||
ref(targetRef.current) | ||
} else { | ||
ref.current = targetRef.current | ||
} | ||
}) | ||
}, [refs]) | ||
return targetRef | ||
} |
7 changes: 5 additions & 2 deletions
7
...ies/core-react/src/_common/useKeyPress.js → ...ies/core-react/src/_common/useKeyPress.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