@octane-xplat/date-picker 0.19.0 → 0.20.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,234 +1,264 @@
1
1
  import type { AppKitDatePickerProps } from './macos/types'
2
2
 
3
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
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
11
  }
12
12
 
13
13
  export interface DatePickerHandle {
14
- update(options: DatePickerOptions): void
15
- detach(): void
14
+ update(options: DatePickerOptions): void
15
+ detach(): void
16
16
  }
17
17
 
18
18
  export type DatePickerAttach = (
19
- view: object,
20
- options: DatePickerOptions,
21
- onChange: (millis: number) => void,
19
+ view: object,
20
+ options: DatePickerOptions,
21
+ onChange: (millis: number) => void,
22
22
  ) => DatePickerHandle | null
23
23
 
24
24
  export type DatePickerStatus = 'ready' | 'invalid' | 'unavailable'
25
25
 
26
26
  function milliseconds(date: Date | undefined): number | undefined {
27
- try {
28
- return date === undefined ? undefined : date.getTime()
29
- } catch {
30
- return NaN
31
- }
27
+ try {
28
+ return date === undefined ? undefined : date.getTime()
29
+ } catch {
30
+ return NaN
31
+ }
32
32
  }
33
33
 
34
34
  const valid = (value: number) =>
35
- Number.isFinite(value) && Number.isFinite(new Date(value).getTime())
35
+ Number.isFinite(value) && Number.isFinite(new Date(value).getTime())
36
36
 
37
37
  /** Own one mounted attachment. Default selection seeds once; mode/view changes
38
38
  * retain the accepted selection. This model does not prove native editing. */
