Skip to content

Commit dcda13c

Browse files
simartnSimon Naglvonovak
authored
feat: add accentColor prop for iOS (#584)
* Add accentColor props for iOs datetimepicker (#20) * Fix rename to accentColor * Fix e2e tests. Scroll to bottom when rendering a date picker. Ensure the date picker is shown. * Fix e2e test. Label Time Picker is used two times in dependency Tree. Find element by type. * Remove code duplications in expample app. * Remove not needed View Tag from example app. * Shorten command with "cd -" Co-authored-by: Vojtech Novak <[email protected]> * Use bigger device for android. * Test: List aviable devices * Use Pixel 4 XL for android e2e Tests * Fix use pixel 4 xl * Revert getInlineTimePickerIOS selection * Revert rename of android.device.debug.binaryPath Co-authored-by: Vojtech Novak <[email protected]> * Fix error in package.json * Fix grammar Co-authored-by: Vojtech Novak <[email protected]> * Fix spelling of iOS Co-authored-by: Vojtech Novak <[email protected]> * Scroll to datepicker only on iOS Co-authored-by: Simon Nagl <[email protected]> Co-authored-by: Vojtech Novak <[email protected]>
1 parent ac4ce89 commit dcda13c

File tree

9 files changed

+83
-15
lines changed

9 files changed

+83
-15
lines changed

.circleci/config.yml

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ jobs:
7676
- android/create-avd:
7777
avd-name: TestingAVD
7878
system-image: system-images;android-29;default;x86
79+
additional-args: --device "pixel_4_xl"
7980
install: true
8081
- android/start-emulator:
8182
avd-name: TestingAVD

CONTRIBUTING.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ For cleaning all the detox builds just run `npm run detox:clean`.
3434
```sh
3535
# Debug requires to run Metro Bundler
3636
yarn start
37+
cd "example/ios" && npx pod-install && cd -
3738
yarn detox:ios:build:debug
3839
yarn detox:ios:test:debug
3940
```

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ React Native date & time picker component for iOS, Android and Windows.
7272
- [`dateFormat` (`optional`, `Windows only`)](#dateFormat-optional-windows-only)
7373
- [`firstDayOfWeek` (`optional`, `Windows only`)](#firstDayOfWeek-optional-windows-only)
7474
- [`textColor` (`optional`, `iOS only`)](#textColor-optional-ios-only)
75+
- [`accentColor` (`optional`, `iOS only`)](#accentColor-optional-ios-only)
7576
- [`themeVariant` (`optional`, `iOS only`)](#themevariant-optional-ios-only)
7677
- [`locale` (`optional`, `iOS only`)](#locale-optional-ios-only)
7778
- [`is24Hour` (`optional`, `Windows and Android only`)](#is24hour-optional-windows-and-android-only)
@@ -389,6 +390,11 @@ Allows changing of the textColor of the date picker. Has effect only when `displ
389390
<RNDateTimePicker textColor="red" />
390391
```
391392

393+
#### `accentColor` (`optional`, `iOS only`)
394+
395+
Allows changing the accentColor (tintColor) of the date picker.
396+
Has no effect when `display` is `"spinner"`.
397+
392398
#### `themeVariant` (`optional`, `iOS only`)
393399

394400
Allows overriding system theme variant (dark or light mode) used by the date picker.

example/App.js

+43-14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import DateTimePicker from '@react-native-community/datetimepicker';
1515
import SegmentedControl from '@react-native-segmented-control/segmented-control';
1616
import {Colors} from 'react-native/Libraries/NewAppScreen';
17-
import React, {useState} from 'react';
17+
import React, {useRef, useState} from 'react';
1818
import {Picker} from 'react-native-windows';
1919
import moment from 'moment';
2020
import {
@@ -43,7 +43,7 @@ const ThemedTextInput = (props) => {
4343

4444
const TextElement = React.createElement(TextInput, props);
4545
return React.cloneElement(TextElement, {
46-
style: [props.style, textColorByMode],
46+
style: [props.style, styles.textInput, textColorByMode],
4747
placeholderTextColor: isDarkMode ? Colors.white : Colors.black,
4848
});
4949
};
@@ -68,7 +68,8 @@ export const App = () => {
6868
const [tzOffsetInMinutes, setTzOffsetInMinutes] = useState(undefined);
6969
const [mode, setMode] = useState(MODE_VALUES[0]);
7070
const [show, setShow] = useState(false);
71-
const [color, setColor] = useState();
71+
const [textColor, setTextColor] = useState();
72+
const [accentColor, setAccentColor] = useState();
7273
const [display, setDisplay] = useState(DISPLAY_VALUES[0]);
7374
const [interval, setMinInterval] = useState(1);
7475
const [neutralButtonLabel, setNeutralButtonLabel] = useState(undefined);
@@ -87,6 +88,8 @@ export const App = () => {
8788
'{dayofweek.abbreviated(2)}',
8889
);
8990

91+
const scrollRef = useRef(null);
92+
9093
const handleResetPress = () => {
9194
setDate(undefined);
9295
};
@@ -131,7 +134,14 @@ export const App = () => {
131134
return (
132135
<SafeAreaView style={[backgroundStyle, {flex: 1}]}>
133136
<StatusBar barStyle="dark-content" />
134-
<ScrollView testID="DateTimePickerScrollView">
137+
<ScrollView
138+
testID="DateTimePickerScrollView"
139+
ref={scrollRef}
140+
onContentSizeChange={() => {
141+
if (Platform.OS === 'ios') {
142+
scrollRef.current?.scrollToEnd({animated: true});
143+
}
144+
}}>
135145
{global.HermesInternal != null && (
136146
<View style={styles.engine}>
137147
<Text testID="hermesIndicator" style={styles.footer}>
@@ -183,38 +193,48 @@ export const App = () => {
183193
}}
184194
/>
185195
<View style={styles.header}>
186-
<ThemedText style={{margin: 10, flex: 1}}>
196+
<ThemedText style={styles.textLabel}>
187197
text color (iOS only)
188198
</ThemedText>
189199
<ThemedTextInput
190-
value={color}
191-
style={{height: 60, flex: 1}}
200+
value={textColor}
192201
onChangeText={(text) => {
193-
setColor(text.toLowerCase());
202+
setTextColor(text.toLowerCase());
194203
}}
195-
placeholder="color"
204+
placeholder="textColor"
196205
/>
197206
</View>
198207
<View style={styles.header}>
199-
<ThemedText style={{margin: 10, flex: 1}}>
208+
<ThemedText style={styles.textLabel}>
209+
accent color (iOS only)
210+
</ThemedText>
211+
<ThemedTextInput
212+
value={accentColor}
213+
onChangeText={(text) => {
214+
setAccentColor(text.toLowerCase());
215+
}}
216+
placeholder="accentColor"
217+
/>
218+
</View>
219+
<View style={styles.header}>
220+
<ThemedText style={styles.textLabel}>
200221
disabled (iOS only)
201222
</ThemedText>
202223
<Switch value={disabled} onValueChange={setDisabled} />
203224
</View>
204225
<View style={styles.header}>
205-
<ThemedText style={{margin: 10, flex: 1}}>
226+
<ThemedText style={styles.textLabel}>
206227
neutralButtonLabel (android only)
207228
</ThemedText>
208229
<ThemedTextInput
209230
value={neutralButtonLabel}
210-
style={{height: 60, flex: 1}}
211231
onChangeText={setNeutralButtonLabel}
212232
placeholder="neutralButtonLabel"
213233
testID="neutralButtonLabelTextInput"
214234
/>
215235
</View>
216236
<View style={styles.header}>
217-
<ThemedText style={{margin: 10, flex: 1}}>
237+
<ThemedText style={styles.textLabel}>
218238
[android] show and dismiss picker after 3 secs
219239
</ThemedText>
220240
</View>
@@ -306,7 +326,8 @@ export const App = () => {
306326
display={display}
307327
onChange={onChange}
308328
style={styles.iOsPicker}
309-
textColor={color || undefined}
329+
textColor={textColor || undefined}
330+
accentColor={accentColor || undefined}
310331
neutralButtonLabel={neutralButtonLabel}
311332
disabled={disabled}
312333
/>
@@ -502,6 +523,14 @@ const styles = StyleSheet.create({
502523
alignItems: 'center',
503524
flexDirection: 'row',
504525
},
526+
textLabel: {
527+
margin: 10,
528+
flex: 1,
529+
},
530+
textInput: {
531+
height: 60,
532+
flex: 1,
533+
},
505534
button: {
506535
alignItems: 'center',
507536
marginBottom: 10,

ios/RNDateTimePickerManager.m

+13
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ + (NSString*) datepickerStyleToString: (UIDatePickerStyle) style API_AVAILABLE(
141141
}
142142
}
143143

144+
RCT_CUSTOM_VIEW_PROPERTY(accentColor, UIColor, RNDateTimePicker)
145+
{
146+
if (json) {
147+
[view setTintColor:[RCTConvert UIColor:json]];
148+
} else {
149+
if (@available(iOS 15.0, *)) {
150+
[view setTintColor:[UIColor tintColor]];
151+
} else {
152+
[view setTintColor:[UIColor systemBlueColor]];
153+
}
154+
}
155+
}
156+
144157
// TODO vonovak setting preferredDatePickerStyle invalidates minuteinterval
145158
RCT_CUSTOM_VIEW_PROPERTY(displayIOS, RNCUIDatePickerStyle, RNDateTimePicker)
146159
{

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
"build": "export RCT_NO_LAUNCH_PACKAGER=true && (cd example/android && ./gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug)",
115115
"type": "android.emulator",
116116
"device": {
117-
"avdName": "Pixel_4_Android_12_api_31"
117+
"avdName": "Pixel_5_Android_12_api_31"
118118
}
119119
},
120120
"android.device.debug": {

src/datetimepicker.ios.js

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export default function Picker({
5252
minuteInterval,
5353
timeZoneOffsetInMinutes,
5454
textColor,
55+
accentColor,
5556
themeVariant,
5657
onChange,
5758
mode = ANDROID_MODE.date,
@@ -126,6 +127,7 @@ export default function Picker({
126127
timeZoneOffsetInMinutes={timeZoneOffsetInMinutes}
127128
onChange={_onChange}
128129
textColor={textColor}
130+
accentColor={accentColor}
129131
themeVariant={themeVariant}
130132
onStartShouldSetResponder={() => true}
131133
onResponderTerminationRequest={() => false}

src/index.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ export type IOSNativeProps = Readonly<
9898
*/
9999
textColor?: string;
100100

101+
/**
102+
* The date picker accent color.
103+
*
104+
* Sets the color of the selected, date and navigation icons.
105+
* Has no effect for display 'spinner'.
106+
*/
107+
accentColor?: string;
108+
101109
/**
102110
* Override theme variant used by iOS native picker
103111
*/

src/types.js

+8
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ export type IOSNativeProps = $ReadOnly<{|
119119
*/
120120
textColor?: ?ColorValue,
121121

122+
/**
123+
* The date picker accent color.
124+
*
125+
* Sets the color of the selected, date and navigation icons.
126+
* Has no effect for display 'spinner'.
127+
*/
128+
accentColor?: ?ColorValue,
129+
122130
/**
123131
* Override theme variant used by iOS native picker
124132
*/

0 commit comments

Comments
 (0)