fix: keep current selected date/number value when reopening browser

This commit is contained in:
Yohan Boniface 2024-05-07 16:39:28 +02:00
parent 8ef8ad538e
commit fa1752c992
2 changed files with 25 additions and 16 deletions

View file

@ -521,8 +521,6 @@ U.FeatureMixin = {
case 'date': case 'date':
case 'datetime': case 'datetime':
case 'number': case 'number':
min = parser(min)
max = parser(max)
if (!isNaN(min) && !isNaN(value) && min > value) return false if (!isNaN(min) && !isNaN(value) && min > value) return false
if (!isNaN(max) && !isNaN(value) && max < value) return false if (!isNaN(max) && !isNaN(value) && max < value) return false
break break

View file

@ -796,7 +796,7 @@ L.FormBuilder.MinMaxBase = L.FormBuilder.Element.extend({
return [L._('Min'), L._('Max')] return [L._('Min'), L._('Max')]
}, },
castValue: function (value) { prepareForHTML: function (value) {
return value.valueOf() return value.valueOf()
}, },
@ -804,6 +804,10 @@ L.FormBuilder.MinMaxBase = L.FormBuilder.Element.extend({
this.container = L.DomUtil.create('fieldset', 'umap-facet', this.parentNode) this.container = L.DomUtil.create('fieldset', 'umap-facet', this.parentNode)
this.container.appendChild(this.label) this.container.appendChild(this.label)
const { min, max, type } = this.options.criteria const { min, max, type } = this.options.criteria
const { min: modifiedMin, max: modifiedMax } = this.get()
const currentMin = modifiedMin !== undefined ? modifiedMin : min
const currentMax = modifiedMax !== undefined ? modifiedMax : max
this.type = type this.type = type
this.inputType = this.getInputType(this.type) this.inputType = this.getInputType(this.type)
@ -816,7 +820,7 @@ L.FormBuilder.MinMaxBase = L.FormBuilder.Element.extend({
this.minInput.type = this.inputType this.minInput.type = this.inputType
this.minInput.step = 'any' this.minInput.step = 'any'
if (min != null) { if (min != null) {
this.minInput.valueAsNumber = this.castValue(min) this.minInput.valueAsNumber = this.prepareForHTML(currentMin)
this.minInput.dataset.value = min this.minInput.dataset.value = min
} }
@ -827,7 +831,7 @@ L.FormBuilder.MinMaxBase = L.FormBuilder.Element.extend({
this.maxInput.type = this.inputType this.maxInput.type = this.inputType
this.maxInput.step = 'any' this.maxInput.step = 'any'
if (max != null) { if (max != null) {
this.maxInput.valueAsNumber = this.castValue(max) this.maxInput.valueAsNumber = this.prepareForHTML(currentMax)
this.maxInput.dataset.value = max this.maxInput.dataset.value = max
} }
@ -842,21 +846,27 @@ L.FormBuilder.MinMaxBase = L.FormBuilder.Element.extend({
}) })
}, },
isMinModified: function () {
return this.minInput.value !== this.minInput.dataset.value
},
isMaxModified: function () {
return this.maxInput.value !== this.maxInput.dataset.value
},
isModified: function () {
return this.isMinModified() || this.isMaxModified()
},
toJS: function () { toJS: function () {
const opts = { const opts = {
type: this.type, type: this.type,
} }
if ( if (this.minInput.value !== '' && this.isMinModified()) {
this.minInput.value !== '' && opts.min = new Date(this.minInput.value)
this.minInput.value !== this.minInput.dataset.value
) {
opts.min = this.minInput.value
} }
if ( if (this.maxInput.value !== '' && this.isMaxModified()) {
this.maxInput.value !== '' && opts.max = new Date(this.maxInput.value)
this.maxInput.value !== this.maxInput.dataset.value
) {
opts.max = this.maxInput.value
} }
return opts return opts
}, },
@ -865,7 +875,8 @@ L.FormBuilder.MinMaxBase = L.FormBuilder.Element.extend({
L.FormBuilder.FacetSearchNumber = L.FormBuilder.MinMaxBase.extend({}) L.FormBuilder.FacetSearchNumber = L.FormBuilder.MinMaxBase.extend({})
L.FormBuilder.FacetSearchDate = L.FormBuilder.MinMaxBase.extend({ L.FormBuilder.FacetSearchDate = L.FormBuilder.MinMaxBase.extend({
castValue: function (value) { prepareForHTML: function (value) {
// Deal with timezone
return value.valueOf() - value.getTimezoneOffset() * 60000 return value.valueOf() - value.getTimezoneOffset() * 60000
}, },
getLabels: function () { getLabels: function () {