Skip to content
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
26 changes: 26 additions & 0 deletions src/vscode-textfield/vscode-textfield.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,30 @@ describe('vscode-textfield', () => {

expect(el.checkValidity()).to.eq(false);
});

it('mirrors a non-empty value into a file input and triggers unhandledrejection (current bug)', async () => {
const el = await fixture<VscodeTextfield>(
html`<vscode-textfield type="file"></vscode-textfield>`
);

const rejection = new Promise<unknown>((resolve) => {
const handler = (ev: PromiseRejectionEvent) => {
window.removeEventListener('unhandledrejection', handler);
resolve(ev.reason);
};
window.addEventListener('unhandledrejection', handler);
});

// mimic the behavior of a file input
el['_value'] = 'C:\\fakepath\\file.txt';

el.requestUpdate();

const result = await Promise.race([
rejection,
el.updateComplete.then(() => 'update-completed'),
]);

expect(result).to.equal('update-completed');
});
});
95 changes: 63 additions & 32 deletions src/vscode-textfield/vscode-textfield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,17 @@ export class VscodeTextfield
private _internals: ElementInternals;

private _dataChanged() {
this._value = this._inputEl.value;

if (this.type === 'file' && this._inputEl.files) {
for (const f of this._inputEl.files) {
this._internals.setFormValue(f);
if (this.type === 'file') {
if (this._inputEl.files) {
for (const f of this._inputEl.files) {
this._internals.setFormValue(f);
}
}
} else {
this._internals.setFormValue(this._inputEl.value);
return;
}

this._value = this._inputEl.value;
this._internals.setFormValue(this._inputEl.value);
}

private _setValidityFromInput() {
Expand Down Expand Up @@ -354,31 +356,60 @@ export class VscodeTextfield
return html`
<div class="root">
<slot name="content-before"></slot>
<input
id="input"
type=${this.type}
?autofocus=${this.autofocus}
autocomplete=${ifDefined(this.autocomplete)}
aria-label=${this.label}
?disabled=${this.disabled}
max=${ifDefined(this.max)}
maxlength=${ifDefined(this.maxLength)}
min=${ifDefined(this.min)}
minlength=${ifDefined(this.minLength)}
?multiple=${this.multiple}
name=${ifDefined(this.name)}
pattern=${ifDefined(this.pattern)}
placeholder=${ifDefined(this.placeholder)}
?readonly=${this.readonly}
?required=${this.required}
step=${ifDefined(this.step)}
.value=${this._value}
@blur=${this._onBlur}
@change=${this._onChange}
@focus=${this._onFocus}
@input=${this._onInput}
@keydown=${this._onKeyDown}
>
${this._type === 'file'
? html`
<input
id="input"
type="file"
?autofocus=${this.autofocus}
autocomplete=${ifDefined(this.autocomplete)}
aria-label=${this.label}
?disabled=${this.disabled}
max=${ifDefined(this.max)}
maxlength=${ifDefined(this.maxLength)}
min=${ifDefined(this.min)}
minlength=${ifDefined(this.minLength)}
?multiple=${this.multiple}
name=${ifDefined(this.name)}
pattern=${ifDefined(this.pattern)}
placeholder=${ifDefined(this.placeholder)}
?readonly=${this.readonly}
?required=${this.required}
step=${ifDefined(this.step)}
@blur=${this._onBlur}
@change=${this._onChange}
@focus=${this._onFocus}
@input=${this._onInput}
@keydown=${this._onKeyDown}
>
`
: html`
<input
id="input"
type=${this.type}
?autofocus=${this.autofocus}
autocomplete=${ifDefined(this.autocomplete)}
aria-label=${this.label}
?disabled=${this.disabled}
max=${ifDefined(this.max)}
maxlength=${ifDefined(this.maxLength)}
min=${ifDefined(this.min)}
minlength=${ifDefined(this.minLength)}
?multiple=${this.multiple}
name=${ifDefined(this.name)}
pattern=${ifDefined(this.pattern)}
placeholder=${ifDefined(this.placeholder)}
?readonly=${this.readonly}
?required=${this.required}
step=${ifDefined(this.step)}
.value=${this._value}
@blur=${this._onBlur}
@change=${this._onChange}
@focus=${this._onFocus}
@input=${this._onInput}
@keydown=${this._onKeyDown}
>
`}
<slot name="content-after"></slot>
</div>
`;
Expand Down
Loading