@octane-xplat/date-picker 0.18.0 → 0.19.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.
@@ -1,5 +1,7 @@
1
+ /** Date-picker presentation variant. */
1
2
  export type MaterialDatePickerVariant = 'picker' | 'input'
2
3
 
4
+ /** Per-part Material color slots. */
3
5
  export type MaterialDatePickerComponents = 'date' | 'hourAndMinute' | 'dateAndTime'
4
6
 
5
7
  /**
@@ -8,48 +10,126 @@ export type MaterialDatePickerComponents = 'date' | 'hourAndMinute' | 'dateAndTi
8
10
  * Material 3 theme defaults.
9
11
  */
10
12
  export interface MaterialDatePickerElementColors {
13
+ /** Part color override (any platform color string). */
14
+
11
15
  containerColor?: string
16
+ /** Part color override (any platform color string). */
17
+
12
18
  titleContentColor?: string
19
+ /** Part color override (any platform color string). */
20
+
13
21
  headlineContentColor?: string
22
+ /** Part color override (any platform color string). */
23
+
14
24
  weekdayContentColor?: string
25
+ /** Part color override (any platform color string). */
26
+
15
27
  subheadContentColor?: string
28
+ /** Part color override (any platform color string). */
29
+
16
30
  navigationContentColor?: string
31
+ /** Part color override (any platform color string). */
32
+
17
33
  yearContentColor?: string
34
+ /** Part color override (any platform color string). */
35
+
18
36
  disabledYearContentColor?: string
37
+ /** Part color override (any platform color string). */
38
+
19
39
  currentYearContentColor?: string
40
+ /** Part color override (any platform color string). */
41
+
20
42
  selectedYearContentColor?: string
43
+ /** Part color override (any platform color string). */
44
+
21
45
  disabledSelectedYearContentColor?: string
46
+ /** Part color override (any platform color string). */
47
+
22
48
  selectedYearContainerColor?: string
49
+ /** Part color override (any platform color string). */
50
+
23
51
  disabledSelectedYearContainerColor?: string
52
+ /** Part color override (any platform color string). */
53
+
24
54
  dayContentColor?: string
55
+ /** Part color override (any platform color string). */
56
+
25
57
  disabledDayContentColor?: string
58
+ /** Part color override (any platform color string). */
59
+
26
60
  selectedDayContentColor?: string
61
+ /** Part color override (any platform color string). */
62
+
27
63
  disabledSelectedDayContentColor?: string
64
+ /** Part color override (any platform color string). */
65
+
28
66
  selectedDayContainerColor?: string
67
+ /** Part color override (any platform color string). */
68
+
29
69
  disabledSelectedDayContainerColor?: string
70
+ /** Part color override (any platform color string). */
71
+
30
72
  todayContentColor?: string
73
+ /** Part color override (any platform color string). */
74
+
31
75
  todayDateBorderColor?: string
76
+ /** Part color override (any platform color string). */
77
+
32
78
  dayInSelectionRangeContentColor?: string
79
+ /** Part color override (any platform color string). */
80
+
33
81
  dayInSelectionRangeContainerColor?: string
82
+ /** Part color override (any platform color string). */
83
+
34
84
  dividerColor?: string
85
+ /** Part color override (any platform color string). */
86
+
35
87
  clockDialColor?: string
88
+ /** Part color override (any platform color string). */
89
+
36
90
  clockDialSelectedContentColor?: string
91
+ /** Part color override (any platform color string). */
92
+
37
93
  clockDialUnselectedContentColor?: string
94
+ /** Part color override (any platform color string). */
95
+
38
96
  selectorColor?: string
97
+ /** Part color override (any platform color string). */
98
+
39
99
  periodSelectorBorderColor?: string
100
+ /** Part color override (any platform color string). */
101
+
40
102
  periodSelectorSelectedContainerColor?: string
103
+ /** Part color override (any platform color string). */
104
+
41
105
  periodSelectorUnselectedContainerColor?: string
106
+ /** Part color override (any platform color string). */
107
+
42
108
  periodSelectorSelectedContentColor?: string
109
+ /** Part color override (any platform color string). */
110
+
43
111
  periodSelectorUnselectedContentColor?: string
112
+ /** Part color override (any platform color string). */
113
+
44
114
  timeSelectorSelectedContainerColor?: string
115
+ /** Part color override (any platform color string). */
116
+
45
117
  timeSelectorUnselectedContainerColor?: string
118
+ /** Part color override (any platform color string). */
119
+
46
120
  timeSelectorSelectedContentColor?: string
121
+ /** Part color override (any platform color string). */
122
+
47
123
  timeSelectorUnselectedContentColor?: string
48
124
  }
