-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
select: patch VSCodeCheckbox to fix onChange
Summary: Add a patched version of `VSCodeCheckbox` that ignores `onChange` if `checked` matches the last render. This allows proper support of "managed" checked toggle, for example, (de-)selecting all lines will (de-)select the chunk checkbox. By using the `onChange` event directly we got better support for keyboard events. Reviewed By: evangrayk Differential Revision: D48520304 fbshipit-source-id: 3b42569c7168779a75daba066e0a88cca4fff9ed
- Loading branch information
1 parent
3e9c5cf
commit 3c7b4f5
Showing
2 changed files
with
59 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import type {Constructable} from '@microsoft/fast-element'; | ||
|
||
import * as OriginalToolkit from '@vscode/webview-ui-toolkit/react'; | ||
import {useRef} from 'react'; | ||
|
||
/** | ||
* A patched version of VSCodeCheckbox. Do not trigger `onChange` if `checked` | ||
* is managed and changed by other places. | ||
* | ||
* See https://github.com/microsoft/vscode-webview-ui-toolkit/issues/408. | ||
*/ | ||
export function VSCodeCheckbox(props: VSCodeCheckboxProps) { | ||
// props.checked from the last render. | ||
const ignoreChecked = useRef<boolean | null>(null); | ||
if (ignoreChecked.current !== props.checked && props.checked != null) { | ||
ignoreChecked.current = props.checked; | ||
} | ||
|
||
// Replace onChange handler to ignore managed 'checked' changed by re-render. | ||
const onChange: VSCodeCheckboxProps['onChange'] = | ||
props.onChange == null | ||
? undefined | ||
: (...args) => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const checked = (args[0].target as any).checked; | ||
if (ignoreChecked.current == checked) { | ||
return; | ||
} | ||
// This seems obviously correct yet tsc cannot type check it. | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
return props.onChange(...args); | ||
}; | ||
|
||
return <OriginalToolkit.VSCodeCheckbox {...props} onChange={onChange} />; | ||
} | ||
|
||
type ExtractProps<T> = T extends Constructable<React.Component<infer P, unknown, unknown>> | ||
? P | ||
: never; | ||
type VSCodeCheckboxProps = ExtractProps<typeof OriginalToolkit.VSCodeCheckbox>; |