-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Polyfill reportValidation calls and bubble UI for webkit. #3734
Changes from all commits
26ba3f7
957c812
910fbee
07ed9b0
c04f97b
29bfdc7
ca352f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,8 @@ import {templatesFor} from '../../../src/template'; | |
import {removeElement, childElementByAttr} from '../../../src/dom'; | ||
import {installStyles} from '../../../src/styles'; | ||
import {CSS} from '../../../build/amp-form-0.1.css'; | ||
import {ValidationBubble} from './validation-bubble'; | ||
import {vsyncFor} from '../../../src/vsync'; | ||
|
||
/** @type {string} */ | ||
const TAG = 'amp-form'; | ||
|
@@ -37,6 +39,9 @@ const FormState_ = { | |
SUBMIT_SUCCESS: 'submit-success', | ||
}; | ||
|
||
/** @type {?./validation-bubble.ValidationBubble|undefined} */ | ||
let validationBubble; | ||
|
||
export class AmpForm { | ||
|
||
/** | ||
|
@@ -50,6 +55,9 @@ export class AmpForm { | |
/** @const @private {!Element} */ | ||
this.form_ = element; | ||
|
||
/** @const @private {!../../../src/service/vsync-impl.Vsync} */ | ||
this.vsync_ = vsyncFor(this.win_); | ||
|
||
/** @const @private {!Templates} */ | ||
this.templates_ = templatesFor(this.win_); | ||
|
||
|
@@ -94,12 +102,20 @@ export class AmpForm { | |
* @private | ||
*/ | ||
handleSubmit_(e) { | ||
if (e.defaultPrevented) { | ||
if (this.state_ == FormState_.SUBMITTING) { | ||
e.preventDefault(); | ||
return; | ||
} | ||
|
||
if (this.state_ == FormState_.SUBMITTING) { | ||
const shouldValidate = !this.form_.hasAttribute('novalidate'); | ||
if (shouldValidate && | ||
this.form_.checkValidity && !this.form_.checkValidity()) { | ||
e.preventDefault(); | ||
// TODO(#3776): Use .mutate method when it supports passing state. | ||
this.vsync_.run({ | ||
measure: undefined, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a TODO #3776, please. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
mutate: reportValidity, | ||
}, {form: this.form_}); | ||
return; | ||
} | ||
|
||
|
@@ -146,7 +162,7 @@ export class AmpForm { | |
} | ||
|
||
/** | ||
* @param {!Object} data | ||
* @param {!Object=} data | ||
* @private | ||
*/ | ||
renderTemplate_(data = {}) { | ||
|
@@ -176,6 +192,73 @@ export class AmpForm { | |
} | ||
|
||
|
||
/** | ||
* Reports validity of the form passed through state object. | ||
* @param {!Object} state | ||
*/ | ||
function reportValidity(state) { | ||
reportFormValidity(state.form); | ||
} | ||
|
||
|
||
/** | ||
* Reports validity for the first invalid input - if any. | ||
* @param {!HTMLFormElement} form | ||
*/ | ||
function reportFormValidity(form) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming here is odd, since we're actually reporting invalidity. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The native API is called reportValidity. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack. |
||
const inputs = form.querySelectorAll('input,select,textarea'); | ||
for (let i = 0; i < inputs.length; i++) { | ||
if (!inputs[i].checkValidity()) { | ||
reportInputValidity(inputs[i]); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Revalidates the currently focused input after a change. | ||
* @param {!KeyboardEvent} event | ||
*/ | ||
function onInvalidInputKeyUp_(event) { | ||
if (event.target.checkValidity()) { | ||
validationBubble.hide(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One last: We may end up double calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah we'd also need to store the targetElement and visibility state of the bubble in that state to make sure we're calling show for the same element as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Filed a bug to track this will do in another PR #3782 |
||
} else { | ||
validationBubble.show(event.target, event.target.validationMessage); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Hides validation bubble and removes listeners on the invalid input. | ||
* @param {!Event} event | ||
*/ | ||
function onInvalidInputBlur_(event) { | ||
validationBubble.hide(); | ||
event.target.removeEventListener('blur', onInvalidInputBlur_); | ||
event.target.removeEventListener('keyup', onInvalidInputKeyUp_); | ||
} | ||
|
||
|
||
/** | ||
* Focuses and reports the invalid message of the input in a message bubble. | ||
* @param {!HTMLInputElement} input | ||
*/ | ||
function reportInputValidity(input) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto naming. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack. |
||
input./*OK*/focus(); | ||
|
||
// Remove any previous listeners on the same input. This avoids adding many | ||
// listeners on the same element when the user submit pressing Enter or any | ||
// other method to submit the form without the element losing focus. | ||
input.removeEventListener('blur', onInvalidInputBlur_); | ||
input.removeEventListener('keyup', onInvalidInputKeyUp_); | ||
|
||
input.addEventListener('keyup', onInvalidInputKeyUp_); | ||
input.addEventListener('blur', onInvalidInputBlur_); | ||
validationBubble.show(input, input.validationMessage); | ||
} | ||
|
||
|
||
/** | ||
* Installs submission handler on all forms in the document. | ||
* @param {!Window} win | ||
|
@@ -189,10 +272,17 @@ function installSubmissionHandlers(win) { | |
} | ||
|
||
|
||
function installAmpForm(win) { | ||
/** | ||
* @param {!Window} win | ||
* @private visible for testing. | ||
*/ | ||
export function installAmpForm(win) { | ||
return getService(win, 'amp-form', () => { | ||
if (isExperimentOn(win, TAG)) { | ||
installStyles(win.document, CSS, () => installSubmissionHandlers(win)); | ||
installStyles(win.document, CSS, () => { | ||
validationBubble = new ValidationBubble(win); | ||
installSubmissionHandlers(win); | ||
}); | ||
} | ||
return {}; | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add undefined to allowed type if we dont init with null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done