-
Notifications
You must be signed in to change notification settings - Fork 325
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
[SPIKE] Add JS property observer function; allow for programatic updating of character count #5067
Draft
querkmachine
wants to merge
2
commits into
main
Choose a base branch
from
character-count-value-observer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add an observer to the character count component's `value` property and call the usual counter update functions if it gets changed programatically.
📋 StatsFile sizes
Modules
View stats and visualisations on the review app Action run for 918908b |
JavaScript changes to npm packagediff --git a/packages/govuk-frontend/dist/govuk/govuk-frontend.min.js b/packages/govuk-frontend/dist/govuk/govuk-frontend.min.js
index 754775151..189ae02b3 100644
--- a/packages/govuk-frontend/dist/govuk/govuk-frontend.min.js
+++ b/packages/govuk-frontend/dist/govuk/govuk-frontend.min.js
@@ -445,7 +445,21 @@ class CharacterCount extends GOVUKFrontendComponent {
const h = document.createElement("div");
h.className = "govuk-character-count__sr-status govuk-visually-hidden", h.setAttribute("aria-live", "polite"), this.$screenReaderCountMessage = h, c.insertAdjacentElement("afterend", h);
const u = document.createElement("div");
- u.className = c.className, u.classList.add("govuk-character-count__status"), u.setAttribute("aria-hidden", "true"), this.$visibleCountMessage = u, c.insertAdjacentElement("afterend", u), c.classList.add("govuk-visually-hidden"), this.$textarea.removeAttribute("maxlength"), this.bindChangeEvents(), window.addEventListener("pageshow", (() => this.updateCountMessage())), this.updateCountMessage()
+ u.className = c.className, u.classList.add("govuk-character-count__status"), u.setAttribute("aria-hidden", "true"), this.$visibleCountMessage = u, c.insertAdjacentElement("afterend", u), c.classList.add("govuk-visually-hidden"), this.$textarea.removeAttribute("maxlength"), this.bindChangeEvents(), window.addEventListener("pageshow", (() => this.updateCountMessage())), this.updateCountMessage(),
+ function(e, t, n) {
+ var i;
+ const s = null != (i = Object.getOwnPropertyDescriptor(e, t)) ? i : Object.getOwnPropertyDescriptor(e.constructor.prototype, t);
+ Object.defineProperty(e, t, {
+ set(e) {
+ var t;
+ null == s || null == (t = s.set) || t.call(this, e), n.call(this, e)
+ },
+ get() {
+ var e;
+ return null == s || null == (e = s.get) ? void 0 : e.call(this)
+ }
+ })
+ }(this.$textarea, "value", this.updateCountMessage.bind(this))
}
bindChangeEvents() {
this.$textarea.addEventListener("keyup", (() => this.handleKeyUp())), this.$textarea.addEventListener("focus", (() => this.handleFocus())), this.$textarea.addEventListener("blur", (() => this.handleBlur()))
Action run for 918908b |
Other changes to npm packagediff --git a/packages/govuk-frontend/dist/govuk/all.bundle.js b/packages/govuk-frontend/dist/govuk/all.bundle.js
index fbc644fbf..962f93ebc 100644
--- a/packages/govuk-frontend/dist/govuk/all.bundle.js
+++ b/packages/govuk-frontend/dist/govuk/all.bundle.js
@@ -860,6 +860,22 @@
return $closestElementWithAttribute ? $closestElementWithAttribute.getAttribute(attributeName) : null;
}
+ function observeElementProperty(element, property, callback) {
+ var _Object$getOwnPropert;
+ const nativeProperty = (_Object$getOwnPropert = Object.getOwnPropertyDescriptor(element, property)) != null ? _Object$getOwnPropert : Object.getOwnPropertyDescriptor(element.constructor.prototype, property);
+ Object.defineProperty(element, property, {
+ set(value) {
+ var _nativeProperty$set;
+ nativeProperty == null || (_nativeProperty$set = nativeProperty.set) == null || _nativeProperty$set.call(this, value);
+ callback.call(this, value);
+ },
+ get() {
+ var _nativeProperty$get;
+ return nativeProperty == null || (_nativeProperty$get = nativeProperty.get) == null ? void 0 : _nativeProperty$get.call(this);
+ }
+ });
+ }
+
/**
* Character count component
*
@@ -956,6 +972,7 @@
this.bindChangeEvents();
window.addEventListener('pageshow', () => this.updateCountMessage());
this.updateCountMessage();
+ observeElementProperty(this.$textarea, 'value', this.updateCountMessage.bind(this));
}
bindChangeEvents() {
this.$textarea.addEventListener('keyup', () => this.handleKeyUp());
diff --git a/packages/govuk-frontend/dist/govuk/all.bundle.mjs b/packages/govuk-frontend/dist/govuk/all.bundle.mjs
index 3d03f5f0f..80e2d4168 100644
--- a/packages/govuk-frontend/dist/govuk/all.bundle.mjs
+++ b/packages/govuk-frontend/dist/govuk/all.bundle.mjs
@@ -854,6 +854,22 @@ function closestAttributeValue($element, attributeName) {
return $closestElementWithAttribute ? $closestElementWithAttribute.getAttribute(attributeName) : null;
}
+function observeElementProperty(element, property, callback) {
+ var _Object$getOwnPropert;
+ const nativeProperty = (_Object$getOwnPropert = Object.getOwnPropertyDescriptor(element, property)) != null ? _Object$getOwnPropert : Object.getOwnPropertyDescriptor(element.constructor.prototype, property);
+ Object.defineProperty(element, property, {
+ set(value) {
+ var _nativeProperty$set;
+ nativeProperty == null || (_nativeProperty$set = nativeProperty.set) == null || _nativeProperty$set.call(this, value);
+ callback.call(this, value);
+ },
+ get() {
+ var _nativeProperty$get;
+ return nativeProperty == null || (_nativeProperty$get = nativeProperty.get) == null ? void 0 : _nativeProperty$get.call(this);
+ }
+ });
+}
+
/**
* Character count component
*
@@ -950,6 +966,7 @@ class CharacterCount extends GOVUKFrontendComponent {
this.bindChangeEvents();
window.addEventListener('pageshow', () => this.updateCountMessage());
this.updateCountMessage();
+ observeElementProperty(this.$textarea, 'value', this.updateCountMessage.bind(this));
}
bindChangeEvents() {
this.$textarea.addEventListener('keyup', () => this.handleKeyUp());
diff --git a/packages/govuk-frontend/dist/govuk/common/observe-element-property.mjs b/packages/govuk-frontend/dist/govuk/common/observe-element-property.mjs
new file mode 100644
index 000000000..85d47ade9
--- /dev/null
+++ b/packages/govuk-frontend/dist/govuk/common/observe-element-property.mjs
@@ -0,0 +1,18 @@
+function observeElementProperty(element, property, callback) {
+ var _Object$getOwnPropert;
+ const nativeProperty = (_Object$getOwnPropert = Object.getOwnPropertyDescriptor(element, property)) != null ? _Object$getOwnPropert : Object.getOwnPropertyDescriptor(element.constructor.prototype, property);
+ Object.defineProperty(element, property, {
+ set(value) {
+ var _nativeProperty$set;
+ nativeProperty == null || (_nativeProperty$set = nativeProperty.set) == null || _nativeProperty$set.call(this, value);
+ callback.call(this, value);
+ },
+ get() {
+ var _nativeProperty$get;
+ return nativeProperty == null || (_nativeProperty$get = nativeProperty.get) == null ? void 0 : _nativeProperty$get.call(this);
+ }
+ });
+}
+
+export { observeElementProperty };
+//# sourceMappingURL=observe-element-property.mjs.map
diff --git a/packages/govuk-frontend/dist/govuk/components/character-count/character-count.bundle.js b/packages/govuk-frontend/dist/govuk/components/character-count/character-count.bundle.js
index baeeae62c..c35dcc5bf 100644
--- a/packages/govuk-frontend/dist/govuk/components/character-count/character-count.bundle.js
+++ b/packages/govuk-frontend/dist/govuk/components/character-count/character-count.bundle.js
@@ -148,6 +148,22 @@
return out;
}
+ function observeElementProperty(element, property, callback) {
+ var _Object$getOwnPropert;
+ const nativeProperty = (_Object$getOwnPropert = Object.getOwnPropertyDescriptor(element, property)) != null ? _Object$getOwnPropert : Object.getOwnPropertyDescriptor(element.constructor.prototype, property);
+ Object.defineProperty(element, property, {
+ set(value) {
+ var _nativeProperty$set;
+ nativeProperty == null || (_nativeProperty$set = nativeProperty.set) == null || _nativeProperty$set.call(this, value);
+ callback.call(this, value);
+ },
+ get() {
+ var _nativeProperty$get;
+ return nativeProperty == null || (_nativeProperty$get = nativeProperty.get) == null ? void 0 : _nativeProperty$get.call(this);
+ }
+ });
+ }
+
class GOVUKFrontendError extends Error {
constructor(...args) {
super(...args);
@@ -490,6 +506,7 @@
this.bindChangeEvents();
window.addEventListener('pageshow', () => this.updateCountMessage());
this.updateCountMessage();
+ observeElementProperty(this.$textarea, 'value', this.updateCountMessage.bind(this));
}
bindChangeEvents() {
this.$textarea.addEventListener('keyup', () => this.handleKeyUp());
diff --git a/packages/govuk-frontend/dist/govuk/components/character-count/character-count.bundle.mjs b/packages/govuk-frontend/dist/govuk/components/character-count/character-count.bundle.mjs
index 473db45b5..244c03e8b 100644
--- a/packages/govuk-frontend/dist/govuk/components/character-count/character-count.bundle.mjs
+++ b/packages/govuk-frontend/dist/govuk/components/character-count/character-count.bundle.mjs
@@ -142,6 +142,22 @@ function normaliseDataset(Component, dataset) {
return out;
}
+function observeElementProperty(element, property, callback) {
+ var _Object$getOwnPropert;
+ const nativeProperty = (_Object$getOwnPropert = Object.getOwnPropertyDescriptor(element, property)) != null ? _Object$getOwnPropert : Object.getOwnPropertyDescriptor(element.constructor.prototype, property);
+ Object.defineProperty(element, property, {
+ set(value) {
+ var _nativeProperty$set;
+ nativeProperty == null || (_nativeProperty$set = nativeProperty.set) == null || _nativeProperty$set.call(this, value);
+ callback.call(this, value);
+ },
+ get() {
+ var _nativeProperty$get;
+ return nativeProperty == null || (_nativeProperty$get = nativeProperty.get) == null ? void 0 : _nativeProperty$get.call(this);
+ }
+ });
+}
+
class GOVUKFrontendError extends Error {
constructor(...args) {
super(...args);
@@ -484,6 +500,7 @@ class CharacterCount extends GOVUKFrontendComponent {
this.bindChangeEvents();
window.addEventListener('pageshow', () => this.updateCountMessage());
this.updateCountMessage();
+ observeElementProperty(this.$textarea, 'value', this.updateCountMessage.bind(this));
}
bindChangeEvents() {
this.$textarea.addEventListener('keyup', () => this.handleKeyUp());
diff --git a/packages/govuk-frontend/dist/govuk/components/character-count/character-count.mjs b/packages/govuk-frontend/dist/govuk/components/character-count/character-count.mjs
index 955e4ef16..04dd78d5b 100644
--- a/packages/govuk-frontend/dist/govuk/components/character-count/character-count.mjs
+++ b/packages/govuk-frontend/dist/govuk/components/character-count/character-count.mjs
@@ -1,6 +1,7 @@
import { closestAttributeValue } from '../../common/closest-attribute-value.mjs';
import { mergeConfigs, validateConfig } from '../../common/index.mjs';
import { normaliseDataset } from '../../common/normalise-dataset.mjs';
+import { observeElementProperty } from '../../common/observe-element-property.mjs';
import { ElementError, ConfigError } from '../../errors/index.mjs';
import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs';
import { I18n } from '../../i18n.mjs';
@@ -101,6 +102,7 @@ class CharacterCount extends GOVUKFrontendComponent {
this.bindChangeEvents();
window.addEventListener('pageshow', () => this.updateCountMessage());
this.updateCountMessage();
+ observeElementProperty(this.$textarea, 'value', this.updateCountMessage.bind(this));
}
bindChangeEvents() {
this.$textarea.addEventListener('keyup', () => this.handleKeyUp());
Action run for 918908b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Some experimental code dabbling into one way we could resolve the old, old problem of form controls with associated JS not responding to programatic value changes.
This is only one possible approach. In the past we've discussed creating an explicit JavaScript API for changing those values, but this hasn't yet come to fruition.
Changes
observeElementProperty
, which overrides an HTML element's native property setter function to inject a customisable callback function, allowing us to invoke custom code when the property's value is changed.value
property.I've devtested this in all contemporary major browser engines and back to Safari 11 without issue.
Limitations
As this works by overriding the property's native setter function, this solution only works for direct manipulation of a property value. It does not work if the property value is passively updated by some other code.
Property setters and getters come as a pair. Because we're overriding the setter, we must also redefine the getter. In this case, I've just manually re-invoked the native getter.
I've not done extensive testing on this whatsoever. There is a possibility that by overriding the native property handling that something somewhere else may become broken as a result.