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

fix: Pin Input paste without transformer violating match regex #1101

Merged
merged 3 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/few-coats-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bits-ui": patch
---

fix: Pin Input allowing paste on non-matching
39 changes: 23 additions & 16 deletions packages/bits-ui/src/lib/bits/pin-input/pin-input.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ class PinInputRootState {
this.#prevInputMetadata.prev = [s, e, dir];
};

oninput = (e: BitsEvent<Event, HTMLInputElement>) => {
oninput = (e: BitsEvent<InputEvent, HTMLInputElement>) => {
const newValue = e.currentTarget.value.slice(0, this.opts.maxLength.current);
if (newValue.length > 0 && this.#regexPattern && !this.#regexPattern.test(newValue)) {
e.preventDefault();
Expand Down Expand Up @@ -392,32 +392,39 @@ class PinInputRootState {
const input = this.#inputRef.current;
if (!input) return;

const getNewValue = (finalContent: string | undefined) => {
const start = input.selectionStart === null ? undefined : input.selectionStart;
const end = input.selectionEnd === null ? undefined : input.selectionEnd;
const isReplacing = start !== end;
const initNewVal = this.opts.value.current;
const newValueUncapped = isReplacing
? initNewVal.slice(0, start) + finalContent + initNewVal.slice(end)
: initNewVal.slice(0, start) + finalContent + initNewVal.slice(start);
return newValueUncapped.slice(0, this.opts.maxLength.current);
};

const isValueInvalid = (newValue: string) => {
return newValue.length > 0 && this.#regexPattern && !this.#regexPattern.test(newValue);
};

if (
!this.opts.onPaste?.current &&
(!this.#initialLoad.isIOS || !e.clipboardData || !input)
) {
const newValue = getNewValue(e.clipboardData?.getData("text/plain"));
if (isValueInvalid(newValue)) {
e.preventDefault();
}
return;
}

const _content = e.clipboardData?.getData("text/plain") ?? "";
const content = this.opts.onPaste?.current ? this.opts.onPaste.current(_content) : _content;
e.preventDefault();

const start = input.selectionStart === null ? undefined : input.selectionStart;
const end = input.selectionEnd === null ? undefined : input.selectionEnd;

const isReplacing = start !== end;
const newValue = getNewValue(content);

const initNewVal = this.opts.value.current;

const newValueUncapped = isReplacing
? initNewVal.slice(0, start) + content + initNewVal.slice(end)
: initNewVal.slice(0, start) + content + initNewVal.slice(start);
const newValue = newValueUncapped.slice(0, this.opts.maxLength.current);

if (newValue.length > 0 && this.#regexPattern && !this.#regexPattern.test(newValue)) {
return;
}
if (isValueInvalid(newValue)) return;

input.value = newValue;
this.opts.value.current = newValue;
Expand Down Expand Up @@ -454,7 +461,7 @@ class PinInputRootState {
"data-pin-input-input-mss": this.#mirrorSelectionStart,
"data-pin-input-input-mse": this.#mirrorSelectionEnd,
inputmode: this.opts.inputmode.current,
// pattern: this.#regexPattern?.source,
pattern: this.#regexPattern?.source,
maxlength: this.opts.maxLength.current,
value: this.opts.value.current,
disabled: getDisabled(this.opts.disabled.current),
Expand Down