Skip to content

Commit

Permalink
Remove focus event listener from iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
jplukarski committed Jan 27, 2025
1 parent af75cd9 commit b10c56b
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 20 deletions.
5 changes: 1 addition & 4 deletions src/lib/formatter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export class PatternFormatter {
start: options.selection.start,
end: options.selection.end,
};

for (let i = 0; i < this.pattern.length; i++) {
const patternChar = this.pattern[i];
let inputChar = originalString[originalStringIndex];
Expand All @@ -53,7 +52,7 @@ export class PatternFormatter {

if (patternChar.value.test(inputChar)) {
formattedString += inputChar;
originalStringIndex++;
originalStringIndex++;
break;
} else {
if (patternChar.index <= selection.start) {
Expand All @@ -74,7 +73,6 @@ export class PatternFormatter {
}

unformat(options: FormatMetadata): FormatMetadata {
console.log("Unformatting: ", options.value);
let start = options.selection.start;
let end = options.selection.end;
let unformattedString = "";
Expand All @@ -101,7 +99,6 @@ export class PatternFormatter {
end--;
}
}
console.log("Unformatted String", unformattedString);
return {
selection: {
start: start,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/strategies/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class BaseStrategy extends StrategyInterface {

getUnformattedValue(forceUnformat?: boolean): string {
let value = this.inputElement.value;
console.log("Raw value: ", value);
// console.log("Raw value: ", value);
if (forceUnformat || this.isFormatted) {
value = this.formatter.unformat({
value: this.inputElement.value,
Expand Down
15 changes: 1 addition & 14 deletions src/lib/strategies/ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,9 @@ export class IosStrategy extends BaseStrategy {
this.formatListener();

if (!isCustomEvent) {
console.log("About to fix leading blank space on ios");
this.fixLeadingBlankSpaceOnIos();
}
});
this.inputElement.addEventListener("focus", () => {
console.log("Focus event on input");
this.formatListener();
});
this.inputElement.addEventListener("paste", (event) => {
this.pasteEventHandler(event as ClipboardEvent);
});
Expand All @@ -43,27 +38,19 @@ export class IosStrategy extends BaseStrategy {
// is positioned as if there is a blank space when there
// is not, setting it to '' in a setTimeout fixes it ¯\_(ツ)_/¯
private fixLeadingBlankSpaceOnIos(): void {
console.log("fixing leading blank space");
const input = this.inputElement;

if (input.value === "") {
console.log("input is empty, fixing the blank space");
setTimeout(function () {
input.value = "";
}, 0);
} else {
console.log("Not the last digit being erased, not fixing blank space");
}
}

private formatListener(): void {
console.log("Formatting the listener");
const input = this.inputElement;
const stateToFormat = this.getStateToFormat();
const formattedState = this.formatter.format(stateToFormat);
console.log("Input to format: ", input);
console.log("State to Format", stateToFormat);
console.log("Formatted State: ", formattedState);
const formattedState = this.formatter.format(stateToFormat); // this is where shit is going awry
input.value = formattedState.value;
setSelection(
input,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/strategies/ios.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("iOS Strategy", function () {
it("adds ios specific listeners", function () {
const strategy = new IosStrategy(options);

["keydown", "input", "focus"].forEach(function (event) {
["keydown", "input"].forEach(function (event) {
expect(strategy.inputElement.addEventListener).toBeCalledWith(
event,
expect.any(Function)
Expand Down

0 comments on commit b10c56b

Please sign in to comment.