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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<ScrollView
contentContainerStyle={[
styles.container,
{backgroundColor: colorScheme === 'dark' ? '#000' : '#FFF'},

Check warning on line 34 in example/App.js

View workflow job for this annotation

GitHub Actions / lint

Inline style: { backgroundColor: "colorScheme === 'dark' ? '#000' : '#FFF'" }
]}>
<View style={styles.segmentContainer}>
<Text style={[styles.text, {color: textColor}]}>
Expand Down Expand Up @@ -65,6 +65,15 @@
</Text>
<SegmentedControl values={['One', 'Two']} momentary={true} />
</View>
<View style={styles.segmentSection}>
<Text style={[styles.text, {color: textColor}]}>
Segment widths can follow their content (iOS)
</Text>
<SegmentedControl
values={['S', 'Medium', 'Extra Large']}
apportionsSegmentWidthsByContent={true}
/>
</View>
<View style={styles.segmentSection}>
<Text style={[styles.text, {color: textColor}]}>
Segmented controls can be disabled
Expand Down Expand Up @@ -95,8 +104,8 @@
</View>
<View style={styles.segmentSection}>
<SegmentedControl
fontStyle={{color: '#ff00ff'}}

Check warning on line 107 in example/App.js

View workflow job for this annotation

GitHub Actions / lint

Inline style: { color: '#ff00ff' }
activeFontStyle={{color: 'blue'}}

Check warning on line 108 in example/App.js

View workflow job for this annotation

GitHub Actions / lint

Inline style: { color: 'blue' }
values={['One', 'Two']}
selectedIndex={1}
/>
Expand All @@ -108,15 +117,15 @@
<View style={styles.segmentContainer}>
<SegmentedControl
values={['One', 'Two']}
style={{height: 80}}

Check warning on line 120 in example/App.js

View workflow job for this annotation

GitHub Actions / lint

Inline style: { height: 80 }
fontStyle={styles.fontStyle}
/>
</View>
<SegmentedControl
values={['One', 'Two']}
tintColor="red"
style={{height: 80}}

Check warning on line 127 in example/App.js

View workflow job for this annotation

GitHub Actions / lint

Inline style: { height: 80 }
fontStyle={{

Check warning on line 128 in example/App.js

View workflow job for this annotation

GitHub Actions / lint

Inline style: { fontFamily: 'Optima', fontSize: 32 }
fontFamily: 'Optima',
fontSize: 32,
}}
Expand Down
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions ios/RNCSegmentedControlManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) && \
Expand Down
6 changes: 6 additions & 0 deletions js/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions types-test/typecheck.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ export const ImageValues = () => (
/>
);

// #883: segment widths that follow their content (iOS).
export const ContentWidths = () => (
<SegmentedControl
values={['S', 'Extra Large']}
apportionsSegmentWidthsByContent
/>
);

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