Skip to content

Commit

Permalink
feat(android): add custom label for positive and negative button (#609)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Jun 28, 2022
1 parent dd51c4f commit b6bfd45
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ React Native date & time picker component for iOS, Android and Windows.
- [`themeVariant` (`optional`, `iOS only`)](#themevariant-optional-ios-only)
- [`locale` (`optional`, `iOS only`)](#locale-optional-ios-only)
- [`is24Hour` (`optional`, `Windows and Android only`)](#is24hour-optional-windows-and-android-only)
- [`positiveButtonLabel` (`optional`, `Android only`)](#positiveButtonLabel-optional-android-only)
- [`negativeButtonLabel` (`optional`, `Android only`)](#negativeButtonLabel-optional-android-only)
- [`neutralButtonLabel` (`optional`, `Android only`)](#neutralbuttonlabel-optional-android-only)
- [`minuteInterval` (`optional`)](#minuteinterval-optional)
- [`style` (`optional`, `iOS only`)](#style-optional-ios-only)
Expand Down Expand Up @@ -427,6 +429,22 @@ Allows changing of the time picker to a 24 hour format. By default, this value i
<RNDateTimePicker is24Hour={true} />
```

#### `positiveButtonLabel` (`optional`, `Android only`)

Changes the label of the positive button.

```js
<RNDateTimePicker positiveButtonLabel="OK!" />
```

#### `negativeButtonLabel` (`optional`, `Android only`)

Changes the label of the negative button.

```js
<RNDateTimePicker positiveButtonLabel="Negative" />
```

#### `neutralButtonLabel` (`optional`, `Android only`)

Allows displaying neutral button on picker dialog.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public final class RNConstants {
public static final String ARG_IS24HOUR = "is24Hour";
public static final String ARG_DISPLAY = "display";
public static final String ARG_NEUTRAL_BUTTON_LABEL = "neutralButtonLabel";
public static final String ARG_POSITIVE_BUTTON_LABEL = "positiveButtonLabel";
public static final String ARG_NEGATIVE_BUTTON_LABEL = "negativeButtonLabel";
public static final String ARG_TZOFFSET_MINS = "timeZoneOffsetInMinutes";
public static final String ACTION_DATE_SET = "dateSetAction";
public static final String ACTION_TIME_SET = "timeSetAction";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ static DatePickerDialog createDialog(
if (args != null && args.containsKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, args.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL), mOnNeutralButtonActionListener);
}
if (args != null && args.containsKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_POSITIVE, args.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL), dialog);
}
if (args != null && args.containsKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, args.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL), dialog);
}

final DatePicker datePicker = dialog.getDatePicker();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ private Bundle createFragmentArguments(ReadableMap options) {
if (options.hasKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL) && !options.isNull(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
args.putString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL, options.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL));
}
if (options.hasKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL) && !options.isNull(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
args.putString(RNConstants.ARG_POSITIVE_BUTTON_LABEL, options.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL));
}
if (options.hasKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL) && !options.isNull(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
args.putString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL, options.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL));
}
if (options.hasKey(RNConstants.ARG_TZOFFSET_MINS) && !options.isNull(RNConstants.ARG_TZOFFSET_MINS)) {
args.putLong(RNConstants.ARG_TZOFFSET_MINS, (long) options.getDouble(RNConstants.ARG_TZOFFSET_MINS));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ static TimePickerDialog createDialog(
if (args != null && args.containsKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, args.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL), mOnNeutralButtonActionListener);
}
if (args != null && args.containsKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_POSITIVE, args.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL), dialog);
}
if (args != null && args.containsKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, args.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL), dialog);
}
return dialog;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ private Bundle createFragmentArguments(ReadableMap options) {
if (options.hasKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL) && !options.isNull(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
args.putString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL, options.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL));
}
if (options.hasKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL) && !options.isNull(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
args.putString(RNConstants.ARG_POSITIVE_BUTTON_LABEL, options.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL));
}
if (options.hasKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL) && !options.isNull(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
args.putString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL, options.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL));
}
if (options.hasKey(RNConstants.ARG_INTERVAL) && !options.isNull(RNConstants.ARG_INTERVAL)) {
args.putInt(RNConstants.ARG_INTERVAL, options.getInt(RNConstants.ARG_INTERVAL));
}
Expand Down
6 changes: 5 additions & 1 deletion src/DateTimePickerAndroid.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ function open(props: AndroidNativeProps) {
is24Hour,
minimumDate,
maximumDate,
positiveButtonLabel,
neutralButtonLabel,
negativeButtonLabel,
minuteInterval,
timeZoneOffsetInMinutes,
onChange,
Expand All @@ -47,9 +49,11 @@ function open(props: AndroidNativeProps) {
is24Hour,
minimumDate,
maximumDate,
neutralButtonLabel,
minuteInterval,
timeZoneOffsetInMinutes,
positiveButtonLabel,
neutralButtonLabel,
negativeButtonLabel,
});

const presentPicker = async () => {
Expand Down
8 changes: 8 additions & 0 deletions src/androidUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type Params = {
neutralButtonLabel: AndroidNativeProps['neutralButtonLabel'],
minuteInterval: AndroidNativeProps['minuteInterval'],
timeZoneOffsetInMinutes: AndroidNativeProps['timeZoneOffsetInMinutes'],
positiveButtonLabel: AndroidNativeProps['positiveButtonLabel'],
negativeButtonLabel: AndroidNativeProps['negativeButtonLabel'],
};
export function getOpenPicker({
mode,
Expand All @@ -32,6 +34,8 @@ export function getOpenPicker({
neutralButtonLabel,
minuteInterval,
timeZoneOffsetInMinutes,
positiveButtonLabel,
negativeButtonLabel,
}: Params): PresentPickerCallback {
switch (mode) {
case ANDROID_MODE.time:
Expand All @@ -44,6 +48,8 @@ export function getOpenPicker({
is24Hour,
neutralButtonLabel,
timeZoneOffsetInMinutes,
positiveButtonLabel,
negativeButtonLabel,
});
default:
return () =>
Expand All @@ -55,6 +61,8 @@ export function getOpenPicker({
maximumDate,
neutralButtonLabel,
timeZoneOffsetInMinutes,
positiveButtonLabel,
negativeButtonLabel,
});
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/datetimepicker.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export default function RNDateTimePicker(props: AndroidNativeProps): null {
neutralButtonLabel,
minuteInterval,
timeZoneOffsetInMinutes,
positiveButtonLabel,
negativeButtonLabel,
onError,
} = props;
invariant(value, 'A date or time must be specified as `value` prop.');
Expand All @@ -46,6 +48,8 @@ export default function RNDateTimePicker(props: AndroidNativeProps): null {
neutralButtonLabel,
minuteInterval,
timeZoneOffsetInMinutes,
positiveButtonLabel,
negativeButtonLabel,
onError,
onChange,
};
Expand Down
3 changes: 2 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ export type AndroidNativeProps = Readonly<
*/
minuteInterval?: MinuteInterval;

positiveButtonLabel?: string;
neutralButtonLabel?: string;

negativeButtonLabel?: string;
/**
* callback when an error occurs inside the date picker native code (such as null activity)
*/
Expand Down
2 changes: 2 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ export type AndroidNativeProps = $ReadOnly<{|
minuteInterval?: MinuteInterval,

neutralButtonLabel?: string,
positiveButtonLabel?: string,
negativeButtonLabel?: string,
onError?: (Error) => void,
|}>;

Expand Down

0 comments on commit b6bfd45

Please sign in to comment.