Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,35 @@ import * as React from 'react';
import {
ViewProps,
NativeSyntheticEvent,
NativeMethods,
TargetedEvent,
ViewStyle,
ImageSourcePropType,
} from 'react-native';

type Constructor<T> = 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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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<string | ImageSourcePropType>;

/**
* (iOS 13+ only)
Expand Down Expand Up @@ -145,7 +180,7 @@ export interface SegmentedControlProps extends ViewProps {
* ````
*/

declare class SegmentedControlComponent extends React.Component<SegmentedControlProps> {}
declare const SegmentedControlBase: Constructor<NativeMethods> &
typeof SegmentedControlComponent;
export default class SegmentedControl extends SegmentedControlBase {}
declare class SegmentedControl extends React.Component<SegmentedControlProps> {}
// Instances (refs) also expose the native methods (measure, focus, ...).
interface SegmentedControl extends SegmentedControlNativeMethods {}
export default SegmentedControl;
58 changes: 58 additions & 0 deletions types-test/typecheck.tsx
Original file line number Diff line number Diff line change
@@ -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 = () => (
<SegmentedControl
values={['One', 'Two']}
selectedIndex={0}
onChange={(event) => {
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 = () => <SegmentedControl notAProp />;
// @ts-expect-error -- wrong type for selectedIndex
export const WrongType = () => <SegmentedControl selectedIndex="1" />;

// A ref exposes the native methods.
export const WithRef = () => {
const ref = React.useRef<SegmentedControl>(null);
React.useEffect(() => {
ref.current?.measure(() => {});
}, []);
return <SegmentedControl ref={ref} values={['One']} />;
};

// #706: numeric fontWeight, as accepted by React Native text styles.
export const NumericFontWeight = () => (
<SegmentedControl
values={['a', 'b']}
fontStyle={{fontFamily: 'Optima', fontSize: 14, fontWeight: 600}}
activeFontStyle={{fontWeight: 'bold'}}
/>
);

// #706: onValueChange can be typed with a narrower value type.
type Size = 'S' | 'M' | 'L';
export const NarrowedValue = () => (
<SegmentedControl values={['S', 'M', 'L']} onValueChange={(v: Size) => v} />
);

// Image values, as documented in the README.
export const ImageValues = () => (
<SegmentedControl
values={['One', require('./icon.png'), {uri: 'https://example.com/a.png'}]}
/>
);

declare function require(path: string): number;
Loading