diff --git a/index.d.ts b/index.d.ts index 2820e3f..00dec52 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,12 +5,35 @@ import * as React from 'react'; import { ViewProps, NativeSyntheticEvent, - NativeMethods, TargetedEvent, ViewStyle, + ImageSourcePropType, } from 'react-native'; -type Constructor = new (...args: any[]) => T; +/** + * Native methods available on a ref to the control. + * Declared here rather than imported, because React Native renamed its type + * (`NativeMethods` -> `HostInstance`) and only exports one or the other + * depending on the version. + */ +export interface SegmentedControlNativeMethods { + measure( + callback: ( + x: number, + y: number, + width: number, + height: number, + pageX: number, + pageY: number, + ) => void, + ): void; + measureInWindow( + callback: (x: number, y: number, width: number, height: number) => void, + ): void; + focus(): void; + blur(): void; + setNativeProps(nativeProps: object): void; +} export interface NativeSegmentedControlIOSChangeEvent extends TargetedEvent { value: string; @@ -44,7 +67,16 @@ export type FontStyle = { | '600' | '700' | '800' - | '900'; + | '900' + | 100 + | 200 + | 300 + | 400 + | 500 + | 600 + | 700 + | 800 + | 900; }; export interface SegmentedControlProps extends ViewProps { @@ -76,7 +108,9 @@ export interface SegmentedControlProps extends ViewProps { /** * Callback that is called when the user taps a segment; passes the segment's value as an argument */ - onValueChange?: (value: string) => void; + // Method syntax (rather than a function-typed property) so callers can + // annotate the value with a narrower type, e.g. a union of their labels. + onValueChange?(value: string): void; /** * The index in props.values of the segment to be (pre)selected. @@ -96,8 +130,9 @@ export interface SegmentedControlProps extends ViewProps { /** * The labels for the control's segment buttons, in order. + * Each value is a string, or an image (e.g. `require('./icon.png')`). */ - values?: string[]; + values?: ReadonlyArray; /** * (iOS 13+ only) @@ -145,7 +180,7 @@ export interface SegmentedControlProps extends ViewProps { * ```` */ -declare class SegmentedControlComponent extends React.Component {} -declare const SegmentedControlBase: Constructor & - typeof SegmentedControlComponent; -export default class SegmentedControl extends SegmentedControlBase {} +declare class SegmentedControl extends React.Component {} +// Instances (refs) also expose the native methods (measure, focus, ...). +interface SegmentedControl extends SegmentedControlNativeMethods {} +export default SegmentedControl; diff --git a/types-test/typecheck.tsx b/types-test/typecheck.tsx new file mode 100644 index 0000000..ad55feb --- /dev/null +++ b/types-test/typecheck.tsx @@ -0,0 +1,58 @@ +// Type-level tests for index.d.ts, checked by `pnpm tsc` (not a Jest test; the +// file name deliberately does not match Jest's *.test.* pattern). +/* eslint-disable react-native/no-inline-styles -- fontStyle props, not styles */ +import * as React from 'react'; +import SegmentedControl from '../index'; + +// #696: the component accepts its props when used as a JSX element. +export const Basic = () => ( + { + const index: number = event.nativeEvent.selectedSegmentIndex; + const value: string = event.nativeEvent.value; + return [index, value]; + }} + disabledIndices={[1]} + /> +); + +// Props are actually type-checked (an `any`-typed component would accept these). +// @ts-expect-error -- unknown prop +export const UnknownProp = () => ; +// @ts-expect-error -- wrong type for selectedIndex +export const WrongType = () => ; + +// A ref exposes the native methods. +export const WithRef = () => { + const ref = React.useRef(null); + React.useEffect(() => { + ref.current?.measure(() => {}); + }, []); + return ; +}; + +// #706: numeric fontWeight, as accepted by React Native text styles. +export const NumericFontWeight = () => ( + +); + +// #706: onValueChange can be typed with a narrower value type. +type Size = 'S' | 'M' | 'L'; +export const NarrowedValue = () => ( + v} /> +); + +// Image values, as documented in the README. +export const ImageValues = () => ( + +); + +declare function require(path: string): number;