From 671089bded92823752ea2aa644ed06b624690bbd Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Fri, 20 Nov 2026 04:34:46 +0530 Subject: [PATCH 1/2] feat: add disabledIndices prop to disable individual segments Allow disabling specific segments while the rest of the control stays interactive (#615, #519). - iOS: forward the new prop to the native view, which calls setEnabled:forSegmentAtIndex: for every segment. The state is reapplied when the values change, since UISegmentedControl drops it when its segments are rebuilt. - Android/web: disable and dim the matching tabs; a control disabled via `enabled` keeps all of its segments disabled. - Add typings (Flow and TypeScript), README docs and unit tests. --- README.md | 8 ++++ index.d.ts | 6 +++ ios/RNCSegmentedControl.h | 1 + ios/RNCSegmentedControl.m | 13 +++++++ ios/RNCSegmentedControlManager.m | 1 + js/SegmentedControl.js | 5 ++- js/SegmentedControlTab.js | 5 ++- js/__tests__/SegmentedControl.test.js | 55 +++++++++++++++++++++++++++ js/types.js | 5 +++ 9 files changed, 97 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 10a0255a..dc2fdcd4 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,14 @@ If false the user won't be able to interact with the control. Default value is t | ---- | -------- | | bool | No | +### `disabledIndices` + +Indexes of the segments (positions in `values`) the user can't interact with, while the other segments stay interactive. For example, `disabledIndices={[1]}` disables the second segment. Out-of-range indexes are ignored. + +| Type | Required | +| --------------- | -------- | +| array of number | No | + --- ### `momentary` diff --git a/index.d.ts b/index.d.ts index aa8d5f54..2820e3fe 100644 --- a/index.d.ts +++ b/index.d.ts @@ -53,6 +53,12 @@ export interface SegmentedControlProps extends ViewProps { */ enabled?: boolean; + /** + * Indexes of the segments (in `props.values`) the user can't interact with. + * The other segments remain interactive. + */ + disabledIndices?: number[]; + /** * If true, then selecting a segment won't persist visually. * The onValueChange callback will still work as expected. diff --git a/ios/RNCSegmentedControl.h b/ios/RNCSegmentedControl.h index 5516e946..46d893d2 100644 --- a/ios/RNCSegmentedControl.h +++ b/ios/RNCSegmentedControl.h @@ -12,5 +12,6 @@ @property(nonatomic, assign) NSInteger selectedIndex; @property(nonatomic, copy) RCTBubblingEventBlock onChange; @property(nonatomic, copy) NSArray *testIDS; +@property(nonatomic, copy) NSArray *disabledIndices; @end diff --git a/ios/RNCSegmentedControl.m b/ios/RNCSegmentedControl.m index 4a1bfea4..0ee234a5 100644 --- a/ios/RNCSegmentedControl.m +++ b/ios/RNCSegmentedControl.m @@ -38,6 +38,19 @@ - (void)setValues:(NSArray *)values { } } super.selectedSegmentIndex = _selectedIndex; + [self applyDisabledIndices]; +} + +- (void)setDisabledIndices:(NSArray *)disabledIndices { + _disabledIndices = [disabledIndices copy]; + [self applyDisabledIndices]; +} + +- (void)applyDisabledIndices { + for (NSUInteger index = 0; index < self.numberOfSegments; index++) { + BOOL disabled = [_disabledIndices containsObject:@(index)]; + [self setEnabled:!disabled forSegmentAtIndex:index]; + } } - (void)setSelectedIndex:(NSInteger)selectedIndex { diff --git a/ios/RNCSegmentedControlManager.m b/ios/RNCSegmentedControlManager.m index 553bc1f3..bc899cae 100644 --- a/ios/RNCSegmentedControlManager.m +++ b/ios/RNCSegmentedControlManager.m @@ -28,6 +28,7 @@ - (UIView *)view { RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock) RCT_EXPORT_VIEW_PROPERTY(appearance, NSString) RCT_EXPORT_VIEW_PROPERTY(testIDS, NSArray) +RCT_EXPORT_VIEW_PROPERTY(disabledIndices, NSArray) RCT_CUSTOM_VIEW_PROPERTY(fontStyle, NSObject, RNCSegmentedControl) { #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ diff --git a/js/SegmentedControl.js b/js/SegmentedControl.js index 1c1c95aa..85ee1659 100644 --- a/js/SegmentedControl.js +++ b/js/SegmentedControl.js @@ -28,6 +28,7 @@ const SegmentedControl = ({ onChange, onValueChange, enabled = true, + disabledIndices, selectedIndex, values, tintColor, @@ -132,9 +133,11 @@ const SegmentedControl = ({ {values && values.map((value, index) => { + const segmentDisabled = !!disabledIndices?.includes(index); return ( index ? testIDS[index] : `${index}` } diff --git a/js/SegmentedControlTab.js b/js/SegmentedControlTab.js index 31f18fa3..e5adac86 100644 --- a/js/SegmentedControlTab.js +++ b/js/SegmentedControlTab.js @@ -24,6 +24,7 @@ type Props = $ReadOnly<{| onSelect: () => void, selected: boolean, enabled: boolean, + dimmed?: boolean, fontStyle?: FontStyle, activeFontStyle?: FontStyle, tabStyle?: ViewStyle, @@ -42,6 +43,7 @@ export const SegmentedControlTab = ({ onSelect, value, enabled, + dimmed = false, selected, tintColor, fontStyle = {}, @@ -90,7 +92,7 @@ export const SegmentedControlTab = ({ return ( { }).not.toThrow(); }); }); + +describe('Android and web disabled segments', () => { + let tree; + + afterEach(() => { + act(() => tree?.unmount()); + }); + + function render(props) { + const onChange = jest.fn(); + act(() => { + tree = renderer.create( + , + ); + }); + return { + onChange, + tabs: tree.root.findAllByType(TouchableOpacity), + }; + } + + it('only disables the segments listed in disabledIndices', () => { + const {tabs} = render({disabledIndices: [1]}); + expect(tabs.map((tab) => tab.props.disabled)).toEqual([false, true, false]); + expect(tabs.map((tab) => tab.props.accessibilityState.disabled)).toEqual([ + false, + true, + false, + ]); + }); + + it('keeps the other segments interactive', () => { + const {tabs, onChange} = render({disabledIndices: [1]}); + act(() => tabs[2].props.onPress()); + expect(onChange).toHaveBeenCalledWith({ + nativeEvent: {value: 'Three', selectedSegmentIndex: 2}, + }); + }); + + it('keeps every segment disabled when the whole control is disabled', () => { + const {tabs} = render({enabled: false, disabledIndices: [1]}); + expect(tabs.map((tab) => tab.props.disabled)).toEqual([true, true, true]); + }); + + it('ignores out-of-range indexes and leaves all segments enabled by default', () => { + expect( + render({disabledIndices: [7]}).tabs.map((tab) => tab.props.disabled), + ).toEqual([false, false, false]); + }); +}); diff --git a/js/types.js b/js/types.js index 93426055..7c6a3b0e 100644 --- a/js/types.js +++ b/js/types.js @@ -71,6 +71,11 @@ export type SegmentedControlProps = $ReadOnly<{| * Default value is true. */ enabled?: boolean, + /** + * Indexes of the segments (in `props.values`) the user can't interact with. + * The other segments remain interactive. + */ + disabledIndices?: $ReadOnlyArray, /** * Accent color of the control. */ From d27d3d6ac566bd6bc77fec0aae3228f748618199 Mon Sep 17 00:00:00 2001 From: Jesse Katsumata Date: Mon, 23 Nov 2026 12:49:18 +0900 Subject: [PATCH 2/2] fix: use ReadonlyArray for disabledIndices $ReadOnlyArray is flagged as a deprecated utility type by the Flow version the repository now uses (0.331, since #939). Co-Authored-By: Claude Opus 5.5 --- js/types.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/types.js b/js/types.js index 5251b335..898dfe1c 100644 --- a/js/types.js +++ b/js/types.js @@ -78,7 +78,7 @@ export type SegmentedControlProps = Readonly<{| * Indexes of the segments (in `props.values`) the user can't interact with. * The other segments remain interactive. */ - disabledIndices?: $ReadOnlyArray, + disabledIndices?: ReadonlyArray, /** * Accent color of the control. */