Skip to content

Commit

Permalink
Add a "cancel" button that cancels the form instead of using the back…
Browse files Browse the repository at this point in the history
… button to clear the entire element.
  • Loading branch information
sbruens committed Sep 25, 2023
1 parent 1442f32 commit cf3b6a9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 30 deletions.
10 changes: 0 additions & 10 deletions src/www/ui_components/app-root.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,16 +785,6 @@ export class AppRoot extends mixinBehaviors([AppLocalizeBehavior], PolymerElemen
history.back();
// Must fire 'location-changed' so app-location notices and updates the route state.
window.dispatchEvent(new CustomEvent('location-changed'));
if (this.page == 'contact') {
// Reset the contact form when a user navigates away from it by replacing
// the element with a fresh one. This is less complex than trying to reset
// all chosen options manually.
const newContactView = document.createElement('contact-view');
newContactView.name = 'contact';
newContactView.id = 'contactView';
newContactView.localize = this.localize;
this.$$('contact-view').replaceWith(newContactView);
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/www/views/contact_view/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ describe('ContactView', () => {
const exitCard = el.shadowRoot!.querySelector('outline-card')!;
expect(exitCard.textContent).toContain('Thanks for helping us improve');
});

it('shows default contact view on cancellation of support form', async () => {
el.shadowRoot!.querySelector('support-form')!.dispatchEvent(new CustomEvent('cancel'));

await nextFrame();

expect(el.shadowRoot?.querySelector('p')?.textContent).toContain('Tell us how we can help.');
expect(el.shadowRoot?.querySelector('support-form')).toBeNull();
});
});
});
});
3 changes: 2 additions & 1 deletion src/www/views/contact_view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class ContactView extends LitElement {
}
}

private submitForm(e: SubmitEvent) {
private submitForm(e: CustomEvent) {
console.log('Feedback form submitted!', e);
this.exitTemplate = html`
Thanks for helping us improve! We love hearing from you.
Expand All @@ -201,6 +201,7 @@ export class ContactView extends LitElement {
<support-form
.variant=${this.variant}
.issueType=${this.selectedIssueType}
@cancel=${() => (this.step = Step.ISSUE_WIZARD)}
@submit=${this.submitForm}
></support-form>
`;
Expand Down
10 changes: 10 additions & 0 deletions src/www/views/contact_view/support_form/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,15 @@ describe('SupportForm', () => {
const {detail} = await listener;
expect(detail).toBeTrue();
});

it('clicking cancel button emits form cancel event', async () => {
const listener = oneEvent(el, 'cancel');

const cancelButton: HTMLElement = el.shadowRoot!.querySelector('mwc-button[label="Cancel"]')!;
cancelButton.click();

const {detail} = await listener;
expect(detail).toBeTrue();
});
});
});
52 changes: 33 additions & 19 deletions src/www/views/contact_view/support_form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ export class SupportForm extends LitElement {
this.isFormValid = Array.from(fieldNodes).every(field => field.validity.valid);
}

/** Resets the form. */
private reset() {
this.formRef.value.reset();
}

/** Cancels the form. */
private cancel() {
this.reset();
const event = new CustomEvent<boolean>('cancel', {detail: true});
this.dispatchEvent(event);
}

/** Submits the form. */
private submit(e: SubmitEvent) {
e.preventDefault();
Expand All @@ -91,7 +103,7 @@ export class SupportForm extends LitElement {

const event = new CustomEvent<boolean>('submit', {detail: true});
this.dispatchEvent(event);
this.formRef.value.reset();
this.reset();
this.isSubmitting = false;
}

Expand Down Expand Up @@ -144,18 +156,18 @@ export class SupportForm extends LitElement {
if (this.variant !== AppType.CLIENT) return nothing;

return html`
<mwc-textfield
name="Where_did_you_get_your_access_key"
label="Source"
helper="Where did you get your access key?"
helperPersistent
maxLength="225"
.disabled="${this.isSubmitting}"
required
outlined
@blur=${this.checkFormValidity}
></mwc-textfield>
`;
<mwc-textfield
name="Where_did_you_get_your_access_key"
label="Source"
helper="Where did you get your access key?"
helperPersistent
maxLength="225"
.disabled="${this.isSubmitting}"
required
outlined
@blur=${this.checkFormValidity}
></mwc-textfield>
`;
}

render() {
Expand Down Expand Up @@ -210,12 +222,14 @@ export class SupportForm extends LitElement {
${this.renderProgressBar}
<mwc-button
label="Submit"
slot="card-actions"
.disabled="${!this.isFormValid || this.isSubmitting}"
@click=${this.submit}
></mwc-button>
<span slot="card-actions">
<mwc-button label="Cancel" .disabled="${this.isSubmitting}" @click=${this.cancel}></mwc-button>
<mwc-button
label="Submit"
.disabled="${!this.isFormValid || this.isSubmitting}"
@click=${this.submit}
></mwc-button>
</span>
</outline-card>
</form>
`;
Expand Down

0 comments on commit cf3b6a9

Please sign in to comment.