-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/microsoft/pxt into thspar…
…ks/teacher_tool/import_export_rubric
- Loading branch information
Showing
37 changed files
with
584 additions
and
244 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useEffect, useRef } from "react"; | ||
import { Input, InputProps } from "react-common/components/controls/Input"; | ||
|
||
export interface DebouncedInputProps extends InputProps { | ||
intervalMs?: number; // Default 500 ms | ||
} | ||
|
||
// This functions like the React Common Input, but debounces onChange calls, | ||
// so if onChange is called multiple times in quick succession, it will only | ||
// be executed once after a pause of the specified `interval` in milliseconds. | ||
export const DebouncedInput: React.FC<DebouncedInputProps> = ({ intervalMs = 500, ...props }) => { | ||
const timerId = useRef<NodeJS.Timeout | undefined>(undefined); | ||
const latestValue = useRef<string>(""); | ||
|
||
const sendChange = () => { | ||
if (props.onChange) { | ||
props.onChange(latestValue.current); | ||
} | ||
}; | ||
|
||
// If the timer is pending and the component unmounts, | ||
// clear the timer and fire the onChange event immediately. | ||
useEffect(() => { | ||
return () => { | ||
if (timerId.current) { | ||
clearTimeout(timerId.current); | ||
sendChange(); | ||
} | ||
}; | ||
}, []); | ||
|
||
const onChangeDebounce = (newValue: string) => { | ||
latestValue.current = newValue; | ||
|
||
if (timerId.current) { | ||
clearTimeout(timerId.current); | ||
} | ||
|
||
timerId.current = setTimeout(sendChange, intervalMs); | ||
}; | ||
|
||
return <Input {...props} onChange={onChangeDebounce} />; | ||
}; |
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
Oops, something went wrong.