diff --git a/README.md b/README.md index dc2fdcd4..8041241d 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,16 @@ If true, then selecting a segment won't persist visually. The `onValueChange` ca --- +### `apportionsSegmentWidthsByContent` + +If true, the width of each segment is proportional to the width of its content, so short labels take less room than long ones. By default all segments have the same width. Maps to `UISegmentedControl`'s [`apportionsSegmentWidthsByContent`](https://developer.apple.com/documentation/uikit/uisegmentedcontrol/apportionssegmentwidthsbycontent). + +| Type | Required | Platform | +| ---- | -------- | -------- | +| bool | No | iOS | + +--- + ### `onChange` Callback that is called when the user taps a segment; passes the event as an argument diff --git a/example/App.js b/example/App.js index 68a108e5..29f1bbd4 100644 --- a/example/App.js +++ b/example/App.js @@ -65,6 +65,15 @@ const App = () => { + + + Segment widths can follow their content (iOS) + + + Segmented controls can be disabled diff --git a/index.d.ts b/index.d.ts index 00dec52f..00dcfbef 100644 --- a/index.d.ts +++ b/index.d.ts @@ -97,6 +97,13 @@ export interface SegmentedControlProps extends ViewProps { */ momentary?: boolean; + /** + * (iOS only) + * If true, the width of each segment is proportional to the width of its + * content instead of all segments sharing the same width. + */ + apportionsSegmentWidthsByContent?: boolean; + /** * Callback that is called when the user taps a segment; * passes the event as an argument diff --git a/ios/RNCSegmentedControlManager.m b/ios/RNCSegmentedControlManager.m index bc899cae..2234a545 100644 --- a/ios/RNCSegmentedControlManager.m +++ b/ios/RNCSegmentedControlManager.m @@ -29,6 +29,7 @@ - (UIView *)view { RCT_EXPORT_VIEW_PROPERTY(appearance, NSString) RCT_EXPORT_VIEW_PROPERTY(testIDS, NSArray) RCT_EXPORT_VIEW_PROPERTY(disabledIndices, NSArray) +RCT_EXPORT_VIEW_PROPERTY(apportionsSegmentWidthsByContent, BOOL) RCT_CUSTOM_VIEW_PROPERTY(fontStyle, NSObject, RNCSegmentedControl) { #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ diff --git a/js/types.js b/js/types.js index 898dfe1c..52eca1d3 100644 --- a/js/types.js +++ b/js/types.js @@ -94,6 +94,12 @@ export type SegmentedControlProps = Readonly<{| * The `onValueChange` callback will still work as expected. */ momentary?: ?boolean, + /** + * (iOS only) + * If true, the width of each segment is proportional to the width of its + * content instead of all segments sharing the same width. + */ + apportionsSegmentWidthsByContent?: ?boolean, /** * (iOS 13+ only, Android, Web) * Overrides the control's appearance irrespective of the OS theme diff --git a/types-test/typecheck.tsx b/types-test/typecheck.tsx index ad55feba..fb069ef4 100644 --- a/types-test/typecheck.tsx +++ b/types-test/typecheck.tsx @@ -55,4 +55,12 @@ export const ImageValues = () => ( /> ); +// #883: segment widths that follow their content (iOS). +export const ContentWidths = () => ( + +); + declare function require(path: string): number;