Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bind:value as Date support for <input type=date> tags #3494

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/compile/render_dom/wrappers/Element/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ function get_value_from_dom(
return `@to_number(this.${name})`;
}

if (type === 'date') {
return `@value_as_date(this.${name})`;
}

if ((name === 'buffered' || name === 'seekable' || name === 'played')) {
return `@time_ranges_to_array(this.${name})`;
}
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/compile/render_ssr/handlers/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
node.attributes.some((attribute) => attribute.name === 'contenteditable')
);

const is_date_input = (
node.name === 'input' &&
node.attributes.some((attribute) => attribute.name === 'type' && attribute.get_static_value() === 'date')
);

const slot = node.get_static_attribute_value('slot');
const component = node.find_nearest(/InlineComponent/);
if (slot && component) {
Expand Down Expand Up @@ -162,6 +167,9 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
} else if (binding.name === 'value' && node.name === 'textarea') {
const snippet = snip(expression);
node_contents = '${(' + snippet + ') || ""}';
} else if (binding.name === 'value' && is_date_input) {
const snippet = snip(expression);
opening_tag += '${@add_attribute("' + name + '", @date_as_value(' + snippet + '), 1)}';
} else {
const snippet = snip(expression);
opening_tag += '${@add_attribute("' + name + '", ' + snippet + ', 1)}';
Expand Down
22 changes: 20 additions & 2 deletions src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ export function to_number(value) {
return value === '' ? undefined : +value;
}

export function value_as_date(value) {
const valueAsDate = new Date(value);
return isNaN(valueAsDate.getTime()) ? undefined : valueAsDate;
}

export function date_as_value(date) {
const validDate = Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime());
if (!validDate) {
return '';
}
const yyyy = date.getUTCFullYear();
const mm = date.getUTCMonth() + 1 < 10 ? `0${date.getUTCMonth() + 1}` : date.getUTCMonth() + 1;
const dd = date.getUTCDate() < 10 ? `0${date.getUTCDate()}` : date.getUTCDate();
return `${yyyy}-${mm}-${dd}`;
}

export function time_ranges_to_array(ranges) {
const array = [];
for (let i = 0; i < ranges.length; i += 1) {
Expand Down Expand Up @@ -170,7 +186,9 @@ export function set_data(text, data) {
}

export function set_input_value(input, value) {
if (value != null || input.value) {
if (Object.prototype.toString.call(value) === '[object Date]') {
input.value = date_as_value(value);
} else if (value != null || input.value) {
input.value = value;
}
}
Expand Down Expand Up @@ -291,4 +309,4 @@ export class HtmlTag {
d() {
this.n.forEach(detach);
}
}
}
55 changes: 55 additions & 0 deletions test/runtime/samples/binding-input-date/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const SEP_03_2019_INPUT_VALUE = '2019-09-03';
const SEP_03_2019_DATE_VALUE = new Date(SEP_03_2019_INPUT_VALUE);

const OCT_07_2019_INPUT_VALUE = '2019-10-07';
const OCT_07_2019_DATE_VALUE = new Date(OCT_07_2019_INPUT_VALUE);

export default {
props: {
date: SEP_03_2019_DATE_VALUE
},

html: `
<input type=date>
<p>[object Date] ${SEP_03_2019_DATE_VALUE}</p>
`,

ssrHtml: `
<input type=date value='${SEP_03_2019_INPUT_VALUE}'>
<p>[object Date] ${SEP_03_2019_DATE_VALUE}</p>
`,

async test({ assert, component, target, window }) {
const input = target.querySelector('input');
assert.equal(input.value, SEP_03_2019_INPUT_VALUE);
assert.equal(component.date.toString(), SEP_03_2019_DATE_VALUE.toString());

const event = new window.Event('input');

input.value = OCT_07_2019_INPUT_VALUE;
await input.dispatchEvent(event);

assert.equal(component.date.toString(), OCT_07_2019_DATE_VALUE.toString());
assert.htmlEqual(target.innerHTML, `
<input type='date'>
<p>[object Date] ${OCT_07_2019_DATE_VALUE}</p>
`);

component.date = SEP_03_2019_DATE_VALUE;
assert.equal(input.value, SEP_03_2019_INPUT_VALUE);
assert.htmlEqual(target.innerHTML, `
<input type='date'>
<p>[object Date] ${SEP_03_2019_DATE_VALUE}</p>
`);

// empty string should be treated as undefined
input.value = '';
await input.dispatchEvent(event);

assert.equal(component.date, undefined);
assert.htmlEqual(target.innerHTML, `
<input type='date'>
<p>[object Undefined] undefined</p>
`);
},
};
6 changes: 6 additions & 0 deletions test/runtime/samples/binding-input-date/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
export let date;
</script>

<input type='date' bind:value={date}>
<p>{Object.prototype.toString.call(date)} {date}</p>