Skip to content

Commit 8ccb07a

Browse files
feat: add disabled prop support for iOS (#412)
* add disabled support for iOS * type updates and readme * update tests and default for disabled * Update README.md Co-authored-by: Vojtech Novak <[email protected]> Co-authored-by: Vojtech Novak <[email protected]>
1 parent c839877 commit 8ccb07a

File tree

7 files changed

+40
-0
lines changed

7 files changed

+40
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ React Native date & time picker component for iOS, Android and Windows.
6363
- [`neutralButtonLabel` (`optional`, `Android only`)](#neutralbuttonlabel-optional-android-only)
6464
- [`minuteInterval` (`optional`)](#minuteinterval-optional)
6565
- [`style` (`optional`, `iOS only`)](#style-optional-ios-only)
66+
- [`disabled` (`optional`, `iOS only`)](#disabled-optional-ios-only)
6667
- [Migration from the older components](#migration-from-the-older-components)
6768
- [DatePickerIOS](#datepickerios)
6869
- [DatePickerAndroid](#datepickerandroid)
@@ -360,6 +361,10 @@ This means that eg. if the device has dark mode turned on, and your screen backg
360361
<RNDateTimePicker style={{flex: 1}} />
361362
```
362363

364+
#### `disabled` (`optional`, `iOS only`)
365+
366+
If true, the user won't be able to interact with the view.
367+
363368
## Migration from the older components
364369

365370
`RNDateTimePicker` is the new common name used to represent the old versions of iOS and Android.

example/App.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Platform,
1010
TextInput,
1111
useColorScheme,
12+
Switch,
1213
} from 'react-native';
1314
import DateTimePicker from '@react-native-community/datetimepicker';
1415
import SegmentedControl from '@react-native-community/segmented-control';
@@ -67,6 +68,7 @@ export const App = () => {
6768
const [display, setDisplay] = useState(DISPLAY_VALUES[0]);
6869
const [interval, setMinInterval] = useState(1);
6970
const [neutralButtonLabel, setNeutralButtonLabel] = useState(undefined);
71+
const [disabled, setDisabled] = useState(false);
7072

7173
// Windows-specific
7274
const [time, setTime] = useState(undefined);
@@ -169,6 +171,12 @@ export const App = () => {
169171
placeholder="color"
170172
/>
171173
</View>
174+
<View style={styles.header}>
175+
<ThemedText style={{margin: 10, flex: 1}}>
176+
disabled (iOS only)
177+
</ThemedText>
178+
<Switch value={disabled} onValueChange={setDisabled} />
179+
</View>
172180
<View style={styles.header}>
173181
<ThemedText style={{margin: 10, flex: 1}}>
174182
neutralButtonLabel (android only)
@@ -257,6 +265,7 @@ export const App = () => {
257265
style={styles.iOsPicker}
258266
textColor={color || undefined}
259267
neutralButtonLabel={neutralButtonLabel}
268+
disabled={disabled}
260269
/>
261270
)}
262271
</View>

ios/RNDateTimePickerManager.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,13 @@ + (NSString*) datepickerStyleToString: (UIDatePickerStyle) style {
118118
}
119119
}
120120

121+
RCT_CUSTOM_VIEW_PROPERTY(disabled, BOOL, RNDateTimePicker)
122+
{
123+
if (json) {
124+
view.enabled = !([RCTConvert BOOL:json]);
125+
} else {
126+
view.enabled = defaultView.enabled;
127+
}
128+
}
129+
121130
@end

src/datetimepicker.ios.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export default function Picker({
5353
timeZoneOffsetInMinutes,
5454
textColor,
5555
onChange,
56+
disabled = false,
5657
...otherProps
5758
}: IOSNativeProps) {
5859
const [heightStyle, setHeightStyle] = useState(undefined);
@@ -123,6 +124,7 @@ export default function Picker({
123124
onStartShouldSetResponder={() => true}
124125
onResponderTerminationRequest={() => false}
125126
displayIOS={display}
127+
disabled={disabled === true}
126128
/>
127129
);
128130
}

src/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ export type IOSNativeProps = Readonly<
100100
* Sets the preferredDatePickerStyle for picker
101101
*/
102102
display?: IOSDisplay;
103+
104+
/**
105+
* Is this picker disabled?
106+
*/
107+
disabled?: boolean;
103108
}
104109
>;
105110

src/types.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ export type IOSNativeProps = $ReadOnly<{|
119119
* Sets the preferredDatePickerStyle for picker
120120
*/
121121
display?: IOSDisplay,
122+
123+
/**
124+
* Is this picker disabled?
125+
*/
126+
disabled?: boolean,
122127
|}>;
123128

124129
export type AndroidNativeProps = $ReadOnly<{|

test/__snapshots__/index.test.js.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
exports[`DatePicker applies styling to DatePicker 1`] = `
44
<RNDateTimePicker
55
date={1376949600000}
6+
disabled={false}
67
displayIOS="default"
78
mode="date"
89
onChange={[Function]}
@@ -24,6 +25,7 @@ exports[`DatePicker applies styling to DatePicker 1`] = `
2425
exports[`DatePicker renders a native Component 1`] = `
2526
<RNDateTimePicker
2627
date={1376949600000}
28+
disabled={false}
2729
displayIOS="default"
2830
mode="date"
2931
onChange={[Function]}
@@ -40,6 +42,7 @@ exports[`DatePicker renders a native Component 1`] = `
4042
exports[`DatePicker renders with mode countdown 1`] = `
4143
<RNDateTimePicker
4244
date={1376949600000}
45+
disabled={false}
4346
displayIOS="default"
4447
mode="countdown"
4548
onChange={[Function]}
@@ -56,6 +59,7 @@ exports[`DatePicker renders with mode countdown 1`] = `
5659
exports[`DatePicker renders with mode datetime 1`] = `
5760
<RNDateTimePicker
5861
date={1376949600000}
62+
disabled={false}
5963
displayIOS="default"
6064
mode="datetime"
6165
onChange={[Function]}
@@ -72,6 +76,7 @@ exports[`DatePicker renders with mode datetime 1`] = `
7276
exports[`DatePicker renders with mode time 1`] = `
7377
<RNDateTimePicker
7478
date={1376949600000}
79+
disabled={false}
7580
displayIOS="default"
7681
mode="time"
7782
onChange={[Function]}

0 commit comments

Comments
 (0)