@octane-xplat/date-picker 0.0.0-stage → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Alec Larson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,34 @@
1
- # Temporary Holding Version
1
+ # `@octane-xplat/date-picker`
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ An install boundary for platform-specific date and time selection controls:
4
+ a SwiftUI `DatePicker` on iOS, Material 3 `DatePicker`/`TimePicker` built with
5
+ Jetpack Compose on Android, a labeled `<input type="date|time|datetime-local">`
6
+ on web, and a real `NSDatePicker` on macOS.
7
+
8
+ Install the package in the app that renders a control. Import
9
+ `SwiftUIDatePicker` from `@octane-xplat/date-picker/ios`,
10
+ `MaterialDatePicker` from `@octane-xplat/date-picker/android`, `DateInput`
11
+ from `@octane-xplat/date-picker/web`, or `AppKitDatePicker` from
12
+ `@octane-xplat/date-picker/macos` in the matching platform-suffixed file.
13
+ Each entry exports its own component and types; there is no shared date-picker
14
+ API at the package root.
15
+
16
+ The platform APIs use their own selection vocabulary: the iOS entry takes a
17
+ controlled `selection`/`defaultSelection` `Date` with `displayedComponents`
18
+ and `pickerStyle`; the Android entry follows the uncontrolled
19
+ `initialDate`/`onDateSelected` model of the Material 3 pickers with
20
+ `selectableDates` range bounds; the web entry follows browser input values;
21
+ the macOS entry takes `components`/`pickerStyle` (`'graphical'` embeds the
22
+ inline calendar) with a controlled `selection` `Date`.
23
+
24
+ Android notes: give `MaterialDatePicker` a bounded height (the M3 calendar's
25
+ lazy grid collapses under unbounded height), and keep `initialDate` and
26
+ `selectableDates` identity-stable — the provider keys its state `remember`
27
+ on them, so a per-render `new Date()` resets the selection.
28
+ See the framework guide for details and maintained examples:
29
+ [`docs/date-picker.md`](../../docs/date-picker.md).
30
+
31
+ The native implementations are adapted from `@expo/ui` (MIT,
32
+ `packages/expo-ui` sdk-57): `ios/DatePickerView.swift` and
33
+ `android/.../ui/DatePickerView.kt`. Attribution headers sit on the ported
34
+ files.
@@ -0,0 +1,46 @@
1
+ import { defineUniversalComponent, registerElement, universalPlan, universalValue } from "@nativescript-community/octane";
2
+ import { ComposeDataDriver, JetpackCompose, registerJetpackCompose } from "@nativescript/jetpack-compose";
3
+ //#region src/MaterialDatePicker.android.tsrx
4
+ var __octaneUniversalPlan0 = universalPlan("nativescript", {
5
+ "kind": "host",
6
+ "type": "xplatandroiddatepicker",
7
+ "props": { "composeId": "xplat-material-datepicker" },
8
+ "bindings": [
9
+ ["id", 0],
10
+ ["className", 1],
11
+ ["style", 2],
12
+ ["accessibilityLabel", 3],
13
+ ["data", 4],
14
+ ["onComposeEvent", 5]
15
+ ]
16
+ });
17
+ registerElement("xplatandroiddatepicker", JetpackCompose);
18
+ registerJetpackCompose("xplat-material-datepicker", (owner) => new ComposeDataDriver(new com.octanexplat.datepicker.XplatDatePickerProvider(), owner));
19
+ var MaterialDatePicker = defineUniversalComponent("nativescript", function MaterialDatePicker(props) {
20
+ const data = {
21
+ initialDate: props.initialDate?.getTime() ?? null,
22
+ variant: props.variant ?? "picker",
23
+ displayedComponents: props.displayedComponents ?? "date",
24
+ showVariantToggle: props.showVariantToggle ?? true ? 1 : 0,
25
+ is24Hour: props.is24Hour ?? true ? 1 : 0,
26
+ color: props.color,
27
+ elementColors: props.elementColors,
28
+ selectableDates: {
29
+ start: props.selectableDates?.start?.getTime() ?? null,
30
+ end: props.selectableDates?.end?.getTime() ?? null
31
+ }
32
+ };
33
+ return universalValue(__octaneUniversalPlan0, [
34
+ props.id,
35
+ props.className,
36
+ props.style,
37
+ props.accessibilityLabel,
38
+ data,
39
+ (event) => {
40
+ const millis = event.data?.date;
41
+ props.onDateSelected?.(typeof millis === "number" ? new Date(millis) : null);
42
+ }
43
+ ]);
44
+ }, { "module": "@nativescript-community/octane" });
45
+ //#endregion
46
+ export { MaterialDatePicker };
@@ -0,0 +1,48 @@
1
+ import { defineUniversalComponent, hookSlots, registerElement, universalPlan, universalValue, useState } from "@nativescript-community/octane";
2
+ import { SwiftUI, UIDataDriver, registerSwiftUI } from "@nativescript/swift-ui";
3
+ //#region src/SwiftUIDatePicker.ios.tsrx
4
+ var _h$0 = /* @__PURE__ */ Symbol(/* @__PURE__ */ hookSlots(1));
5
+ var __octaneUniversalPlan0 = universalPlan("nativescript", {
6
+ "kind": "host",
7
+ "type": "xplatiosdatepicker",
8
+ "props": { "swiftId": "xplat-swiftui-datepicker" },
9
+ "bindings": [
10
+ ["id", 0],
11
+ ["className", 1],
12
+ ["style", 2],
13
+ ["data", 3],
14
+ ["onSwiftUIEvent", 4]
15
+ ]
16
+ });
17
+ registerElement("xplatiosdatepicker", SwiftUI);
18
+ registerSwiftUI("xplat-swiftui-datepicker", (owner) => new UIDataDriver(new XplatDatePickerProvider(), owner));
19
+ var SwiftUIDatePicker = defineUniversalComponent("nativescript", function SwiftUIDatePicker(props) {
20
+ const controlled = props.selection !== void 0;
21
+ const [internalSelection, setInternalSelection] = useState(props.defaultSelection, _h$0);
22
+ const selection = controlled ? props.selection : internalSelection;
23
+ return universalValue(__octaneUniversalPlan0, [
24
+ props.id,
25
+ props.className,
26
+ props.style,
27
+ {
28
+ title: props.title,
29
+ selection: selection?.getTime(),
30
+ range: {
31
+ start: props.minimumDate?.getTime(),
32
+ end: props.maximumDate?.getTime()
33
+ },
34
+ displayedComponents: props.displayedComponents ?? ["date"],
35
+ pickerStyle: props.pickerStyle ?? "automatic",
36
+ disabled: props.disabled ?? false
37
+ },
38
+ (event) => {
39
+ const millis = event.data?.date;
40
+ if (typeof millis !== "number") return;
41
+ const next = new Date(millis);
42
+ if (!controlled) setInternalSelection(next);
43
+ props.onSelectionChange?.(next);
44
+ }
45
+ ]);
46
+ }, { "module": "@nativescript-community/octane" });
47
+ //#endregion
48
+ export { SwiftUIDatePicker };
@@ -0,0 +1,2 @@
1
+ import { MaterialDatePicker } from "../MaterialDatePicker.android.tsrx.js";
2
+ export { MaterialDatePicker };
@@ -0,0 +1,2 @@
1
+ import { SwiftUIDatePicker } from "../SwiftUIDatePicker.ios.tsrx.js";
2
+ export { SwiftUIDatePicker };
@@ -0,0 +1,8 @@
1
+ {
2
+ "version": 1,
3
+ "buildId": "1fb9fdab-32f1-4d82-ad87-ffe708c70839",
4
+ "mode": "production",
5
+ "capabilities": {
6
+ "independentHydration": false
7
+ }
8
+ }
@@ -0,0 +1,57 @@
1
+ import { __s, bag16, child, clone, delegateEvents, hookSlots, setAriaAttribute, setBooleanAttribute, setEventHandler, setStyle, sibling, template, useState } from "octane";
2
+ import { bindSignalAttribute, bindSignalChild, bindSignalValue, enableSignalBindings, setPlainAttribute } from "octane/internal/client";
3
+ //#region src/DateInput.web.tsrx
4
+ enableSignalBindings(1, true);
5
+ delegateEvents(["change"]);
6
+ var _t$0 = /* @__PURE__ */ template("<label><span></span><input data-octane-input=\"i:ca76d877\"/></label>");
7
+ var _hs$ = /* @__PURE__ */ hookSlots(1);
8
+ var DateInput = /* @__PURE__ */ __s(function DateInput(props, __s, __extra) {
9
+ const controlled = props.value !== void 0;
10
+ const [internalValue, setInternalValue] = useState(props.defaultValue ?? "", _hs$ + 0);
11
+ const value = controlled ? props.value : internalValue;
12
+ const labelId = props.id ? `${props.id}-label` : void 0;
13
+ if (__s.block.pending && !__s.block.crossRenderUpdate) return;
14
+ let _b = __s.slots[0];
15
+ if (_b === void 0) {
16
+ let _m1, _m4, _m6, _m10;
17
+ const _root = clone(_t$0);
18
+ _m1 = _root;
19
+ const _el0 = child(_root);
20
+ _m4 = _el0;
21
+ const _el1 = sibling(_el0, 1);
22
+ _m6 = _el1;
23
+ _m10 = bindSignalValue(__s, void 0, _el1, value, "i:ca76d877");
24
+ setEventHandler(_el1, "$$change", (event) => {
25
+ const nextValue = event.target.value;
26
+ if (!controlled) setInternalValue(nextValue);
27
+ props.onChange?.(nextValue);
28
+ });
29
+ _b = bag16(__s, _root, void 0, _m1, void 0, void 0, _m4, void 0, _m6, void 0, void 0, void 0, _m10, void 0, void 0, void 0, null, void 0);
30
+ } else {
31
+ _b.k = bindSignalValue(__s, _b.k, _b.g, value, "i:ca76d877");
32
+ setEventHandler(_b.g, "$$change", (event) => {
33
+ const nextValue = event.target.value;
34
+ if (!controlled) setInternalValue(nextValue);
35
+ props.onChange?.(nextValue);
36
+ });
37
+ }
38
+ _b.a = bindSignalAttribute(__s, _b.a, _b.b, "class", props.className, "b:cabeae14", "class");
39
+ {
40
+ const _v = props.style;
41
+ if (_b.c !== _v) {
42
+ setStyle(_b.b, _v, _b.c);
43
+ _b.c = _v;
44
+ }
45
+ }
46
+ _b.d = bindSignalAttribute(__s, _b.d, _b.e, "id", labelId, "b:6dc088f1", setPlainAttribute);
47
+ _b.f = bindSignalAttribute(__s, _b.f, _b.g, "id", props.id, "b:f74893e7", setPlainAttribute);
48
+ _b.h = bindSignalAttribute(__s, _b.h, _b.g, "type", props.type ?? "date", "b:47e366ba", setPlainAttribute);
49
+ _b.i = bindSignalAttribute(__s, _b.i, _b.g, "aria-label", props.accessibilityLabel ?? props.label, "b:a953fab4", setAriaAttribute);
50
+ _b.j = bindSignalAttribute(__s, _b.j, _b.g, "aria-labelledby", labelId, "b:10bc1230", setAriaAttribute);
51
+ _b.l = bindSignalAttribute(__s, _b.l, _b.g, "min", props.min, "b:4fe4c70f", setPlainAttribute);
52
+ _b.m = bindSignalAttribute(__s, _b.m, _b.g, "max", props.max, "b:4c98be11", setPlainAttribute);
53
+ _b.n = bindSignalAttribute(__s, _b.n, _b.g, "disabled", props.disabled, "b:701d5b16", setBooleanAttribute);
54
+ _b.q = bindSignalChild(__s, _b.q, 1, _b.e, props.label, "b:25c6cb91", null, void 0, void 0, void 0, true);
55
+ });
56
+ //#endregion
57
+ export { DateInput };
@@ -0,0 +1,8 @@
1
+ {
2
+ "version": 1,
3
+ "buildId": "f258a4ce-175f-4808-aeed-e9a10c1bcf3e",
4
+ "mode": "production",
5
+ "capabilities": {
6
+ "independentHydration": false
7
+ }
8
+ }
@@ -0,0 +1,2 @@
1
+ import { DateInput } from "../DateInput.web.tsrx.js";
2
+ export { DateInput };
package/package.json CHANGED
@@ -1,6 +1,74 @@
1
1
  {
2
2
  "name": "@octane-xplat/date-picker",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.9.0",
4
+ "description": "Platform-specific date and time selection controls for Octane xplat",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/octane-xplat/octane-xplat",
8
+ "directory": "packages/date-picker"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "types",
13
+ "src",
14
+ "platforms"
15
+ ],
16
+ "type": "module",
17
+ "exports": {
18
+ "./ios": {
19
+ "types": "./types/ios/index.d.ts",
20
+ "native": "./dist/native/ios/index.js"
21
+ },
22
+ "./android": {
23
+ "types": "./types/android/index.d.ts",
24
+ "native": "./dist/native/android/index.js"
25
+ },
26
+ "./web": {
27
+ "types": "./types/web/index.d.ts",
28
+ "web": "./dist/web/web/index.js"
29
+ }
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "dependencies": {
35
+ "@nativescript/jetpack-compose": "1.0.0-beta.0",
36
+ "@nativescript/swift-ui": "4.0.2"
37
+ },
38
+ "devDependencies": {
39
+ "@nativescript-community/octane": "0.2.4",
40
+ "@nativescript/core": "9.1.2",
41
+ "@octanejs/vite-plugin": "0.1.60",
42
+ "octane": "0.5.0",
43
+ "tsrx-typegen": "0.2.1",
44
+ "typescript": "5.9.3",
45
+ "vite": "8.3.0"
46
+ },
47
+ "peerDependencies": {
48
+ "@nativescript-community/octane": ">=0.2.1 <1",
49
+ "@nativescript/core": ">=9.1.0 <10",
50
+ "octane": ">=0.1.51 <1"
51
+ },
52
+ "peerDependenciesMeta": {
53
+ "@nativescript-community/octane": {
54
+ "optional": true
55
+ },
56
+ "@nativescript/core": {
57
+ "optional": true
58
+ },
59
+ "octane": {
60
+ "optional": true
61
+ }
62
+ },
63
+ "nativescript": {
64
+ "platforms": {
65
+ "android": "9.1.0",
66
+ "ios": "9.1.0"
67
+ }
68
+ },
69
+ "scripts": {
70
+ "build": "vite build && vite build --mode native && tsc -p tsconfig.types.json",
71
+ "test:packed": "node tests/packed-consumer.mjs",
72
+ "pack:check": "tsrx-typegen --pack-check"
73
+ }
6
74
  }
