Files
react-native/Libraries/Components/Switch/Switch.d.ts
T
David 5dd2f2e4b7 fix: Update incorrect SwitchChangeEvent type (#34931)
Summary:
I noticed that typescript type for `onChange`  event of `<Switch/>` was incorrect

```tsx
<Switch
  onChange={(event) => {
    // TS
    event.value; // boolean
    event.nativeEvent.value; //TS2339: Property 'value' does not exist on type 'Event'.
    // JS
    console.log(event.nativeEvent); // {value:false,target:87}
    console.log(event.value); // undefined
  }}
/>
```

## Changelog

[General] [Changed] - Typescript: update incorrect `SwitchChangeEvent` type

Pull Request resolved: https://github.com/facebook/react-native/pull/34931

Test Plan: ...

Reviewed By: lunaleaps

Differential Revision: D40240552

Pulled By: skinsshark

fbshipit-source-id: 4d39d547778de4ac4dc6c94471f05bfbe157d0e5
2022-10-12 13:05:35 -07:00

116 lines
3.1 KiB
TypeScript

/**
* 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.
*
* @format
*/
import type * as React from 'react';
import {Constructor} from 'Utilities';
import {NativeMethods} from '../../Renderer/shims/ReactNativeTypes';
import {ColorValue, StyleProp} from '../../StyleSheet/StyleSheet';
import {ViewStyle} from '../../StyleSheet/StyleSheetTypes';
import {ViewProps} from '../View/ViewPropTypes';
import {NativeSyntheticEvent, TargetedEvent} from '../../Types/CoreEventTypes';
export interface SwitchPropsIOS extends ViewProps {
/**
* Background color when the switch is turned on.
*
* @deprecated use trackColor instead
*/
onTintColor?: ColorValue | undefined;
/**
* Color of the foreground switch grip.
*
* @deprecated use thumbColor instead
*/
thumbTintColor?: ColorValue | undefined;
/**
* Background color when the switch is turned off.
*
* @deprecated use trackColor instead
*/
tintColor?: ColorValue | undefined;
}
export interface SwitchChangeEventData extends TargetedEvent {
value: boolean;
}
export interface SwitchChangeEvent
extends NativeSyntheticEvent<SwitchChangeEventData> {}
export interface SwitchProps extends SwitchPropsIOS {
/**
* Color of the foreground switch grip.
*/
thumbColor?: ColorValue | undefined;
/**
* Custom colors for the switch track
*
* Color when false and color when true
*/
trackColor?:
| {
false?: ColorValue | null | undefined;
true?: ColorValue | null | undefined;
}
| undefined;
/**
* If true the user won't be able to toggle the switch.
* Default value is false.
*/
disabled?: boolean | undefined;
/**
* Invoked with the change event as an argument when the value changes.
*/
onChange?:
| ((event: SwitchChangeEvent) => Promise<void> | void)
| null
| undefined;
/**
* Invoked with the new value when the value changes.
*/
onValueChange?: ((value: boolean) => Promise<void> | void) | null | undefined;
/**
* Used to locate this view in end-to-end tests.
*/
testID?: string | undefined;
/**
* The value of the switch. If true the switch will be turned on.
* Default value is false.
*/
value?: boolean | undefined;
/**
* On iOS, custom color for the background.
* Can be seen when the switch value is false or when the switch is disabled.
*/
ios_backgroundColor?: ColorValue | undefined;
style?: StyleProp<ViewStyle> | undefined;
}
/**
* Renders a boolean input.
*
* This is a controlled component that requires an `onValueChange` callback that
* updates the `value` prop in order for the component to reflect user actions.
* If the `value` prop is not updated, the component will continue to render
* the supplied `value` prop instead of the expected result of any user actions.
*/
declare class SwitchComponent extends React.Component<SwitchProps> {}
declare const SwitchBase: Constructor<NativeMethods> & typeof SwitchComponent;
export class Switch extends SwitchBase {}