Skip to content

Commit 2a06f0a

Browse files
authored
feat: make custom type from customInput works (#5)
* feat: better function to set caret position * ci: remove deprecated husky line * feat: make custom type from customInput works --------- Co-authored-by: danestves <danestves@users.noreply.github.com>
1 parent 2bd3bbe commit 2a06f0a

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

.husky/commit-msg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/bin/sh
2-
. "$(dirname "$0")/_/husky.sh"
32
. "$(dirname "$0")/common.sh"
43

54
bun commitlint --edit $1

.husky/pre-commit

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/bin/sh
2-
. "$(dirname "$0")/_/husky.sh"
32
. "$(dirname "$0")/common.sh"
43

54
bunx lint-staged

src/currency-input.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { mergeRefs } from "@react-aria/utils";
22
import { resolveCurrencyFormat } from "@sumup/intl";
33
import { type FocusEvent, type FormEvent, type ForwardedRef, forwardRef, useRef } from "react";
44
import {
5+
type InputAttributes,
56
NumberFormatBase,
67
type NumberFormatBaseProps,
78
type NumberFormatValues,
@@ -10,14 +11,18 @@ import {
1011

1112
import { setCaretPosition } from "./utils";
1213

13-
export interface CurrencyInputProps extends Omit<NumberFormatBaseProps, "format" | "prefix"> {
14-
currency?: string;
14+
type CurrencyInputProps<BaseType = InputAttributes> = Omit<
15+
NumberFormatBaseProps<BaseType>,
16+
"format" | "prefix" | "customInput"
17+
> & {
1518
locale?: string;
19+
currency?: string;
1620
withCurrencySymbol?: boolean;
17-
}
21+
customInput?: React.ComponentType<BaseType>;
22+
};
1823

19-
function RenderCurrencyInput(
20-
{ locale = "en", currency = "USD", withCurrencySymbol = true, ...props }: CurrencyInputProps,
24+
function RenderCurrencyInput<BaseType = InputAttributes>(
25+
{ locale = "en", currency = "USD", withCurrencySymbol = true, ...props }: CurrencyInputProps<BaseType>,
2126
forwadedRef: ForwardedRef<HTMLInputElement>,
2227
) {
2328
const innerRef = useRef<HTMLInputElement>(null);
@@ -106,10 +111,12 @@ function RenderCurrencyInput(
106111
setCaretPosition(innerRef.current, positionLastDigit + (minimumFractionDigits ?? 0));
107112
}
108113

114+
// @ts-expect-error - onInput is not part of the type
109115
props?.onInput?.(event);
110116
}
111117

112118
return (
119+
// @ts-expect-error
113120
<NumberFormatBase
114121
{...props}
115122
format={format}
@@ -123,4 +130,7 @@ function RenderCurrencyInput(
123130
);
124131
}
125132

126-
export const CurrencyInput = forwardRef<HTMLInputElement, CurrencyInputProps>(RenderCurrencyInput);
133+
// @ts-expect-error - forwardRef is not part of the type
134+
export const CurrencyInput: <BaseType = InputAttributes>(
135+
props: CurrencyInputProps<BaseType> & { ref?: ForwardedRef<HTMLInputElement> },
136+
) => ReturnType<typeof RenderCurrencyInput<BaseType>> = forwardRef(RenderCurrencyInput);

src/utils.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
/**
22
* @see https://github.com/i18n-components/i18n-components/blob/main/packages/input-number/src/helpers.ts#L2
33
*/
4-
export function setCaretPosition(el: HTMLInputElement, caretPos: number): boolean {
4+
export function setCaretPosition(el: HTMLInputElement, caretPos = 0): boolean {
5+
// biome-ignore lint/correctness/noSelfAssign: comes from the reference
6+
el.value = el.value;
7+
// ^ this is used to not only get "focus", but
8+
// to make sure we don't have it everything -selected-
9+
// (it causes an issue in chrome, and having it doesn't hurt any other browser)
10+
511
if (el !== null) {
12+
if (el.selectionStart || el.selectionStart === 0) {
13+
el.focus();
14+
el.setSelectionRange(caretPos, caretPos);
15+
return true;
16+
}
17+
18+
// fail city, fortunately this never happens (as far as I've tested) :)
619
el.focus();
7-
el.setSelectionRange(caretPos, caretPos);
8-
return true;
20+
return false;
921
}
1022

1123
return false;

0 commit comments

Comments
 (0)