1
1
import { UmbPropertyValueChangeEvent } from '@umbraco-cms/backoffice/property-editor' ;
2
2
import type { UmbPropertyEditorConfigCollection } from '@umbraco-cms/backoffice/property-editor' ;
3
- import { html , customElement , property , state , ifDefined } from '@umbraco-cms/backoffice/external/lit' ;
3
+ import { html , customElement , property , state } from '@umbraco-cms/backoffice/external/lit' ;
4
4
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element' ;
5
5
import type { UmbInputDateElement } from '@umbraco-cms/backoffice/components' ;
6
6
import type { UmbPropertyEditorUiElement } from '@umbraco-cms/backoffice/extension-registry' ;
@@ -42,13 +42,16 @@ export class UmbPropertyEditorUIDatePickerElement extends UmbLitElement implemen
42
42
@property ( )
43
43
value ?: string ;
44
44
45
+ @state ( )
46
+ private _inputValue ?: string ;
47
+
45
48
public set config ( config : UmbPropertyEditorConfigCollection | undefined ) {
46
49
if ( ! config ) return ;
47
50
48
51
// Format string prevalue/config
49
52
const format = config . getValueByAlias < string > ( 'format' ) ;
50
- const hasTime = format ?. includes ( 'H' ) || format ?. includes ( 'm' ) ;
51
- const hasSeconds = format ?. includes ( 's' ) ;
53
+ const hasTime = ( format ?. includes ( 'H' ) || format ?. includes ( 'm' ) ) ?? false ;
54
+ const hasSeconds = format ?. includes ( 's' ) ?? false ;
52
55
this . _inputType = hasTime ? 'datetime-local' : 'date' ;
53
56
54
57
// Based on the type of format string change the UUI-input type
@@ -70,33 +73,54 @@ export class UmbPropertyEditorUIDatePickerElement extends UmbLitElement implemen
70
73
}
71
74
72
75
#onChange( event : CustomEvent & { target : UmbInputDateElement } ) {
73
- this . #formatValue( event . target . value . toString ( ) ) ;
76
+ let value = event . target . value . toString ( ) ;
77
+
78
+ switch ( this . _inputType ) {
79
+ case 'time' :
80
+ value = `0001-01-01 ${ value } ` ;
81
+ break ;
82
+ case 'date' :
83
+ value = `${ value } 00:00:00` ;
84
+ break ;
85
+ case 'datetime-local' :
86
+ value = value . replace ( 'T' , ' ' ) ;
87
+ break ;
88
+ }
89
+
90
+ this . #syncValue( value ) ;
74
91
}
75
92
76
93
/**
77
94
* Formats the value depending on the input type.
78
95
*/
79
96
#formatValue( value : string ) {
80
- // Check that the value is a valid date
81
- const valueToDate = new Date ( value ) ;
82
- if ( isNaN ( valueToDate . getTime ( ) ) ) {
83
- console . warn ( '[Umbraco.DatePicker] The value is not a valid date.' , value ) ;
97
+ this . _inputValue = undefined ;
98
+
99
+ if ( isNaN ( new Date ( value ) . getTime ( ) ) ) {
100
+ console . warn ( `[UmbDatePicker] Invalid date: ${ value } ` ) ;
84
101
return ;
85
102
}
86
103
87
- // Replace the potential time demoninator 'T' with a whitespace for backwards compatibility
88
- value = value . replace ( 'T' , ' ' ) ;
89
-
90
- // If the inputType is 'date', we need to make sure the value doesn't have a time
91
- if ( this . _inputType === 'date' && value . includes ( ' ' ) ) {
92
- value = value . split ( ' ' ) [ 0 ] ;
104
+ const dateSplit = value . split ( ' ' ) ;
105
+ if ( dateSplit . length !== 2 ) {
106
+ console . warn ( `[UmbDatePicker] Invalid date: ${ value } ` ) ;
107
+ return ;
93
108
}
94
109
95
- // If the inputType is 'time', we need to remove the date part of the value
96
- if ( this . _inputType === 'time' && value . includes ( ' ' ) ) {
97
- value = value . split ( ' ' ) [ 1 ] ;
110
+ switch ( this . _inputType ) {
111
+ case 'time' :
112
+ this . _inputValue = dateSplit [ 1 ] ;
113
+ break ;
114
+ case 'date' :
115
+ this . _inputValue = dateSplit [ 0 ] ;
116
+ break ;
117
+ default :
118
+ this . _inputValue = dateSplit . join ( 'T' ) ;
119
+ break ;
98
120
}
121
+ }
99
122
123
+ #syncValue( value : string ) {
100
124
const valueHasChanged = this . value !== value ;
101
125
if ( valueHasChanged ) {
102
126
this . value = value ;
@@ -107,7 +131,7 @@ export class UmbPropertyEditorUIDatePickerElement extends UmbLitElement implemen
107
131
override render ( ) {
108
132
return html `
109
133
< umb-input-date
110
- value =" ${ ifDefined ( this . value ) } "
134
+ . value =${ this . _inputValue }
111
135
.min =${ this . _min }
112
136
.max=${ this . _max }
113
137
.step=${ this . _step }
0 commit comments