49
125
 
126
+ /** Android date-picker props (Material3 date picker). */
50
127
  export interface MaterialDatePickerProps {
128
+ /** Element id passed through to the host element. */
51
129
  id?: string
130
+ /** Class list applied to the picker element. */
52
131
  className?: any
132
+ /** Inline style (string CSS or object). */
53
133
  style?: any
54
134
  /** Date the picker starts on. The picker keeps its own selection after mount. */
55
135
  initialDate?: Date
@@ -66,10 +146,15 @@ export interface MaterialDatePickerProps {
66
146
  /** 24-hour dial for the time picker. @default true */
67
147
  is24Hour?: boolean
68
148
  /** Accent applied to a subset of elements when `elementColors` is unset. */
149
+ /** Part color override (any platform color string). */
150
+
69
151
  color?: string
152
+ /** Part color override (any platform color string). */
153
+
70
154
  elementColors?: MaterialDatePickerElementColors
71
155
  /** Bounds the selectable date range; also derives the calendar year range. */
72
156
  selectableDates?: { start?: Date; end?: Date }
157
+ /** Spoken name for assistive tech. */
73
158
  accessibilityLabel?: string
74
159
  /**
75
160
  * Called with the selected timestamp (null when the selection clears). The
@@ -0,0 +1,234 @@
1
+ import type { AppKitDatePickerProps } from './macos/types'
2
+
3
+ export interface DatePickerOptions {
4
+ mode: NonNullable<AppKitDatePickerProps['components']>
5
+ style: NonNullable<AppKitDatePickerProps['pickerStyle']>
6
+ selectionMillis: number
7
+ minimumMillis?: number
8
+ maximumMillis?: number
9
+ enabled: boolean
10
+ accessibilityLabel?: string
11
+ }
12
+
13
+ export interface DatePickerHandle {
14
+ update(options: DatePickerOptions): void
15
+ detach(): void
16
+ }
17
+
18
+ export type DatePickerAttach = (
19
+ view: object,
20
+ options: DatePickerOptions,
21
+ onChange: (millis: number) => void,
22
+ ) => DatePickerHandle | null
23
+
24
+ export type DatePickerStatus = 'ready' | 'invalid' | 'unavailable'
25
+
26
+ function milliseconds(date: Date | undefined): number | undefined {
27
+ try {
28
+ return date === undefined ? undefined : date.getTime()
29
+ } catch {
30
+ return NaN
31
+ }
32
+ }
33
+
34
+ const valid = (value: number) =>
35
+ Number.isFinite(value) && Number.isFinite(new Date(value).getTime())
36
+
37
+ /** Own one mounted attachment. Default selection seeds once; mode/view changes
38
+ * retain the accepted selection. This model does not prove native editing. */
39
+ export function createDatePickerOwner(deps: {
40
+ props(): AppKitDatePickerProps
41
+ attach(): DatePickerAttach | undefined
42
+ status?(status: DatePickerStatus): void
43
+ now?(): number
44
+ }): {
45
+ update: () => void
46
+ bind(next: object | null): void
47
+ dispose(): void
48
+ getStatus: () => DatePickerStatus
49
+ } {
50
+ let view: object | null = null,
51
+ disposed = false,
52
+ retained: number | undefined
53
+ let status: DatePickerStatus = 'unavailable'
54
+ type Session = {
55
+ view: object
56
+ mode: string
57
+ style: string
58
+ writing: number
59
+ handle: DatePickerHandle | null
60
+ }
61
+ let session: Session | null = null
62
+ const report = (next: DatePickerStatus) => {
63
+ if (!disposed && next !== status) {
64
+ status = next
65
+ deps.status?.(next)
66
+ }
67
+ }
68
+ const detach = () => {
69
+ const old = session
70
+ session = null
71
+ try {
72
+ old?.handle?.detach()
73
+ } catch {}
74
+ }
75
+ const plan = (): DatePickerOptions | null => {
76
+ const props = deps.props(),
77
+ min = milliseconds(props.minimumDate),
78
+ max = milliseconds(props.maximumDate)
79
+ if (
80
+ (min !== undefined && !valid(min)) ||
81
+ (max !== undefined && !valid(max)) ||
82
+ (min !== undefined && max !== undefined && min > max)
83
+ ) {
84
+ return null
85
+ }
86
+ const controlled = props.selection !== undefined
87
+ let selected = controlled
88
+ ? milliseconds(props.selection)!
89
+ : (retained ?? milliseconds(props.defaultSelection) ?? (deps.now ?? Date.now)())
90
+ if (!valid(selected)) {
91
+ return null
92
+ }
93
+ if (
94
+ controlled &&
95
+ ((min !== undefined && selected < min) || (max !== undefined && selected > max))
96
+ ) {
97
+ return null
98
+ }
99
+ if (!controlled) {
100
+ selected = Math.min(max ?? Infinity, Math.max(min ?? -Infinity, selected))
101
+ }
102
+ const mode = props.components ?? 'date'
103
+ return {
104
+ mode,
105
+ style: mode === 'date' ? (props.pickerStyle ?? 'textField') : 'textField',
106
+ selectionMillis: selected,
107
+ minimumMillis: min,
108
+ maximumMillis: max,
109
+ enabled: !props.disabled,
110
+ accessibilityLabel: props.accessibilityLabel,
111
+ }
112
+ }
113
+ const update = () => {
114
+ if (disposed || !view) {
115
+ return
116
+ }
117
+ const options = plan()
118
+ if (!options) {
119
+ detach()
120
+ report('invalid')
121
+ return
122
+ }
123
+ retained = options.selectionMillis
124
+ if (
125
+ session &&
126
+ (session.view !== view || session.mode !== options.mode || session.style !== options.style)
127
+ ) {
128
+ detach()
129
+ }
130
+ if (session?.handle) {
131
+ const owner = session
132
+ owner.writing++
133
+ try {
134
+ owner.handle!.update(options)
135
+ if (session === owner) {
136
+ report('ready')
137
+ }
138
+ } catch {
139
+ if (session === owner) {
140
+ detach()
141
+ report('unavailable')
142
+ }
143
+ } finally {
144
+ owner.writing--
145
+ }
146
+
147
+ return
148
+ }
149
+ const attach = deps.attach()
150
+ if (!attach) {
151
+ report('unavailable')
152
+ return
153
+ }
154
+ const owner: Session = {
155
+ view,
156
+ mode: options.mode,
157
+ style: options.style,
158
+ writing: 1,
159
+ handle: null,
160
+ }
161
+ session = owner
162
+ try {
163
+ const handle = attach(view, options, (millis) => {
164
+ if (disposed || session !== owner || owner.writing || !owner.handle) {
165
+ return
166
+ }
167
+ const current = plan()
168
+ if (!current || !current.enabled || !valid(millis)) {
169
+ update()
170
+ return
171
+ }
172
+ const picked = new Date(millis).getTime()
173
+ if (
174
+ (current.minimumMillis !== undefined && picked < current.minimumMillis) ||
175
+ (current.maximumMillis !== undefined && picked > current.maximumMillis)
176
+ ) {
177
+ update()
178
+ return
179
+ }
180
+ if (picked === current.selectionMillis) {
181
+ return
182
+ }
183
+ if (deps.props().selection === undefined) {
184
+ retained = picked
185
+ }
186
+ try {
187
+ deps.props().onSelectionChange?.(new Date(picked))
188
+ } finally {
189
+ update()
190
+ } // Restore a rejected controlled value; suppress programmatic echoes.
191
+ })
192
+
193
+ if (disposed || session !== owner) {
194
+ handle?.detach()
195
+ return
196
+ }
197
+
198
+ owner.handle = handle
199
+ if (!handle) {
200
+ session = null
201
+ report('unavailable')
202
+ } else {
203
+ report('ready')
204
+ }
205
+ } catch {
206
+ if (session === owner) {
207
+ detach()
208
+ report('unavailable')
209
+ }
210
+ } finally {
211
+ owner.writing--
212
+ }
213
+ }
214
+ return {
215
+ update,
216
+ bind(next: object | null) {
217
+ if (!disposed && next !== view) {
218
+ detach()
219
+ view = next
220
+ if (view) {
221
+ update()
222
+ }
223
+ }
224
+ },
225
+ dispose() {
226
+ if (!disposed) {
227
+ disposed = true
228
+ detach()
229
+ view = null
230
+ }
231
+ },
232
+ getStatus: () => status,
233
+ }
234
+ }
package/src/ios/types.ts CHANGED
@@ -1,10 +1,16 @@
1
+ /** SwiftUI date-picker style. */
1
2
  export type SwiftUIDatePickerStyle = 'automatic' | 'compact' | 'graphical' | 'wheel'
2
3
 
4
+ /** SwiftUI picker component selector. */
3
5
  export type SwiftUIDatePickerComponent = 'date' | 'hourAndMinute'
4
6
 
7
+ /** iOS date-picker props (SwiftUI DatePicker). */
5
8
  export interface SwiftUIDatePickerProps {
9
+ /** Element id passed through to the host element. */
6
10
  id?: string
11
+ /** Class list applied to the picker element. */
7
12
  className?: any
13
+ /** Inline style (string CSS or object). */
8
14
  style?: any
9
15
  /** Label shown next to the picker. When omitted, the label is hidden. */
10
16
  title?: string
@@ -20,7 +26,10 @@ export interface SwiftUIDatePickerProps {
20
26
  displayedComponents?: readonly SwiftUIDatePickerComponent[]
21
27
  /** SwiftUI presentation style. `graphical` renders an inline calendar. @default 'automatic' */
22
28
  pickerStyle?: SwiftUIDatePickerStyle
29
+ /** Disables interaction with the picker. */
23
30
  disabled?: boolean
31
+ /** Spoken name for assistive tech. */
24
32
  accessibilityLabel?: string
33
+ /** Called when the user picks a new date; programmatic `selection` updates do not emit. */
25
34
  onSelectionChange?: (date: Date) => void
26
35
  }
@@ -1,10 +1,16 @@
1
+ /** AppKit picker component slots. */
1
2
  export type AppKitDatePickerComponents = 'date' | 'time' | 'dateAndTime'
2
3
 
4
+ /** macOS date-picker style. */
3
5
  export type AppKitDatePickerStyle = 'textField' | 'graphical'
4
6
 
7
+ /** macOS date-picker props (NSDatePicker). */
5
8
  export interface AppKitDatePickerProps {
9
+ /** Element id passed through to the host element. */
6
10
  id?: string
11
+ /** Class list applied to the picker element. */
7
12
  className?: any
13
+ /** Inline style (string CSS or object). */
8
14
  style?: any
9
15
  /** Which element groups the NSDatePicker shows. @default 'date' */
10
16
  components?: AppKitDatePickerComponents
@@ -12,13 +18,16 @@ export interface AppKitDatePickerProps {
12
18
  pickerStyle?: AppKitDatePickerStyle
13
19
  /** Controlled selection. Pair with `onSelectionChange`. */
14
20
  selection?: Date
15
- /** Initial selection when `selection` is not provided. */
21
+ /** Initial uncontrolled seed. Later defaults do not reset the mounted picker. */
16
22
  defaultSelection?: Date
17
23
  /** Earliest selectable date. */
18
24
  minimumDate?: Date
19
25
  /** Latest selectable date. */
20
26
  maximumDate?: Date
27
+ /** Disables interaction with the picker. */
21
28
  disabled?: boolean
29
+ /** Spoken name for assistive tech. */
22
30
  accessibilityLabel?: string
31
+ /** User selection changes; programmatic updates and retired controls do not emit. */
23
32
  onSelectionChange?: (date: Date) => void
24
33
  }
@@ -0,0 +1,56 @@
1
+ const kebab = (key: string) => key.replace(/[A-Z]/g, (c) => '-' + c.toLowerCase())
2
+
3
+ /** Anything accepted as a style value, including nested arrays. */
4
+ export type StyleInput = string | Record<string, any> | null | undefined | readonly StyleInput[]
5
+
6
+ /** Merge component-emitted style defaults under the authored `style` prop.
7
+ * Authored keys win; array styles fold in order; a string style is appended
8
+ * after the serialized defaults so its declarations override them too. */
9
+ export function mergeStyle(
10
+ base: Record<string, any>,
11
+ style?: Record<string, any> | readonly (Record<string, any> | null | undefined)[] | null,
12
+ ): Record<string, any>
13
+
14
+ export function mergeStyle(
15
+ base: Record<string, any>,
16
+ style?: StyleInput,
17
+ ): string | Record<string, any>
18
+
19
+ export function mergeStyle(
20
+ base: Record<string, any>,
21
+ style?: StyleInput,
22
+ ): string | Record<string, any> {
23
+ if (style == null) {
24
+ return base
25
+ }
26
+
27
+ let obj: Record<string, any> = { ...base }
28
+ const strings: string[] = []
29
+ const add = (s: StyleInput) => {
30
+ if (s == null) {
31
+ return
32
+ }
33
+ if (typeof s === 'string') {
34
+ strings.push(s)
35
+ } else if (Array.isArray(s)) {
36
+ for (const part of s) {
37
+ add(part)
38
+ }
39
+ } else {
40
+ obj = { ...obj, ...s }
41
+ }
42
+ }
43
+ add(style)
44
+
45
+ if (strings.length === 0) {
46
+ return obj
47
+ }
48
+
49
+ const prefix = Object.entries(obj)
50
+ .filter(([, v]) => v != null)
51
+ .map(([k, v]) => `${kebab(k)}:${v}`)
52
+ .join(';')
53
+ return [prefix, ...strings].filter(Boolean).join(';')
54
+ }
55
+
56
+ export type StyleProp = string | Record<string, any> | readonly StyleProp[] | null | undefined
@@ -0,0 +1,17 @@
1
+ {
2
+ "version": 1,
3
+ "files": [
4
+ "AppKitDatePicker.macos.d.ts",
5
+ "MaterialDatePicker.android.d.ts",
6
+ "SwiftUIDatePicker.ios.d.ts",
7
+ "android/index.d.ts",
8
+ "android/types.d.ts",
9
+ "date-picker-owner.macos.d.ts",
10
+ "index.d.ts",
11
+ "ios/index.d.ts",
12
+ "ios/types.d.ts",
13
+ "macos/index.d.ts",
14
+ "macos/types.d.ts",
15
+ "style-merge.d.ts"
16
+ ]
17
+ }
@@ -0,0 +1,10 @@
1
+ /** @jsxImportSource @octane-xplat/jsx */
2
+
3
+ import type { AppKitDatePickerProps } from './macos/types.js';
4
+ /** Embeds a real NSDatePicker in the leaf's backing view via the
5
+ * `__xplatAppKit` bridge. 'date' uses a text field (or the inline
6
+ * graphical calendar when `pickerStyle` is 'graphical'); 'time' uses the
7
+ * clock-and-calendar field; 'dateAndTime' shows both element groups.
8
+ * The host element carries an explicit size — the picker is pinned to
9
+ * its edges. */
10
+ export declare function AppKitDatePicker(props: AppKitDatePickerProps): unknown;
@@ -0,0 +1,15 @@
1
+ import type { Attributes } from '@octane-xplat/jsx/intrinsics';
2
+ import type { MaterialDatePickerProps } from './android/types.js';
3
+ declare module '@octane-xplat/jsx/intrinsics' {
4
+ interface NativeElements<TElement> {
5
+ xplatandroiddatepicker: Attributes<TElement, {
6
+ composeId?: string;
7
+ data?: Record<string, unknown>;
8
+ onComposeEvent?: (event: {
9
+ data?: unknown;
10
+ }) => void;
11
+ }>;
12
+ }
13
+ }
14
+ /** Renders a Material 3 DatePicker/TimePicker through the NativeScript Jetpack Compose bridge. */
15
+ export declare function MaterialDatePicker(props: MaterialDatePickerProps): unknown;
@@ -0,0 +1,15 @@
1
+ import type { Attributes } from '@octane-xplat/jsx/intrinsics';
2
+ import type { SwiftUIDatePickerProps } from './ios/types.js';
3
+ declare module '@octane-xplat/jsx/intrinsics' {
4
+ interface NativeElements<TElement> {
5
+ xplatiosdatepicker: Attributes<TElement, {
6
+ swiftId?: string;
7
+ data?: Record<string, unknown>;
8
+ onSwiftUIEvent?: (event: {
9
+ data?: unknown;
10
+ }) => void;
11
+ }>;
12
+ }
13
+ }
14
+ /** Renders a SwiftUI DatePicker through NativeScript's SwiftUI bridge. */
15
+ export declare function SwiftUIDatePicker(props: SwiftUIDatePickerProps): unknown;
@@ -0,0 +1,2 @@
1
+ export { MaterialDatePicker } from '../MaterialDatePicker.android.js';
2
+ export type { MaterialDatePickerComponents, MaterialDatePickerElementColors, MaterialDatePickerProps, MaterialDatePickerVariant, } from './types.js';