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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions ios/RNCSegmentedControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
@property(nonatomic, assign) NSInteger selectedIndex;
@property(nonatomic, copy) RCTBubblingEventBlock onChange;
@property(nonatomic, copy) NSArray *testIDS;
@property(nonatomic, copy) NSArray<NSNumber *> *disabledIndices;

@end
13 changes: 13 additions & 0 deletions ios/RNCSegmentedControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ - (void)setValues:(NSArray *)values {
}
}
super.selectedSegmentIndex = _selectedIndex;
[self applyDisabledIndices];
}

- (void)setDisabledIndices:(NSArray<NSNumber *> *)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 {
Expand Down
1 change: 1 addition & 0 deletions ios/RNCSegmentedControlManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) && \
Expand Down
5 changes: 4 additions & 1 deletion js/SegmentedControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
onChange,
onValueChange,
enabled = true,
disabledIndices,
selectedIndex,
values,
tintColor,
Expand Down Expand Up @@ -120,7 +121,7 @@
<Animated.View
style={[
styles.slider,
{

Check warning on line 124 in js/SegmentedControl.js

View workflow job for this annotation

GitHub Actions / lint

Inline style: { zIndex: -1 }
transform: [{translateX: animation}],
width: segmentWidth - 4,
zIndex: -1,
Expand All @@ -134,9 +135,11 @@
<View style={styles.segmentsContainer}>
{values &&
values.map((value, index) => {
const segmentDisabled = !!disabledIndices?.includes(index);
return (
<SegmentedControlTab
enabled={enabled}
enabled={enabled && !segmentDisabled}
dimmed={segmentDisabled}
testID={
(testIDS?.length ?? 0) > index ? testIDS[index] : `${index}`
}
Expand Down
5 changes: 4 additions & 1 deletion js/SegmentedControlTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Props = Readonly<{|
onSelect: () => void,
selected: boolean,
enabled: boolean,
dimmed?: boolean,
fontStyle?: FontStyle,
activeFontStyle?: FontStyle,
tabStyle?: ViewStyle,
Expand All @@ -43,6 +44,7 @@ export const SegmentedControlTab = ({
onSelect,
value,
enabled,
dimmed = false,
selected,
tintColor,
fontStyle = {},
Expand Down Expand Up @@ -91,7 +93,7 @@ export const SegmentedControlTab = ({

return (
<TouchableOpacity
style={[styles.container, tabStyle]}
style={[styles.container, tabStyle, dimmed && styles.dimmed]}
disabled={!enabled}
onPress={onSelect}
accessibilityHint={accessibilityHint}
Expand All @@ -118,6 +120,7 @@ export const SegmentedControlTab = ({

const styles = StyleSheet.create({
container: {flex: 1, borderRadius: 5},
dimmed: {opacity: 0.4},
default: {
flex: 1,
justifyContent: 'center',
Expand Down
55 changes: 55 additions & 0 deletions js/__tests__/SegmentedControl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,58 @@ describe('Android and web selection callbacks', () => {
}).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(
<SegmentedControl
values={['One', 'Two', 'Three']}
selectedIndex={0}
onChange={onChange}
{...props}
/>,
);
});
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]);
});
});
5 changes: 5 additions & 0 deletions js/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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<number>,
/**
* Accent color of the control.
*/
Expand Down
Loading