numora-react 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +15 -433
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +6 -4
- package/dist/index.esm.js +8 -433
- package/dist/index.esm.js.map +1 -1
- package/package.json +16 -4
- package/rollup.config.mjs +0 -38
- package/src/index.tsx +0 -144
- package/tsconfig.json +0 -18
package/src/index.tsx
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import React, { ChangeEvent, ClipboardEvent, forwardRef } from 'react';
|
|
2
|
-
import {
|
|
3
|
-
handleOnChangeNumoraInput,
|
|
4
|
-
handleOnPasteNumoraInput,
|
|
5
|
-
handleOnKeyDownNumoraInput,
|
|
6
|
-
FormatOn,
|
|
7
|
-
ThousandStyle,
|
|
8
|
-
type FormattingOptions,
|
|
9
|
-
type CaretPositionInfo,
|
|
10
|
-
} from 'numora';
|
|
11
|
-
|
|
12
|
-
interface NumericInputProps extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange' | 'type' | 'inputMode'> {
|
|
13
|
-
maxDecimals?: number;
|
|
14
|
-
onChange?: (e: ChangeEvent<HTMLInputElement> | ClipboardEvent<HTMLInputElement>) => void;
|
|
15
|
-
|
|
16
|
-
// Formatting options
|
|
17
|
-
formatOn?: FormatOn;
|
|
18
|
-
thousandSeparator?: string;
|
|
19
|
-
thousandStyle?: ThousandStyle;
|
|
20
|
-
decimalSeparator?: string;
|
|
21
|
-
decimalMinLength?: number;
|
|
22
|
-
|
|
23
|
-
// Feature flags
|
|
24
|
-
enableCompactNotation?: boolean;
|
|
25
|
-
enableNegative?: boolean;
|
|
26
|
-
enableLeadingZeros?: boolean;
|
|
27
|
-
rawValueMode?: boolean;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const DEFAULT_PROPS = {
|
|
31
|
-
autoComplete: 'off',
|
|
32
|
-
autoCorrect: 'off',
|
|
33
|
-
autoCapitalize: 'none',
|
|
34
|
-
minLength: 1,
|
|
35
|
-
placeholder: '0.0',
|
|
36
|
-
pattern: '^[0-9]*[.,]?[0-9]*$',
|
|
37
|
-
spellCheck: false,
|
|
38
|
-
step: 'any',
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const NumericInput = forwardRef<HTMLInputElement, NumericInputProps>(
|
|
42
|
-
({
|
|
43
|
-
maxDecimals = 2,
|
|
44
|
-
onChange,
|
|
45
|
-
formatOn = FormatOn.Blur,
|
|
46
|
-
thousandSeparator = ',',
|
|
47
|
-
thousandStyle = ThousandStyle.Thousand,
|
|
48
|
-
decimalSeparator = '.',
|
|
49
|
-
decimalMinLength,
|
|
50
|
-
enableCompactNotation = false,
|
|
51
|
-
enableNegative = false,
|
|
52
|
-
enableLeadingZeros = false,
|
|
53
|
-
rawValueMode = false,
|
|
54
|
-
...props
|
|
55
|
-
}: NumericInputProps, ref) => {
|
|
56
|
-
const [caretPositionBeforeChange, setCaretPositionBeforeChange] =
|
|
57
|
-
React.useState<CaretPositionInfo>();
|
|
58
|
-
|
|
59
|
-
const formattingOptions: FormattingOptions = {
|
|
60
|
-
formatOn,
|
|
61
|
-
thousandSeparator,
|
|
62
|
-
ThousandStyle: thousandStyle as any,
|
|
63
|
-
decimalSeparator,
|
|
64
|
-
decimalMinLength,
|
|
65
|
-
enableCompactNotation,
|
|
66
|
-
enableNegative,
|
|
67
|
-
enableLeadingZeros,
|
|
68
|
-
rawValueMode,
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
function handleOnKeyDown(e: React.KeyboardEvent<HTMLInputElement>): void {
|
|
72
|
-
const caretInfo = handleOnKeyDownNumoraInput(e.nativeEvent, formattingOptions);
|
|
73
|
-
|
|
74
|
-
if (caretInfo) {
|
|
75
|
-
setCaretPositionBeforeChange(caretInfo);
|
|
76
|
-
} else {
|
|
77
|
-
const input = e.currentTarget;
|
|
78
|
-
setCaretPositionBeforeChange({
|
|
79
|
-
selectionStart: input.selectionStart ?? 0,
|
|
80
|
-
selectionEnd: input.selectionEnd ?? 0,
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (props.onKeyDown) {
|
|
85
|
-
props.onKeyDown(e);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function handleOnChange(e: ChangeEvent<HTMLInputElement>): void {
|
|
90
|
-
handleOnChangeNumoraInput(
|
|
91
|
-
e.nativeEvent,
|
|
92
|
-
maxDecimals,
|
|
93
|
-
caretPositionBeforeChange,
|
|
94
|
-
formattingOptions
|
|
95
|
-
);
|
|
96
|
-
setCaretPositionBeforeChange(undefined);
|
|
97
|
-
if (onChange) onChange(e);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function handleOnPaste(e: ClipboardEvent<HTMLInputElement>): void {
|
|
101
|
-
handleOnPasteNumoraInput(e.nativeEvent, maxDecimals, formattingOptions);
|
|
102
|
-
if (onChange) onChange(e);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function handleOnFocus(e: React.FocusEvent<HTMLInputElement>): void {
|
|
106
|
-
if (formatOn === FormatOn.Blur && thousandSeparator) {
|
|
107
|
-
const target = e.currentTarget;
|
|
108
|
-
target.value = target.value.replace(
|
|
109
|
-
new RegExp(thousandSeparator.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'),
|
|
110
|
-
''
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (props.onFocus) {
|
|
115
|
-
props.onFocus(e);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function handleOnBlur(e: React.FocusEvent<HTMLInputElement>): void {
|
|
120
|
-
if (props.onBlur) {
|
|
121
|
-
props.onBlur(e);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return (
|
|
126
|
-
<input
|
|
127
|
-
{...DEFAULT_PROPS}
|
|
128
|
-
{...props}
|
|
129
|
-
ref={ref}
|
|
130
|
-
onChange={handleOnChange}
|
|
131
|
-
onKeyDown={handleOnKeyDown}
|
|
132
|
-
onPaste={handleOnPaste}
|
|
133
|
-
onFocus={handleOnFocus}
|
|
134
|
-
onBlur={handleOnBlur}
|
|
135
|
-
type="text"
|
|
136
|
-
inputMode="decimal"
|
|
137
|
-
/>
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
);
|
|
141
|
-
|
|
142
|
-
NumericInput.displayName = 'NumericInput';
|
|
143
|
-
|
|
144
|
-
export { NumericInput };
|
package/tsconfig.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ESNext",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"jsx": "react-jsx",
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"outDir": "./dist",
|
|
8
|
-
"rootDir": "./src",
|
|
9
|
-
"strict": true,
|
|
10
|
-
"esModuleInterop": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"moduleResolution": "node",
|
|
13
|
-
"allowSyntheticDefaultImports": true,
|
|
14
|
-
"lib": ["ESNext", "DOM"]
|
|
15
|
-
},
|
|
16
|
-
"include": ["src/**/*"],
|
|
17
|
-
"exclude": ["node_modules", "dist"]
|
|
18
|
-
}
|