@ebay/ebayui-core
Version:
Collection of core eBay components; considered to be the building blocks for all composite structures, pages & apps.
36 lines (35 loc) • 1.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class CharacterCount {
onCreate(input) {
this.state = {
count: this.countFromValue(input.value),
};
}
onInput(input) {
if (typeof window === "undefined") {
return;
}
if (this.timeout) {
clearTimeout(this.timeout);
}
this.timeout = setTimeout(() => {
this.state.count = this.countFromValue(input.value);
this.emit("change", {
count: this.state.count,
inputAriaLive: this.state.count >= input.max ? "polite" : "off",
});
}, 500);
}
countFromValue(value) {
if (typeof value === "string") {
// use iterator to account for emojis and other multi-char symbols
return [...value].length;
}
if (typeof value === "number") {
return value;
}
return 0;
}
}
module.exports = CharacterCount;