Skip to content

Commit

Permalink
fix: Fix Naive DateTime Selector (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
staylorwr committed Nov 13, 2020
1 parent 2b841be commit 226b8c4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
2 changes: 0 additions & 2 deletions assets/src/components/DateTimePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ export default {
enableSeconds: this.enableSeconds,
onClose: this.onChange,
dateFormat: 'Z',
allowInput: true,
altInput: true,
altFormat: this.dateFormat,
time_24hr: !this.twelveHourTime
});
});
Expand Down
25 changes: 6 additions & 19 deletions assets/src/components/Form/DateTime.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<DateTimePicker
:field="field"
:field-name="field.name"
:value="localizedValue"
:value="value"
class="w-full form-control form-input form-input-bordered"
:date-format="pickerFormat"
:placeholder="placeholder"
Expand All @@ -26,8 +26,6 @@ export default {
components: { DateTimePicker },
mixins: [ HandlesValidationErrors, FormField ],
data: () => ({ localizedValue: '' }),
computed: {
format () {
return this.field.options.format || DateTime.DATETIME_FULL;
Expand All @@ -44,6 +42,10 @@ export default {
twelveHourTime () {
return !this.field.options.twenty_four_hour_time || true;
},
naiveDateTime () {
return this.field.options.naive_datetime || false;
}
},
Expand All @@ -52,33 +54,18 @@ export default {
this.value = this.field.value || '';
if (this.value !== '') {
this.localizedValue = this.fromUTC(this.value);
return;
}
if (this.naiveDateTime) {
this.localizedValue = this.value;
}
},
handleChange (value) {
if (this.naiveDateTime) {
const now = DateTime.local();
const dt = DateTime.fromISO(value).setZone(now.zoneName, {
keepLocalTime: true
});
const dt = DateTime.fromISO(value).setZone(now.zoneName);
this.value = `${dt.toFormat('yyyy-M-dd')}T${dt.toFormat('TT')}`;
this.localizedValue = dt.toLocaleString(DateTime.DATETIME_MED);
return;
}
this.value = value;
this.localizedValue = this.fromUTC(this.value);
},
fromUTC (value) {
return DateTime.fromISO(value).toLocaleString(DateTime.DATETIME_FULL);
}
}
};
Expand Down
6 changes: 5 additions & 1 deletion lib/ex_teal/fields/date_time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ defmodule ExTeal.Fields.DateTime do
nil

%NaiveDateTime{} = naive ->
naive
if Map.get(field.options, :naive_datetime) do
naive
else
DateTime.from_naive!(naive, "Etc/UTC")
end

val ->
val
Expand Down

0 comments on commit 226b8c4

Please sign in to comment.