@@ -0,0 +1,15 @@
1
+ apply plugin: 'org.jetbrains.kotlin.plugin.compose'
2
+
3
+ android {
4
+ buildFeatures {
5
+ compose true
6
+ }
7
+ kotlinOptions {
8
+ jvmTarget = '17'
9
+ }
10
+ }
11
+
12
+ dependencies {
13
+ implementation platform('androidx.compose:compose-bom:2024.12.01')
14
+ implementation 'androidx.compose.material3:material3'
15
+ }
@@ -0,0 +1,335 @@
1
+ // Adapted from @expo/ui (MIT) — Copyright 2025-present 650 Industries.
2
+ // Upstream: expo/packages/expo-ui/android/.../ui/DatePickerView.kt (sdk-57).
3
+ // The ExpoModules glue (@OptimizedRecord/@OptimizedComposeProps records,
4
+ // ModifierRegistry, AsyncFunctionHandle) is replaced by the
5
+ // @nativescript/jetpack-compose provider contract: updateData(Map) + onEvent.
6
+ // Colors arrive as CSS hex strings instead of expo ColorValue ints. The
7
+ // dialog wrappers (DatePickerDialog/TimePickerDialog) are not ported; the
8
+ // inline DatePicker/TimePicker composables cover this leaf's contract.
9
+
10
+ package com.octanexplat.datepicker
11
+
12
+ import androidx.compose.material3.DatePicker
13
+ import androidx.compose.material3.DatePickerDefaults
14
+ import androidx.compose.material3.DatePickerState
15
+ import androidx.compose.material3.DisplayMode
16
+ import androidx.compose.material3.ExperimentalMaterial3Api
17
+ import androidx.compose.material3.LocalContentColor
18
+ import androidx.compose.material3.MaterialTheme
19
+ import androidx.compose.material3.SelectableDates
20
+ import androidx.compose.material3.TimePicker
21
+ import androidx.compose.material3.TimePickerDefaults
22
+ import androidx.compose.material3.TimePickerLayoutType
23
+ import androidx.compose.material3.TimePickerState
24
+ import androidx.compose.runtime.Composable
25
+ import androidx.compose.runtime.CompositionLocalProvider
26
+ import androidx.compose.runtime.LaunchedEffect
27
+ import androidx.compose.runtime.getValue
28
+ import androidx.compose.runtime.mutableStateOf
29
+ import androidx.compose.runtime.remember
30
+ import androidx.compose.runtime.setValue
31
+ import androidx.compose.ui.Modifier
32
+ import androidx.compose.ui.graphics.Color
33
+ import androidx.compose.ui.platform.ComposeView
34
+ import androidx.compose.ui.platform.LocalConfiguration
35
+ import org.json.JSONObject
36
+ import java.util.Calendar
37
+ import java.util.Date
38
+
39
+ private fun Any?.asBoolean(): Boolean = when (this) {
40
+ is Boolean -> this
41
+ is Number -> toInt() != 0
42
+ else -> false
43
+ }
44
+
45
+ private fun Any?.asLong(): Long? = when (this) {
46
+ is Number -> toLong()
47
+ else -> null
48
+ }
49
+
50
+ private fun Any?.asColor(): Color? = when (this) {
51
+ is String -> runCatching { Color(android.graphics.Color.parseColor(this)) }.getOrNull()
52
+ else -> null
53
+ }
54
+
55
+ private data class SelectableRange(
56
+ val start: Long? = null,
57
+ val end: Long? = null,
58
+ )
59
+
60
+ private data class DatePickerUiState(
61
+ val initialDate: Long? = null,
62
+ val variant: String = "picker",
63
+ val displayedComponents: String = "date",
64
+ val showVariantToggle: Boolean = true,
65
+ val is24Hour: Boolean = true,
66
+ val color: Color? = null,
67
+ val elementColors: Map<*, *> = emptyMap<Any?, Any?>(),
68
+ val selectableDates: SelectableRange = SelectableRange(),
69
+ )
70
+
71
+ private fun toUtcDayMillis(localMillis: Long): Long {
72
+ val cal = Calendar.getInstance()
73
+ cal.timeInMillis = localMillis
74
+ val utcCal = Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC"))
75
+ utcCal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0)
76
+ utcCal.set(Calendar.MILLISECOND, 0)
77
+ return utcCal.timeInMillis
78
+ }
79
+
80
+ // Material3 stores selectedDateMillis as UTC-midnight epoch; emitting it raw
81
+ // makes `new Date(ms)` resolve to the previous day in negative-UTC zones.
82
+ // Convert back to local-midnight millis so the JS-side Date lands on the
83
+ // calendar day the user picked.
84
+ private fun toLocalDayMillis(utcMillis: Long): Long {
85
+ val utcCal = Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC"))
86
+ utcCal.timeInMillis = utcMillis
87
+ val localCal = Calendar.getInstance()
88
+ localCal.clear()
89
+ localCal.set(utcCal.get(Calendar.YEAR), utcCal.get(Calendar.MONTH), utcCal.get(Calendar.DAY_OF_MONTH))
90
+ return localCal.timeInMillis
91
+ }
92
+
93
+ @OptIn(ExperimentalMaterial3Api::class)
94
+ @Composable
95
+ private fun rememberSelectableDates(range: SelectableRange): SelectableDates {
96
+ val start = range.start
97
+ val end = range.end
98
+ return remember(start, end) {
99
+ if (start != null || end != null) {
100
+ val startUtcDayMillis = start?.let { toUtcDayMillis(it) }
101
+ val endUtcDayMillis = end?.let { toUtcDayMillis(it) }
102
+ val startYear = start?.let {
103
+ val cal = Calendar.getInstance()
104
+ cal.timeInMillis = it
105
+ cal.get(Calendar.YEAR)
106
+ }
107
+ val endYear = end?.let {
108
+ val cal = Calendar.getInstance()
109
+ cal.timeInMillis = it
110
+ cal.get(Calendar.YEAR)
111
+ }
112
+ object : SelectableDates {
113
+ override fun isSelectableDate(utcTimeMillis: Long): Boolean {
114
+ if (startUtcDayMillis != null && utcTimeMillis < startUtcDayMillis) return false
115
+ if (endUtcDayMillis != null && utcTimeMillis > endUtcDayMillis) return false
116
+ return true
117
+ }
118
+
119
+ override fun isSelectableYear(year: Int): Boolean {
120
+ if (startYear != null && year < startYear) return false
121
+ if (endYear != null && year > endYear) return false
122
+ return true
123
+ }
124
+ }
125
+ } else {
126
+ DatePickerDefaults.AllDates
127
+ }
128
+ }
129
+ }
130
+
131
+ // `selectableDates` only greys out unselectable cells; the calendar's actual
132
+ // extent (months list, year grid, range content description) comes from
133
+ // `yearRange`. Derive it from min/max so the picker reflects the constraints
134
+ // instead of always spanning the 1900..2100 default.
135
+ // https://github.com/expo/expo/issues/47206
136
+ @OptIn(ExperimentalMaterial3Api::class)
137
+ @Composable
138
+ private fun rememberDatePickerYearRange(range: SelectableRange, initialDateMillis: Long): IntRange {
139
+ val start = range.start
140
+ val end = range.end
141
+ return remember(start, end, initialDateMillis) {
142
+ val defaults = DatePickerDefaults.YearRange
143
+ val yearOf = { millis: Long ->
144
+ Calendar.getInstance().apply { timeInMillis = millis }.get(Calendar.YEAR)
145
+ }
146
+ val startYear = start?.let(yearOf) ?: defaults.first
147
+ val endYear = end?.let(yearOf) ?: defaults.last
148
+ val initialYear = yearOf(initialDateMillis)
149
+ minOf(startYear, initialYear)..maxOf(endYear, initialYear)
150
+ }
151
+ }
152
+
153
+ private fun Map<*, *>.color(name: String): Color? = this[name].asColor()
154
+
155
+ @OptIn(ExperimentalMaterial3Api::class)
156
+ @Composable
157
+ private fun buildDatePickerColors(
158
+ elementColors: Map<*, *>,
159
+ colorProp: Color?,
160
+ ): androidx.compose.material3.DatePickerColors {
161
+ val defaults = DatePickerDefaults.colors()
162
+ return defaults.copy(
163
+ containerColor = elementColors.color("containerColor") ?: defaults.containerColor,
164
+ titleContentColor = elementColors.color("titleContentColor") ?: colorProp ?: defaults.titleContentColor,
165
+ headlineContentColor = elementColors.color("headlineContentColor") ?: colorProp ?: defaults.headlineContentColor,
166
+ weekdayContentColor = elementColors.color("weekdayContentColor") ?: defaults.weekdayContentColor,
167
+ subheadContentColor = elementColors.color("subheadContentColor") ?: defaults.subheadContentColor,
168
+ navigationContentColor = elementColors.color("navigationContentColor") ?: defaults.navigationContentColor,
169
+ yearContentColor = elementColors.color("yearContentColor") ?: defaults.yearContentColor,
170
+ disabledYearContentColor = elementColors.color("disabledYearContentColor") ?: defaults.disabledYearContentColor,
171
+ currentYearContentColor = elementColors.color("currentYearContentColor") ?: defaults.currentYearContentColor,
172
+ selectedYearContentColor = elementColors.color("selectedYearContentColor") ?: defaults.selectedYearContentColor,
173
+ disabledSelectedYearContentColor = elementColors.color("disabledSelectedYearContentColor") ?: defaults.disabledSelectedYearContentColor,
174
+ selectedYearContainerColor = elementColors.color("selectedYearContainerColor") ?: defaults.selectedYearContainerColor,
175
+ disabledSelectedYearContainerColor = elementColors.color("disabledSelectedYearContainerColor") ?: defaults.disabledSelectedYearContainerColor,
176
+ dayContentColor = elementColors.color("dayContentColor") ?: defaults.dayContentColor,
177
+ disabledDayContentColor = elementColors.color("disabledDayContentColor") ?: defaults.disabledDayContentColor,
178
+ selectedDayContentColor = elementColors.color("selectedDayContentColor") ?: defaults.selectedDayContentColor,
179
+ disabledSelectedDayContentColor = elementColors.color("disabledSelectedDayContentColor") ?: defaults.disabledSelectedDayContentColor,
180
+ selectedDayContainerColor = elementColors.color("selectedDayContainerColor") ?: colorProp ?: defaults.selectedDayContainerColor,
181
+ disabledSelectedDayContainerColor = elementColors.color("disabledSelectedDayContainerColor") ?: defaults.disabledSelectedDayContainerColor,
182
+ todayContentColor = elementColors.color("todayContentColor") ?: defaults.todayContentColor,
183
+ todayDateBorderColor = elementColors.color("todayDateBorderColor") ?: colorProp ?: defaults.todayDateBorderColor,
184
+ dayInSelectionRangeContentColor = elementColors.color("dayInSelectionRangeContentColor") ?: defaults.dayInSelectionRangeContentColor,
185
+ dayInSelectionRangeContainerColor = elementColors.color("dayInSelectionRangeContainerColor") ?: defaults.dayInSelectionRangeContainerColor,
186
+ dividerColor = elementColors.color("dividerColor") ?: defaults.dividerColor,
187
+ )
188
+ }
189
+
190
+ @OptIn(ExperimentalMaterial3Api::class)
191
+ @Composable
192
+ private fun buildTimePickerColors(
193
+ elementColors: Map<*, *>,
194
+ colorProp: Color?,
195
+ ): androidx.compose.material3.TimePickerColors {
196
+ val defaults = TimePickerDefaults.colors()
197
+ return defaults.copy(
198
+ containerColor = elementColors.color("containerColor") ?: defaults.containerColor,
199
+ clockDialColor = elementColors.color("clockDialColor") ?: colorProp?.copy(alpha = 0.3f) ?: defaults.clockDialColor,
200
+ clockDialSelectedContentColor = elementColors.color("clockDialSelectedContentColor") ?: defaults.clockDialSelectedContentColor,
201
+ clockDialUnselectedContentColor = elementColors.color("clockDialUnselectedContentColor") ?: defaults.clockDialUnselectedContentColor,
202
+ selectorColor = elementColors.color("selectorColor") ?: colorProp ?: defaults.selectorColor,
203
+ periodSelectorBorderColor = elementColors.color("periodSelectorBorderColor") ?: defaults.periodSelectorBorderColor,
204
+ periodSelectorSelectedContainerColor = elementColors.color("periodSelectorSelectedContainerColor") ?: defaults.periodSelectorSelectedContainerColor,
205
+ periodSelectorUnselectedContainerColor = elementColors.color("periodSelectorUnselectedContainerColor") ?: defaults.periodSelectorUnselectedContainerColor,
206
+ periodSelectorSelectedContentColor = elementColors.color("periodSelectorSelectedContentColor") ?: defaults.periodSelectorSelectedContentColor,
207
+ periodSelectorUnselectedContentColor = elementColors.color("periodSelectorUnselectedContentColor") ?: defaults.periodSelectorUnselectedContentColor,
208
+ timeSelectorSelectedContainerColor = elementColors.color("timeSelectorSelectedContainerColor") ?: colorProp ?: defaults.timeSelectorSelectedContainerColor,
209
+ timeSelectorUnselectedContainerColor = elementColors.color("timeSelectorUnselectedContainerColor") ?: defaults.timeSelectorUnselectedContainerColor,
210
+ timeSelectorSelectedContentColor = elementColors.color("timeSelectorSelectedContentColor") ?: defaults.timeSelectorSelectedContentColor,
211
+ timeSelectorUnselectedContentColor = elementColors.color("timeSelectorUnselectedContentColor") ?: defaults.timeSelectorUnselectedContentColor,
212
+ )
213
+ }
214
+
215
+ @OptIn(ExperimentalMaterial3Api::class)
216
+ @Composable
217
+ private fun ExpoDatePicker(modifier: Modifier = Modifier, state: DatePickerUiState, onDateSelected: (Long?) -> Unit) {
218
+ val locale = LocalConfiguration.current.locales[0]
219
+ val displayMode = if (state.variant == "input") DisplayMode.Input else DisplayMode.Picker
220
+ val fallbackDate = remember { Date().time }
221
+ val initialDate = state.initialDate ?: fallbackDate
222
+ val selectableDates = rememberSelectableDates(state.selectableDates)
223
+ val yearRange = rememberDatePickerYearRange(state.selectableDates, initialDate)
224
+
225
+ val pickerState = remember(displayMode, initialDate, selectableDates, yearRange) {
226
+ DatePickerState(
227
+ initialDisplayMode = displayMode,
228
+ locale = locale,
229
+ initialSelectedDateMillis = state.initialDate,
230
+ initialDisplayedMonthMillis = initialDate,
231
+ yearRange = yearRange,
232
+ selectableDates = selectableDates,
233
+ )
234
+ }
235
+
236
+ LaunchedEffect(pickerState.selectedDateMillis) {
237
+ onDateSelected(pickerState.selectedDateMillis)
238
+ }
239
+
240
+ val colors = buildDatePickerColors(state.elementColors, state.color)
241
+
242
+ // Material3's year-selector chevron tints from the ambient
243
+ // LocalContentColor (which defaults to black), not
244
+ // `navigationContentColor`; bind the local so the chevron honors the
245
+ // navigation color.
246
+ CompositionLocalProvider(LocalContentColor provides colors.navigationContentColor) {
247
+ DatePicker(
248
+ modifier = modifier,
249
+ state = pickerState,
250
+ showModeToggle = state.showVariantToggle,
251
+ colors = colors,
252
+ )
253
+ }
254
+ }
255
+
256
+ @OptIn(ExperimentalMaterial3Api::class)
257
+ @Composable
258
+ private fun ExpoTimePicker(modifier: Modifier = Modifier, state: DatePickerUiState, onDateSelected: (Long?) -> Unit) {
259
+ val initialDate = state.initialDate
260
+
261
+ val pickerState = remember(initialDate, state.is24Hour) {
262
+ val cal = Calendar.getInstance()
263
+ cal.isLenient = false
264
+ if (initialDate != null) {
265
+ cal.timeInMillis = initialDate
266
+ }
267
+ TimePickerState(
268
+ initialHour = cal.get(Calendar.HOUR_OF_DAY),
269
+ initialMinute = cal.get(Calendar.MINUTE),
270
+ is24Hour = state.is24Hour,
271
+ )
272
+ }
273
+
274
+ LaunchedEffect(pickerState.hour, pickerState.minute) {
275
+ val cal = Calendar.getInstance()
276
+ cal.isLenient = false
277
+ if (initialDate != null) {
278
+ cal.timeInMillis = initialDate
279
+ }
280
+ cal.set(Calendar.HOUR_OF_DAY, pickerState.hour)
281
+ cal.set(Calendar.MINUTE, pickerState.minute)
282
+ onDateSelected(cal.time.time)
283
+ }
284
+
285
+ TimePicker(
286
+ modifier = modifier,
287
+ state = pickerState,
288
+ layoutType = TimePickerLayoutType.Vertical,
289
+ colors = buildTimePickerColors(state.elementColors, state.color),
290
+ )
291
+ }
292
+
293
+ class XplatDatePickerProvider {
294
+ private var state by mutableStateOf(DatePickerUiState())
295
+ var onEvent: ((Any) -> Unit)? = null
296
+
297
+ fun generateComposeView(view: ComposeView) {
298
+ view.setContent {
299
+ MaterialTheme {
300
+ if (state.displayedComponents == "hourAndMinute") {
301
+ ExpoTimePicker(state = state) { millis ->
302
+ onEvent?.invoke(JSONObject().put("date", millis ?: JSONObject.NULL))
303
+ }
304
+ } else {
305
+ ExpoDatePicker(state = state) { millis ->
306
+ onEvent?.invoke(
307
+ JSONObject().put(
308
+ "date",
309
+ millis?.let { toLocalDayMillis(it) } ?: JSONObject.NULL,
310
+ ),
311
+ )
312
+ }
313
+ }
314
+ }
315
+ }
316
+ }
317
+
318
+ fun updateData(input: Map<Any, Any>) {
319
+ val data = input["data"] as? Map<*, *> ?: input
320
+ val range = data["selectableDates"] as? Map<*, *>
321
+ state = DatePickerUiState(
322
+ initialDate = data["initialDate"].asLong(),
323
+ variant = data["variant"] as? String ?: "picker",
324
+ displayedComponents = data["displayedComponents"] as? String ?: "date",
325
+ showVariantToggle = data["showVariantToggle"]?.asBoolean() ?: true,
326
+ is24Hour = data["is24Hour"]?.asBoolean() ?: true,
327
+ color = data["color"].asColor(),
328
+ elementColors = data["elementColors"] as? Map<*, *> ?: emptyMap<Any?, Any?>(),
329
+ selectableDates = SelectableRange(
330
+ start = range?.get("start").asLong(),
331
+ end = range?.get("end").asLong(),
332
+ ),
333
+ )
334
+ }
335
+ }