39
39
  export function createDatePickerOwner(deps: {
40
- props(): AppKitDatePickerProps
41
- attach(): DatePickerAttach | undefined
42
- status?(status: DatePickerStatus): void
43
- now?(): number
40
+ props(): AppKitDatePickerProps
41
+ attach(): DatePickerAttach | undefined
42
+ status?(status: DatePickerStatus): void
43
+ now?(): number
44
44
  }): {
45
- update: () => void
46
- bind(next: object | null): void
47
- dispose(): void
48
- getStatus: () => DatePickerStatus
45
+ update: () => void
46
+ bind(next: object | null): void
47
+ dispose(): void
48
+ getStatus: () => DatePickerStatus
49
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
- }
50
+ let view: object | null = null,
51
+ disposed = false,
52
+ retained: number | undefined
53
+
54
+ let status: DatePickerStatus = 'unavailable'
55
+ type Session = {
56
+ view: object
57
+ mode: string
58
+ style: string
59
+ writing: number
60
+ handle: DatePickerHandle | null
61
+ }
62
+
63
+ let session: Session | null = null
64
+ const report = (next: DatePickerStatus) => {
65
+ if (!disposed && next !== status) {
66
+ status = next
67
+ deps.status?.(next)
68
+ }
69
+ }
70
+
71
+ const detach = () => {
72
+ const old = session
73
+
74
+ session = null
75
+ try {
76
+ old?.handle?.detach()
77
+ } catch {}
78
+ }
79
+
80
+ const plan = (): DatePickerOptions | null => {
81
+ const props = deps.props(),
82
+ min = milliseconds(props.minimumDate),
83
+ max = milliseconds(props.maximumDate)
84
+
85
+ if (
86
+ (min !== undefined && !valid(min)) ||
87
+ (max !== undefined && !valid(max)) ||
88
+ (min !== undefined && max !== undefined && min > max)
89
+ ) {
90
+ return null
91
+ }
92
+
93
+ const controlled = props.selection !== undefined
94
+ let selected = controlled
95
+ ? milliseconds(props.selection)!
96
+ : (retained ?? milliseconds(props.defaultSelection) ?? (deps.now ?? Date.now)())
97
+
98
+ if (!valid(selected)) {
99
+ return null
100
+ }
101
+
102
+ if (
103
+ controlled &&
104
+ ((min !== undefined && selected < min) || (max !== undefined && selected > max))
105
+ ) {
106
+ return null
107
+ }
108
+
109
+ if (!controlled) {
110
+ selected = Math.min(max ?? Infinity, Math.max(min ?? -Infinity, selected))
111
+ }
112
+
113
+ const mode = props.components ?? 'date'
114
+
115
+ return {
116
+ mode,
117
+ style: mode === 'date' ? (props.pickerStyle ?? 'textField') : 'textField',
118
+ selectionMillis: selected,
119
+ minimumMillis: min,
120
+ maximumMillis: max,
121
+ enabled: !props.disabled,
122
+ accessibilityLabel: props.accessibilityLabel,
123
+ }
124
+ }
125
+
126
+ const update = () => {
127
+ if (disposed || !view) {
128
+ return
129
+ }
130
+
131
+ const options = plan()
132
+
133
+ if (!options) {
134
+ detach()
135
+ report('invalid')
136
+ return
137
+ }
138
+
139
+ retained = options.selectionMillis
140
+ if (
141
+ session &&
142
+ (session.view !== view || session.mode !== options.mode || session.style !== options.style)
143
+ ) {
144
+ detach()
145
+ }
146
+
147
+ if (session?.handle) {
148
+ const owner = session
149
+
150
+ owner.writing++
151
+ try {
152
+ owner.handle!.update(options)
153
+ if (session === owner) {
154
+ report('ready')
155
+ }
156
+ } catch {
157
+ if (session === owner) {
158
+ detach()
159
+ report('unavailable')
160
+ }
161
+ } finally {
162
+ owner.writing--
163
+ }
164
+
165
+ return
166
+ }
167
+
168
+ const attach = deps.attach()
169
+
170
+ if (!attach) {
171
+ report('unavailable')
172
+ return
173
+ }
174
+
175
+ const owner: Session = {
176
+ view,
177
+ mode: options.mode,
178
+ style: options.style,
179
+ writing: 1,
180
+ handle: null,
181
+ }
182
+
183
+ session = owner
184
+ try {
185
+ const handle = attach(view, options, (millis) => {
186
+ if (disposed || session !== owner || owner.writing || !owner.handle) {
187
+ return
188
+ }
189
+
190
+ const current = plan()
191
+
192
+ if (!current || !current.enabled || !valid(millis)) {
193
+ update()
194
+ return
195
+ }
196
+
197
+ const picked = new Date(millis).getTime()
198
+
199
+ if (
200
+ (current.minimumMillis !== undefined && picked < current.minimumMillis) ||
201
+ (current.maximumMillis !== undefined && picked > current.maximumMillis)
202
+ ) {
203
+ update()
204
+ return
205
+ }
206
+
207
+ if (picked === current.selectionMillis) {
208
+ return
209
+ }
210
+
211
+ if (deps.props().selection === undefined) {
212
+ retained = picked
213
+ }
214
+
215
+ try {
216
+ deps.props().onSelectionChange?.(new Date(picked))
217
+ } finally {
218
+ update()
219
+ } // Restore a rejected controlled value; suppress programmatic echoes.
220
+ })
221
+
222
+ if (disposed || session !== owner) {
223
+ handle?.detach()
224
+ return
225
+ }
226
+
227
+ owner.handle = handle
228
+ if (!handle) {
229
+ session = null
230
+ report('unavailable')
231
+ } else {
232
+ report('ready')
233
+ }
234
+ } catch {
235
+ if (session === owner) {
236
+ detach()
237
+ report('unavailable')
238
+ }
239
+ } finally {
240
+ owner.writing--
241
+ }
242
+ }
243
+
244
+ return {
245
+ update,
246
+ bind(next: object | null) {
247
+ if (!disposed && next !== view) {
248
+ detach()
249
+ view = next
250
+ if (view) {
251
+ update()
252
+ }
253
+ }
254
+ },
255
+ dispose() {
256
+ if (!disposed) {
257
+ disposed = true
258
+ detach()
259
+ view = null
260
+ }
261
+ },
262
+ getStatus: () => status,
263
+ }
234
264
  }
package/src/ios/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { SwiftUIDatePicker } from '../SwiftUIDatePicker.ios.tsrx'
2
2
  export type {
3
- SwiftUIDatePickerComponent,
4
- SwiftUIDatePickerProps,
5
- SwiftUIDatePickerStyle,
3
+ SwiftUIDatePickerComponent,
4
+ SwiftUIDatePickerProps,
5
+ SwiftUIDatePickerStyle,
6
6
  } from './types'
package/src/ios/types.ts CHANGED
@@ -6,30 +6,30 @@ export type SwiftUIDatePickerComponent = 'date' | 'hourAndMinute'
6
6
 
7
7
  /** iOS date-picker props (SwiftUI DatePicker). */
