Skip to content

Commit

Permalink
housekeeping
Browse files Browse the repository at this point in the history
  • Loading branch information
huntabyte committed Feb 4, 2025
1 parent 40438c2 commit ac875ff
Showing 1 changed file with 17 additions and 26 deletions.
43 changes: 17 additions & 26 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 @@ -392,24 +392,27 @@ class PinInputRootState {
const input = this.#inputRef.current;
if (!input) return;

if (
!this.opts.onPaste?.current &&
(!this.#initialLoad.isIOS || !e.clipboardData || !input)
) {
const contentData = e.clipboardData?.getData("text/plain");
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) + contentData + initNewVal.slice(end)
: initNewVal.slice(0, start) + contentData + initNewVal.slice(start);
const newValue = newValueUncapped.slice(0, this.opts.maxLength.current);
? 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 (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;
Expand All @@ -419,21 +422,9 @@ class PinInputRootState {
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

0 comments on commit ac875ff

Please sign in to comment.