Skip to content

Commit b6bfd45

Browse files
IsaacAnghengkiat-lololvonovak
authored
feat(android): add custom label for positive and negative button (#609)
* Android: add custom label for positive and negative button (positiveButtonLabel, negativeButtonLabel) * add docs * add forgotten file * feat: add custom label for positive and negative button * update TS typings * Update index.d.ts Co-authored-by: Ang Heng Kiat <[email protected]> Co-authored-by: Vojtech Novak <[email protected]>
1 parent dd51c4f commit b6bfd45

11 files changed

+65
-2
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ React Native date & time picker component for iOS, Android and Windows.
7676
- [`themeVariant` (`optional`, `iOS only`)](#themevariant-optional-ios-only)
7777
- [`locale` (`optional`, `iOS only`)](#locale-optional-ios-only)
7878
- [`is24Hour` (`optional`, `Windows and Android only`)](#is24hour-optional-windows-and-android-only)
79+
- [`positiveButtonLabel` (`optional`, `Android only`)](#positiveButtonLabel-optional-android-only)
80+
- [`negativeButtonLabel` (`optional`, `Android only`)](#negativeButtonLabel-optional-android-only)
7981
- [`neutralButtonLabel` (`optional`, `Android only`)](#neutralbuttonlabel-optional-android-only)
8082
- [`minuteInterval` (`optional`)](#minuteinterval-optional)
8183
- [`style` (`optional`, `iOS only`)](#style-optional-ios-only)
@@ -427,6 +429,22 @@ Allows changing of the time picker to a 24 hour format. By default, this value i
427429
<RNDateTimePicker is24Hour={true} />
428430
```
429431

432+
#### `positiveButtonLabel` (`optional`, `Android only`)
433+
434+
Changes the label of the positive button.
435+
436+
```js
437+
<RNDateTimePicker positiveButtonLabel="OK!" />
438+
```
439+
440+
#### `negativeButtonLabel` (`optional`, `Android only`)
441+
442+
Changes the label of the negative button.
443+
444+
```js
445+
<RNDateTimePicker positiveButtonLabel="Negative" />
446+
```
447+
430448
#### `neutralButtonLabel` (`optional`, `Android only`)
431449

432450
Allows displaying neutral button on picker dialog.

android/src/main/java/com/reactcommunity/rndatetimepicker/RNConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public final class RNConstants {
99
public static final String ARG_IS24HOUR = "is24Hour";
1010
public static final String ARG_DISPLAY = "display";
1111
public static final String ARG_NEUTRAL_BUTTON_LABEL = "neutralButtonLabel";
12+
public static final String ARG_POSITIVE_BUTTON_LABEL = "positiveButtonLabel";
13+
public static final String ARG_NEGATIVE_BUTTON_LABEL = "negativeButtonLabel";
1214
public static final String ARG_TZOFFSET_MINS = "timeZoneOffsetInMinutes";
1315
public static final String ACTION_DATE_SET = "dateSetAction";
1416
public static final String ACTION_TIME_SET = "timeSetAction";

android/src/main/java/com/reactcommunity/rndatetimepicker/RNDatePickerDialogFragment.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ static DatePickerDialog createDialog(
106106
if (args != null && args.containsKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
107107
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, args.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL), mOnNeutralButtonActionListener);
108108
}
109+
if (args != null && args.containsKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
110+
dialog.setButton(DialogInterface.BUTTON_POSITIVE, args.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL), dialog);
111+
}
112+
if (args != null && args.containsKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
113+
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, args.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL), dialog);
114+
}
109115

110116
final DatePicker datePicker = dialog.getDatePicker();
111117

android/src/main/java/com/reactcommunity/rndatetimepicker/RNDatePickerDialogModule.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ private Bundle createFragmentArguments(ReadableMap options) {
171171
if (options.hasKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL) && !options.isNull(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
172172
args.putString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL, options.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL));
173173
}
174+
if (options.hasKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL) && !options.isNull(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
175+
args.putString(RNConstants.ARG_POSITIVE_BUTTON_LABEL, options.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL));
176+
}
177+
if (options.hasKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL) && !options.isNull(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
178+
args.putString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL, options.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL));
179+
}
174180
if (options.hasKey(RNConstants.ARG_TZOFFSET_MINS) && !options.isNull(RNConstants.ARG_TZOFFSET_MINS)) {
175181
args.putLong(RNConstants.ARG_TZOFFSET_MINS, (long) options.getDouble(RNConstants.ARG_TZOFFSET_MINS));
176182
}

android/src/main/java/com/reactcommunity/rndatetimepicker/RNTimePickerDialogFragment.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ static TimePickerDialog createDialog(
106106
if (args != null && args.containsKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
107107
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, args.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL), mOnNeutralButtonActionListener);
108108
}
109+
if (args != null && args.containsKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
110+
dialog.setButton(DialogInterface.BUTTON_POSITIVE, args.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL), dialog);
111+
}
112+
if (args != null && args.containsKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
113+
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, args.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL), dialog);
114+
}
109115
return dialog;
110116
}
111117

android/src/main/java/com/reactcommunity/rndatetimepicker/RNTimePickerDialogModule.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ private Bundle createFragmentArguments(ReadableMap options) {
145145
if (options.hasKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL) && !options.isNull(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
146146
args.putString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL, options.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL));
147147
}
148+
if (options.hasKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL) && !options.isNull(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
149+
args.putString(RNConstants.ARG_POSITIVE_BUTTON_LABEL, options.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL));
150+
}
151+
if (options.hasKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL) && !options.isNull(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
152+
args.putString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL, options.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL));
153+
}
148154
if (options.hasKey(RNConstants.ARG_INTERVAL) && !options.isNull(RNConstants.ARG_INTERVAL)) {
149155
args.putInt(RNConstants.ARG_INTERVAL, options.getInt(RNConstants.ARG_INTERVAL));
150156
}

src/DateTimePickerAndroid.android.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ function open(props: AndroidNativeProps) {
3030
is24Hour,
3131
minimumDate,
3232
maximumDate,
33+
positiveButtonLabel,
3334
neutralButtonLabel,
35+
negativeButtonLabel,
3436
minuteInterval,
3537
timeZoneOffsetInMinutes,
3638
onChange,
@@ -47,9 +49,11 @@ function open(props: AndroidNativeProps) {
4749
is24Hour,
4850
minimumDate,
4951
maximumDate,
50-
neutralButtonLabel,
5152
minuteInterval,
5253
timeZoneOffsetInMinutes,
54+
positiveButtonLabel,
55+
neutralButtonLabel,
56+
negativeButtonLabel,
5357
});
5458

5559
const presentPicker = async () => {

src/androidUtils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type Params = {
2121
neutralButtonLabel: AndroidNativeProps['neutralButtonLabel'],
2222
minuteInterval: AndroidNativeProps['minuteInterval'],
2323
timeZoneOffsetInMinutes: AndroidNativeProps['timeZoneOffsetInMinutes'],
24+
positiveButtonLabel: AndroidNativeProps['positiveButtonLabel'],
25+
negativeButtonLabel: AndroidNativeProps['negativeButtonLabel'],
2426
};
2527
export function getOpenPicker({
2628
mode,
@@ -32,6 +34,8 @@ export function getOpenPicker({
3234
neutralButtonLabel,
3335
minuteInterval,
3436
timeZoneOffsetInMinutes,
37+
positiveButtonLabel,
38+
negativeButtonLabel,
3539
}: Params): PresentPickerCallback {
3640
switch (mode) {
3741
case ANDROID_MODE.time:
@@ -44,6 +48,8 @@ export function getOpenPicker({
4448
is24Hour,
4549
neutralButtonLabel,
4650
timeZoneOffsetInMinutes,
51+
positiveButtonLabel,
52+
negativeButtonLabel,
4753
});
4854
default:
4955
return () =>
@@ -55,6 +61,8 @@ export function getOpenPicker({
5561
maximumDate,
5662
neutralButtonLabel,
5763
timeZoneOffsetInMinutes,
64+
positiveButtonLabel,
65+
negativeButtonLabel,
5866
});
5967
}
6068
}

src/datetimepicker.android.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export default function RNDateTimePicker(props: AndroidNativeProps): null {
2323
neutralButtonLabel,
2424
minuteInterval,
2525
timeZoneOffsetInMinutes,
26+
positiveButtonLabel,
27+
negativeButtonLabel,
2628
onError,
2729
} = props;
2830
invariant(value, 'A date or time must be specified as `value` prop.');
@@ -46,6 +48,8 @@ export default function RNDateTimePicker(props: AndroidNativeProps): null {
4648
neutralButtonLabel,
4749
minuteInterval,
4850
timeZoneOffsetInMinutes,
51+
positiveButtonLabel,
52+
negativeButtonLabel,
4953
onError,
5054
onChange,
5155
};

src/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@ export type AndroidNativeProps = Readonly<
142142
*/
143143
minuteInterval?: MinuteInterval;
144144

145+
positiveButtonLabel?: string;
145146
neutralButtonLabel?: string;
146-
147+
negativeButtonLabel?: string;
147148
/**
148149
* callback when an error occurs inside the date picker native code (such as null activity)
149150
*/

src/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ export type AndroidNativeProps = $ReadOnly<{|
172172
minuteInterval?: MinuteInterval,
173173

174174
neutralButtonLabel?: string,
175+
positiveButtonLabel?: string,
176+
negativeButtonLabel?: string,
175177
onError?: (Error) => void,
176178
|}>;
177179

0 commit comments

Comments
 (0)