8
8
  export interface SwiftUIDatePickerProps {
9
- /** Element id passed through to the host element. */
10
- id?: string
11
- /** Class list applied to the picker element. */
12
- className?: any
13
- /** Inline style (string CSS or object). */
14
- style?: any
15
- /** Label shown next to the picker. When omitted, the label is hidden. */
16
- title?: string
17
- /** Controlled selection. Pair with `onSelectionChange`. */
18
- selection?: Date
19
- /** Initial selection when `selection` is not provided. */
20
- defaultSelection?: Date
21
- /** Earliest selectable date. */
22
- minimumDate?: Date
23
- /** Latest selectable date. */
24
- maximumDate?: Date
25
- /** Which components the picker displays. @default ['date'] */
26
- displayedComponents?: readonly SwiftUIDatePickerComponent[]
27
- /** SwiftUI presentation style. `graphical` renders an inline calendar. @default 'automatic' */
28
- pickerStyle?: SwiftUIDatePickerStyle
29
- /** Disables interaction with the picker. */
30
- disabled?: boolean
31
- /** Spoken name for assistive tech. */
32
- accessibilityLabel?: string
33
- /** Called when the user picks a new date; programmatic `selection` updates do not emit. */
34
- onSelectionChange?: (date: Date) => void
9
+ /** Element id passed through to the host element. */
10
+ id?: string
11
+ /** Class list applied to the picker element. */
12
+ className?: any
13
+ /** Inline style (string CSS or object). */
14
+ style?: any
15
+ /** Label shown next to the picker. When omitted, the label is hidden. */
16
+ title?: string
17
+ /** Controlled selection. Pair with `onSelectionChange`. */
18
+ selection?: Date
19
+ /** Initial selection when `selection` is not provided. */
20
+ defaultSelection?: Date
21
+ /** Earliest selectable date. */
22
+ minimumDate?: Date
23
+ /** Latest selectable date. */
24
+ maximumDate?: Date
25
+ /** Which components the picker displays. @default ['date'] */
26
+ displayedComponents?: readonly SwiftUIDatePickerComponent[]
27
+ /** SwiftUI presentation style. `graphical` renders an inline calendar. @default 'automatic' */
28
+ pickerStyle?: SwiftUIDatePickerStyle
29
+ /** Disables interaction with the picker. */
30
+ disabled?: boolean
31
+ /** Spoken name for assistive tech. */
32
+ accessibilityLabel?: string
33
+ /** Called when the user picks a new date; programmatic `selection` updates do not emit. */
34
+ onSelectionChange?: (date: Date) => void
35
35
  }
@@ -1,6 +1,6 @@
1
1
  export { AppKitDatePicker } from '../AppKitDatePicker.macos.tsrx'
2
2
  export type {
3
- AppKitDatePickerComponents,
4
- AppKitDatePickerProps,
5
- AppKitDatePickerStyle,
3
+ AppKitDatePickerComponents,
4
+ AppKitDatePickerProps,
5
+ AppKitDatePickerStyle,
6
6
  } from './types'
@@ -6,28 +6,28 @@ export type AppKitDatePickerStyle = 'textField' | 'graphical'
6
6
 
7
7
  /** macOS date-picker props (NSDatePicker). */
8
8
  export interface AppKitDatePickerProps {
9
- /** Element id passed through to the host element. */
10
- id?: string
11
- /** Class list applied to the picker element. */
12
- className?: any
13
- /** Inline style (string CSS or object). */
14
- style?: any
15
- /** Which element groups the NSDatePicker shows. @default 'date' */
16
- components?: AppKitDatePickerComponents
17
- /** 'graphical' embeds the inline calendar (date mode only). @default 'textField' */
18
- pickerStyle?: AppKitDatePickerStyle
19
- /** Controlled selection. Pair with `onSelectionChange`. */
20
- selection?: Date
21
- /** Initial uncontrolled seed. Later defaults do not reset the mounted picker. */
22
- defaultSelection?: Date
23
- /** Earliest selectable date. */
24
- minimumDate?: Date
25
- /** Latest selectable date. */
26
- maximumDate?: Date
27
- /** Disables interaction with the picker. */
28
- disabled?: boolean
29
- /** Spoken name for assistive tech. */
30
- accessibilityLabel?: string
31
- /** User selection changes; programmatic updates and retired controls do not emit. */
32
- onSelectionChange?: (date: Date) => void
9
+ /** Element id passed through to the host element. */
10
+ id?: string
11
+ /** Class list applied to the picker element. */
12
+ className?: any
13
+ /** Inline style (string CSS or object). */
14
+ style?: any
15
+ /** Which element groups the NSDatePicker shows. @default 'date' */
16
+ components?: AppKitDatePickerComponents
17
+ /** 'graphical' embeds the inline calendar (date mode only). @default 'textField' */
18
+ pickerStyle?: AppKitDatePickerStyle
19
+ /** Controlled selection. Pair with `onSelectionChange`. */
20
+ selection?: Date
21
+ /** Initial uncontrolled seed. Later defaults do not reset the mounted picker. */
22
+ defaultSelection?: Date
23
+ /** Earliest selectable date. */
24
+ minimumDate?: Date
25
+ /** Latest selectable date. */
26
+ maximumDate?: Date
27
+ /** Disables interaction with the picker. */
28
+ disabled?: boolean
29
+ /** Spoken name for assistive tech. */
30
+ accessibilityLabel?: string
31
+ /** User selection changes; programmatic updates and retired controls do not emit. */
32
+ onSelectionChange?: (date: Date) => void
33
33
  }