numeric-input-react 1.0.1 → 1.0.3
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/LICENSE +15 -0
- package/README.md +179 -0
- package/package.json +18 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# numeric-input-react
|
|
2
|
+
|
|
3
|
+
A React component for handling numeric input with advanced features including decimal support, negative numbers, thousands separators, and IME composition handling.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ✅ **Decimal number support** - Allow or disallow decimal points
|
|
8
|
+
- ✅ **Negative number support** - Optional negative number input
|
|
9
|
+
- ✅ **Thousands separator** - Customizable separator for number formatting
|
|
10
|
+
- ✅ **Full-width character conversion** - Automatically converts full-width Japanese characters (0-9, ., ,, -) to half-width equivalents
|
|
11
|
+
- ✅ **IME composition handling** - Properly handles IME input methods
|
|
12
|
+
- ✅ **Leading zero handling** - Smart handling of leading zeros
|
|
13
|
+
- ✅ **TypeScript support** - Fully typed with TypeScript
|
|
14
|
+
- ✅ **React 19 compatible** - Built for React 19
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install numeric-input-react
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
import { NumericInput } from 'numeric-input-react'
|
|
26
|
+
|
|
27
|
+
function App() {
|
|
28
|
+
const [value, setValue] = useState({ value: 0, formattedValue: '' })
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<NumericInput
|
|
32
|
+
value={value.formattedValue}
|
|
33
|
+
onValueChange={setValue}
|
|
34
|
+
allowDecimal={true}
|
|
35
|
+
allowNegative={true}
|
|
36
|
+
separator=","
|
|
37
|
+
placeholder="Enter a number"
|
|
38
|
+
/>
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Props
|
|
44
|
+
|
|
45
|
+
### NumericInputProps
|
|
46
|
+
|
|
47
|
+
Extends all standard HTML input props (`ComponentProps<'input'>`) with the following additional props:
|
|
48
|
+
|
|
49
|
+
| Prop | Type | Default | Description |
|
|
50
|
+
|------|------|---------|-------------|
|
|
51
|
+
| `onValueChange` | `(valueObject: NumericInputValue) => void` | **Required** | Callback function that receives the numeric value and formatted string |
|
|
52
|
+
| `separator` | `string` | `undefined` | Thousands separator (e.g., `","` for comma, `" "` for space) |
|
|
53
|
+
| `allowDecimal` | `boolean` | `false` | Whether to allow decimal point input |
|
|
54
|
+
| `allowNegative` | `boolean` | `false` | Whether to allow negative numbers |
|
|
55
|
+
|
|
56
|
+
### NumericInputValue
|
|
57
|
+
|
|
58
|
+
The callback receives an object with:
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
type NumericInputValue = {
|
|
62
|
+
value: number // The numeric value
|
|
63
|
+
formattedValue: string // The formatted string (with separator if provided)
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Examples
|
|
68
|
+
|
|
69
|
+
### Basic numeric input
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
<NumericInput
|
|
73
|
+
onValueChange={(val) => console.log(val.value)}
|
|
74
|
+
placeholder="Enter number"
|
|
75
|
+
/>
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### With decimal support
|
|
79
|
+
|
|
80
|
+
```tsx
|
|
81
|
+
<NumericInput
|
|
82
|
+
allowDecimal={true}
|
|
83
|
+
onValueChange={(val) => console.log(val.value)}
|
|
84
|
+
placeholder="Enter decimal number"
|
|
85
|
+
/>
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### With negative numbers
|
|
89
|
+
|
|
90
|
+
```tsx
|
|
91
|
+
<NumericInput
|
|
92
|
+
allowNegative={true}
|
|
93
|
+
onValueChange={(val) => console.log(val.value)}
|
|
94
|
+
placeholder="Enter number (can be negative)"
|
|
95
|
+
/>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### With thousands separator
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
<NumericInput
|
|
102
|
+
separator=","
|
|
103
|
+
onValueChange={(val) => {
|
|
104
|
+
console.log('Numeric value:', val.value)
|
|
105
|
+
console.log('Formatted:', val.formattedValue) // e.g., "1,234,567"
|
|
106
|
+
}}
|
|
107
|
+
placeholder="Enter number"
|
|
108
|
+
/>
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Full example with all features
|
|
112
|
+
|
|
113
|
+
```tsx
|
|
114
|
+
import { useState } from 'react'
|
|
115
|
+
import { NumericInput } from 'numeric-input-react'
|
|
116
|
+
|
|
117
|
+
function PriceInput() {
|
|
118
|
+
const [price, setPrice] = useState({ value: 0, formattedValue: '' })
|
|
119
|
+
|
|
120
|
+
return (
|
|
121
|
+
<div>
|
|
122
|
+
<NumericInput
|
|
123
|
+
value={price.formattedValue}
|
|
124
|
+
onValueChange={setPrice}
|
|
125
|
+
allowDecimal={true}
|
|
126
|
+
allowNegative={false}
|
|
127
|
+
separator=","
|
|
128
|
+
placeholder="Enter price"
|
|
129
|
+
className="price-input"
|
|
130
|
+
/>
|
|
131
|
+
<p>Value: {price.value}</p>
|
|
132
|
+
<p>Formatted: {price.formattedValue}</p>
|
|
133
|
+
</div>
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Behavior
|
|
139
|
+
|
|
140
|
+
### Leading Zeros
|
|
141
|
+
- Single `0` is preserved when typed
|
|
142
|
+
- Multiple leading zeros (e.g., `001`, `0123`) are automatically removed
|
|
143
|
+
- `0.` and `-0.` patterns are preserved to allow decimal input
|
|
144
|
+
|
|
145
|
+
### Decimal Points
|
|
146
|
+
- Only one decimal point is allowed
|
|
147
|
+
- If `allowDecimal` is `false`, decimal points are removed
|
|
148
|
+
- Values ending with `.` (e.g., `123.`) are preserved to allow continued typing
|
|
149
|
+
|
|
150
|
+
### Negative Numbers
|
|
151
|
+
- Negative sign (`-`) can only appear at the start
|
|
152
|
+
- If `allowNegative` is `false`, negative signs are removed
|
|
153
|
+
- Multiple negative signs are normalized to a single sign at the start
|
|
154
|
+
|
|
155
|
+
### Full-width Character Conversion
|
|
156
|
+
The component automatically converts full-width Japanese characters to half-width:
|
|
157
|
+
- `0-9` → `0-9`
|
|
158
|
+
- `.` → `.`
|
|
159
|
+
- `,` → `,`
|
|
160
|
+
- `-` → `-`
|
|
161
|
+
|
|
162
|
+
### IME Composition
|
|
163
|
+
The component properly handles IME (Input Method Editor) composition events, ensuring correct behavior when using Japanese, Chinese, or other IME-based input methods.
|
|
164
|
+
|
|
165
|
+
## TypeScript
|
|
166
|
+
|
|
167
|
+
The library is written in TypeScript and includes full type definitions:
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
import { NumericInput, type NumericInputProps } from 'numeric-input-react'
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## License
|
|
174
|
+
|
|
175
|
+
ISC
|
|
176
|
+
|
|
177
|
+
## Contributing
|
|
178
|
+
|
|
179
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "numeric-input-react",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "A React component for handling numeric input with decimal support, negative numbers, thousands separators, and IME composition handling",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"exports": {
|
|
@@ -14,9 +14,24 @@
|
|
|
14
14
|
"build": "tsc -p tsconfig.build.json"
|
|
15
15
|
},
|
|
16
16
|
"files": ["dist"],
|
|
17
|
-
"keywords": [
|
|
17
|
+
"keywords": [
|
|
18
|
+
"react",
|
|
19
|
+
"numeric",
|
|
20
|
+
"input",
|
|
21
|
+
"number",
|
|
22
|
+
"decimal",
|
|
23
|
+
"separator",
|
|
24
|
+
"thousands",
|
|
25
|
+
"ime",
|
|
26
|
+
"typescript",
|
|
27
|
+
"component"
|
|
28
|
+
],
|
|
18
29
|
"author": "",
|
|
19
30
|
"license": "ISC",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/khachoangpt/numeric-input-react.git"
|
|
34
|
+
},
|
|
20
35
|
"type": "module",
|
|
21
36
|
"dependencies": {
|
|
22
37
|
"react": "^19.2.3"
|