Skip to content

Commit 81d0f3b

Browse files
author
amandaesmith3
committed
get valueChanged working for arrays
1 parent 60b5a30 commit 81d0f3b

File tree

4 files changed

+64
-33
lines changed

4 files changed

+64
-33
lines changed

core/src/components/datetime/datetime.tsx

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -333,32 +333,40 @@ export class Datetime implements ComponentInterface {
333333
* This allows us to update the current value's date/time display without
334334
* refocusing or shifting the user's display (leaves the user in place).
335335
*/
336-
// TODO: most of this will need a decent restructure
337336
const valueDateParts = parseDate(this.value);
338337
if (valueDateParts) {
339338
warnIfValueOutOfBounds(valueDateParts, this.minParts, this.maxParts);
340339

341-
const { month, day, year, hour, minute } = valueDateParts;
342-
const ampm = hour >= 12 ? 'pm' : 'am';
343-
344-
this.activePartsClone = {
345-
...this.activeParts,
346-
month,
347-
day,
348-
year,
349-
hour,
350-
minute,
351-
ampm,
352-
};
340+
if(Array.isArray(valueDateParts)) {
341+
this.activePartsClone = [...valueDateParts];
342+
} else {
343+
const { month, day, year, hour, minute } = valueDateParts;
344+
const ampm = hour ?
345+
hour >= 12 ? 'pm' : 'am'
346+
: undefined;
347+
348+
this.activePartsClone = {
349+
...this.activeParts,
350+
month,
351+
day,
352+
year,
353+
hour,
354+
minute,
355+
ampm,
356+
};
353357

354-
/**
355-
* The working parts am/pm value must be updated when the value changes, to
356-
* ensure the time picker hour column values are generated correctly.
357-
*/
358-
this.setWorkingParts({
359-
...this.workingParts,
360-
ampm,
361-
});
358+
/**
359+
* The working parts am/pm value must be updated when the value changes, to
360+
* ensure the time picker hour column values are generated correctly.
361+
*
362+
* Note that we don't need to do this if valueDateParts is an array, since
363+
* multiple="true" does not apply to time pickers.
364+
*/
365+
this.setWorkingParts({
366+
...this.workingParts,
367+
ampm,
368+
});
369+
}
362370
} else {
363371
printIonWarning(`Unable to parse date string: ${this.value}. Please provide a valid ISO 8601 datetime string.`);
364372
}
@@ -551,10 +559,19 @@ export class Datetime implements ComponentInterface {
551559
};
552560

553561
private setActiveParts = (parts: DatetimeParts, removeDate = false) => {
554-
const { multiple, activeParts, highlightActiveParts } = this;
562+
const { multiple, activePartsClone, highlightActiveParts } = this;
555563

556564
if(multiple) {
557-
const activePartsArray = Array.isArray(activeParts) ? activeParts : [activeParts];
565+
/**
566+
* We read from activePartsClone here because valueChanged() only updates that,
567+
* so it's the more reliable source of truth. If we read from activeParts, then
568+
* if you click July 1, manually set the value to July 2, and then click July 3,
569+
* the new value would be [July 1, July 3], ignoring the value set.
570+
*
571+
* We can then pass the new value to activeParts (rather than activePartsClone)
572+
* since the clone will be updated automatically by activePartsChanged().
573+
*/
574+
const activePartsArray = Array.isArray(activePartsClone) ? activePartsClone : [activePartsClone];
558575
if(removeDate) {
559576
this.activeParts = activePartsArray.filter(p => !isSameDay(p, parts));
560577
} else if(highlightActiveParts) {

core/src/components/datetime/test/multiple/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
<h2>No Default Value</h2>
4646
<ion-datetime presentation="date" multiple="true"></ion-datetime>
4747
</div>
48+
<div class="grid-item">
49+
<h2>Single Default Value</h2>
50+
<ion-datetime presentation="date" multiple="true" value="2022-07-01"></ion-datetime>
51+
</div>
4852
</div>
4953
</ion-content>
5054
</ion-app>

core/src/components/datetime/utils/comparison.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,23 @@ export const isAfter = (baseParts: DatetimeParts, compareParts: DatetimeParts) =
3939
);
4040
};
4141

42-
export const warnIfValueOutOfBounds = (value: DatetimeParts, min: DatetimeParts, max: DatetimeParts) => {
43-
if ((min && isBefore(value, min)) || (max && isAfter(value, max))) {
44-
printIonWarning(
45-
'The value provided to ion-datetime is out of bounds.\n\n' +
46-
`Min: ${JSON.stringify(min)}\n` +
47-
`Max: ${JSON.stringify(max)}\n` +
48-
`Value: ${JSON.stringify(value)}`
49-
);
42+
export const warnIfValueOutOfBounds = (value: DatetimeParts | DatetimeParts[], min: DatetimeParts, max: DatetimeParts) => {
43+
const isOutOfBounds = (value: DatetimeParts) => (min && isBefore(value, min)) || (max && isAfter(value, max));
44+
const printWarning = () => printIonWarning(
45+
'The value provided to ion-datetime is out of bounds.\n\n' +
46+
`Min: ${JSON.stringify(min)}\n` +
47+
`Max: ${JSON.stringify(max)}\n` +
48+
`Value: ${JSON.stringify(value)}`
49+
);
50+
51+
if(Array.isArray(value)) {
52+
for(const val of value) {
53+
if (isOutOfBounds(val)) {
54+
printWarning();
55+
break;
56+
}
57+
}
58+
} else if(isOutOfBounds(value)) {
59+
printWarning();
5060
}
5161
};

packages/vue/src/vue-component-lib/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { VNode, defineComponent, getCurrentInstance, h, inject, ref, Ref } from 'vue';
22

3-
export interface InputProps {
4-
modelValue?: string | boolean;
3+
export interface InputProps extends Object {
4+
modelValue: string | boolean;
55
}
66

77
const UPDATE_VALUE_EVENT = 'update:modelValue';

0 commit comments

Comments
 (0)