Skip to content

Commit

Permalink
breaking: rename onPaste to pasteTransformer (#1152)
Browse files Browse the repository at this point in the history
* breaking: rename `onPaste` to `pasteTransformer`

* update tests
  • Loading branch information
huntabyte authored Feb 12, 2025
1 parent 3a16171 commit 7ab3546
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-peas-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bits-ui": patch
---

breaking: rename `PinInput` `onPaste` to `pasteTransformer`
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
disabled = false,
value = $bindable(""),
onValueChange = noop,
onPaste,
pasteTransformer,
...restProps
}: PinInputRootProps = $props();
Expand All @@ -47,7 +47,7 @@
}
),
pushPasswordManagerStrategy: box.with(() => pushPasswordManagerStrategy),
onPaste: box.with(() => onPaste),
pasteTransformer: box.with(() => pasteTransformer),
});
const mergedInputProps = $derived(mergeProps(restProps, rootState.inputProps));
Expand Down
9 changes: 5 additions & 4 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 @@ -30,8 +30,7 @@ type PinInputRootStateProps = WithRefProps<
disabled: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onComplete: (...args: any[]) => void;
onPaste?: (text: string) => string;

pasteTransformer?: (text: string) => string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
pattern: any;
maxLength: number;
Expand Down Expand Up @@ -408,7 +407,7 @@ class PinInputRootState {
};

if (
!this.opts.onPaste?.current &&
!this.opts.pasteTransformer?.current &&
(!this.#initialLoad.isIOS || !e.clipboardData || !input)
) {
const newValue = getNewValue(e.clipboardData?.getData("text/plain"));
Expand All @@ -419,7 +418,9 @@ class PinInputRootState {
}

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

const newValue = getNewValue(content);
Expand Down
2 changes: 1 addition & 1 deletion packages/bits-ui/src/lib/bits/pin-input/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type PinInputRootPropsWithoutHTML = Omit<
* Use this function to clean up the pasted text, like removing hyphens or other
* characters that should not make it into the input.
*/
onPaste?: (text: string) => string;
pasteTransformer?: (text: string) => string;

/**
* The max length of the input.
Expand Down
4 changes: 2 additions & 2 deletions packages/tests/src/tests/pin-input/pin-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe("Pin Input", () => {
const mockComplete = vi.fn();
const { user, hiddenInput } = setup({
onComplete: mockComplete,
onPaste: (text) => text.replace(/-/g, ""),
pasteTransformer: (text) => text.replace(/-/g, ""),
});

await user.click(hiddenInput);
Expand Down Expand Up @@ -207,7 +207,7 @@ describe("Pin Input", () => {
const { user, hiddenInput } = setup({
maxlength: 6,
onComplete: mockComplete,
onPaste: (text) => text.replace(/-/g, ""),
pasteTransformer: (text) => text.replace(/-/g, ""),
});

await user.click(hiddenInput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export { default as PinInputRootPushPasswordManagerStrategyProp } from "./pin-in
export { default as PinInputCellCellProp } from "./pin-input-cell-cell-prop.md";
export { default as PinInputRootChildSnippetProps } from "./pin-input-root-child-snippet-props.md";
export { default as PinInputRootChildrenSnippetProps } from "./pin-input-root-children-snippet-props.md";
export { default as PinInputRootOnPasteProp } from "./pin-input-root-on-paste.md";
export { default as PinInputRootPasteTransformerProp } from "./pin-input-root-paste-transformer.md";
6 changes: 3 additions & 3 deletions sites/docs/src/lib/content/api-reference/pin-input.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
PinInputRootChildSnippetProps,
PinInputRootChildrenSnippetProps,
PinInputRootOnCompleteProp,
PinInputRootOnPasteProp,
PinInputRootPasteTransformerProp,
PinInputRootPushPasswordManagerStrategyProp,
PinInputRootTextAlignProp,
} from "./extended-types/pin-input/index.js";
Expand Down Expand Up @@ -53,10 +53,10 @@ const root = createApiSchema<PinInputRootPropsWithoutHTML>({
description: "A callback function that is called when the input is completely filled.",
definition: PinInputRootOnCompleteProp,
}),
onPaste: createFunctionProp({
pasteTransformer: createFunctionProp({
description:
"A callback function that is called when the user pastes text into the input. It receives the pasted text as an argument and should return the sanitized text. Useful for cleaning up pasted text, like removing hyphens or other characters that should not make it into the input.",
definition: PinInputRootOnPasteProp,
definition: PinInputRootPasteTransformerProp,
}),
inputId: createStringProp({
description: "Optionally provide an ID to apply to the hidden input element.",
Expand Down

0 comments on commit 7ab3546

Please sign in to comment.