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

✨ FEATURE: AMP form dirtiness indicator class #22640

Merged
merged 9 commits into from Jun 4, 2019

Conversation

xrav3nz
Copy link
Contributor

@xrav3nz xrav3nz commented Jun 3, 2019

This currently supports detecting the dirtiness of text-typed <input>
and <textarea>. This does not take the submitted value into account
yet.

This is part of #22534.

/cc @GoTcWang @cvializ

This currently supports detecting the dirtiness of text-typed `<input>`
and `<textarea>`. This does not take the submitted value into account
yet.
Copy link
Contributor

@GoTcWang GoTcWang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only looked at implementation

extensions/amp-form/0.1/form-dirtiness.js Show resolved Hide resolved
this.dirtyFieldCount_ = 0;

/** @private {!Object<string, boolean>} */
this.isFieldNameDirty_ = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need a map rather than a Set()?
In that way we can also get rid of the counter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right I should've left a comment. Set is not allowed here

requirement: {
type: BANNED_NAME
error_message: 'Set is not allowed'
value: 'Set'
}

I think it's probably due to compatibility (#6551). I don't think we need to polyfill since its effects can be emulated with an object.

extensions/amp-form/0.1/form-dirtiness.js Outdated Show resolved Hide resolved
extensions/amp-form/0.1/form-dirtiness.js Outdated Show resolved Hide resolved
extensions/amp-form/0.1/form-dirtiness.js Show resolved Hide resolved
this.dirtyFieldCount_ = 0;

/** @private {!Object<string, boolean>} */
this.isFieldNameDirty_ = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the src/object.js#map type here instead of object literals. It creates a new prototypeless object, which decreases the cost of property lookups under the hood.

this.isFieldNameDirty_ = map();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL {} vs Object.create thanks!

extensions/amp-form/0.1/form-dirtiness.js Outdated Show resolved Hide resolved
* @private
*/
updateDirtinessClass_() {
if (this.dirtyFieldCount_ == 0 || this.isSubmitting_) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified into

const isDirty = this.dirtyFieldCount_ > 0 && !this.isSubmitting_;
this.form_.classList.toggle(DIRTINESS_INDICATOR_CLASS, isDirty);

It creates a new prototypeless object, which decreases the cost of
property lookups under the hood.
Copy link
Contributor

@GoTcWang GoTcWang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good tests, LGTM!

extensions/amp-form/0.1/form-dirtiness.js Show resolved Hide resolved
});
});

describe('#onSubmitting', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, what does this # stands for, the method name?

Copy link
Contributor Author

@xrav3nz xrav3nz Jun 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh an old habit. # denotes instance methods while . or :: are for static methods. I couldn't remember/find the source of this convention tho. Just grep'ed the AMP code base -

Copy link
Contributor

@cvializ cvializ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, let me know when you're ready to merge

@xrav3nz
Copy link
Contributor Author

xrav3nz commented Jun 4, 2019

@cvializ ready now, thanks!

@cvializ cvializ merged commit ce9bfba into ampproject:master Jun 4, 2019
@xrav3nz xrav3nz deleted the feature/amp-form-dirty branch June 4, 2019 13:42
thekorn pushed a commit to edelight/amphtml that referenced this pull request Sep 11, 2019
* FEATURE: AMP form dirtiness indicator class

This currently supports detecting the dirtiness of text-typed `<input>`
and `<textarea>`. This does not take the submitted value into account
yet.

* remove incorrect `@const` annotation

* do not hook this up yet

* remove by keeping the property instead

* rename 'listeners' to 'handlers'

* move `updateDirtinessClass_` up

* use `utils/object#map` instead of `{}`

It creates a new prototypeless object, which decreases the cost of
property lookups under the hood.

* refactoring for simplicity and readability

* Added description for non-trivial methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants