@instructure/ui-number-input 11.6.0 → 11.6.1-snapshot-129
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/CHANGELOG.md +40 -299
- package/es/NumberInput/{index.js → v1/index.js} +2 -2
- package/es/NumberInput/v2/index.js +292 -0
- package/es/NumberInput/v2/props.js +26 -0
- package/es/NumberInput/v2/styles.js +208 -0
- package/es/{index.js → exports/a.js} +1 -1
- package/{src/index.ts → es/exports/b.js} +1 -2
- package/lib/NumberInput/{index.js → v1/index.js} +7 -7
- package/lib/NumberInput/v2/index.js +301 -0
- package/lib/NumberInput/v2/props.js +31 -0
- package/lib/NumberInput/v2/styles.js +214 -0
- package/lib/{index.js → exports/a.js} +2 -2
- package/lib/exports/b.js +12 -0
- package/package.json +41 -19
- package/src/NumberInput/{index.tsx → v1/index.tsx} +2 -2
- package/src/NumberInput/{props.ts → v1/props.ts} +4 -1
- package/src/NumberInput/v2/README.md +205 -0
- package/src/NumberInput/v2/index.tsx +382 -0
- package/src/NumberInput/v2/props.ts +240 -0
- package/src/NumberInput/v2/styles.ts +219 -0
- package/src/exports/a.ts +25 -0
- package/src/exports/b.ts +25 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- package/types/NumberInput/{index.d.ts → v1/index.d.ts} +1 -1
- package/types/NumberInput/v1/index.d.ts.map +1 -0
- package/types/NumberInput/{props.d.ts → v1/props.d.ts} +1 -1
- package/types/NumberInput/v1/props.d.ts.map +1 -0
- package/types/NumberInput/v1/styles.d.ts.map +1 -0
- package/types/NumberInput/v1/theme.d.ts.map +1 -0
- package/types/NumberInput/v2/index.d.ts +104 -0
- package/types/NumberInput/v2/index.d.ts.map +1 -0
- package/types/NumberInput/v2/props.d.ts +127 -0
- package/types/NumberInput/v2/props.d.ts.map +1 -0
- package/types/NumberInput/v2/styles.d.ts +22 -0
- package/types/NumberInput/v2/styles.d.ts.map +1 -0
- package/types/exports/a.d.ts +3 -0
- package/types/exports/a.d.ts.map +1 -0
- package/types/exports/b.d.ts +3 -0
- package/types/exports/b.d.ts.map +1 -0
- package/types/NumberInput/index.d.ts.map +0 -1
- package/types/NumberInput/props.d.ts.map +0 -1
- package/types/NumberInput/styles.d.ts.map +0 -1
- package/types/NumberInput/theme.d.ts.map +0 -1
- package/types/index.d.ts +0 -3
- package/types/index.d.ts.map +0 -1
- /package/es/NumberInput/{props.js → v1/props.js} +0 -0
- /package/es/NumberInput/{styles.js → v1/styles.js} +0 -0
- /package/es/NumberInput/{theme.js → v1/theme.js} +0 -0
- /package/lib/NumberInput/{props.js → v1/props.js} +0 -0
- /package/lib/NumberInput/{styles.js → v1/styles.js} +0 -0
- /package/lib/NumberInput/{theme.js → v1/theme.js} +0 -0
- /package/src/NumberInput/{README.md → v1/README.md} +0 -0
- /package/src/NumberInput/{styles.ts → v1/styles.ts} +0 -0
- /package/src/NumberInput/{theme.ts → v1/theme.ts} +0 -0
- /package/types/NumberInput/{styles.d.ts → v1/styles.d.ts} +0 -0
- /package/types/NumberInput/{theme.d.ts → v1/theme.d.ts} +0 -0
|
@@ -35,7 +35,10 @@ import type {
|
|
|
35
35
|
ComponentStyle,
|
|
36
36
|
Spacing
|
|
37
37
|
} from '@instructure/emotion'
|
|
38
|
-
import type {
|
|
38
|
+
import type {
|
|
39
|
+
FormFieldOwnProps,
|
|
40
|
+
FormMessage
|
|
41
|
+
} from '@instructure/ui-form-field/v11_6'
|
|
39
42
|
import type {
|
|
40
43
|
InteractionType,
|
|
41
44
|
WithDeterministicIdProps
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
---
|
|
2
|
+
describes: NumberInput
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
A controlled number input field.
|
|
6
|
+
By deafult, it renders a `<intput type="number">` to the DOM. However, if you need any string value, use the `allowStringValue` flag. Only use this if absolutely necessary, since it could be confusing for screenreader users.
|
|
7
|
+
Note that this field **does not work
|
|
8
|
+
uncontrolled** - you must pass event handlers if you want it to respond to
|
|
9
|
+
user input.
|
|
10
|
+
|
|
11
|
+
This example handles arrow buttons, up/down arrow keys, and typing into the input. It also includes an `onBlur` handler that displays an error message if the input is invalid or missing.
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
---
|
|
15
|
+
type: example
|
|
16
|
+
---
|
|
17
|
+
const Example = () => {
|
|
18
|
+
const MIN = 0
|
|
19
|
+
const MAX = 999
|
|
20
|
+
|
|
21
|
+
const [disabled, setDisabled] = useState(false)
|
|
22
|
+
const [inline, setInline] = useState(false)
|
|
23
|
+
const [messages, setMessages] = useState(null)
|
|
24
|
+
const [number, setNumber] = useState(null)
|
|
25
|
+
const [readOnly, setReadOnly] = useState(false)
|
|
26
|
+
const [showArrows, setShowArrows] = useState(true)
|
|
27
|
+
const [value, setValue] = useState('')
|
|
28
|
+
|
|
29
|
+
const handleChange = (event, value) => {
|
|
30
|
+
setMessages(null)
|
|
31
|
+
setNumber(value ? Number(value) : null)
|
|
32
|
+
setValue(value)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const handleDecrement = (event) => {
|
|
36
|
+
if (!isNaN(number)) {
|
|
37
|
+
const newNumber =
|
|
38
|
+
number === null
|
|
39
|
+
? MIN
|
|
40
|
+
: Number.isInteger(number)
|
|
41
|
+
? number - 1
|
|
42
|
+
: Math.floor(number)
|
|
43
|
+
updateNumber(newNumber)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const handleIncrement = (event) => {
|
|
48
|
+
if (!isNaN(number)) {
|
|
49
|
+
const newNumber =
|
|
50
|
+
number === null
|
|
51
|
+
? MIN + 1
|
|
52
|
+
: Number.isInteger(number)
|
|
53
|
+
? number + 1
|
|
54
|
+
: Math.ceil(number)
|
|
55
|
+
updateNumber(newNumber)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const handleBlur = (event) => {
|
|
60
|
+
if (isNaN(number)) return invalidNumber(value)
|
|
61
|
+
if (number === null) return required()
|
|
62
|
+
return updateNumber(Math.round(number))
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const updateNumber = (n) => {
|
|
66
|
+
const number = bound(n)
|
|
67
|
+
setMessages(null)
|
|
68
|
+
setNumber(number)
|
|
69
|
+
setValue(number)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const bound = (n) => {
|
|
73
|
+
if (n < MIN) return MIN
|
|
74
|
+
if (n > MAX) return MAX
|
|
75
|
+
return n
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const invalidNumber = (value) => {
|
|
79
|
+
setMessages([
|
|
80
|
+
{ text: `'${value}' is not a valid number.`, type: 'error' }
|
|
81
|
+
])
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const required = () => {
|
|
85
|
+
setMessages([{ text: 'This is required.', type: 'error' }])
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const toggleDisabled = (event) => {
|
|
89
|
+
setDisabled((prevDisabled) => !prevDisabled)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const toggleInline = (event) => {
|
|
93
|
+
setInline((prevInline) => !prevInline)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const toggleReadOnly = (event) => {
|
|
97
|
+
setReadOnly((prevReadOnly) => !prevReadOnly)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const toggleShowArrows = (event) => {
|
|
101
|
+
setShowArrows((prevShowArrows) => !prevShowArrows)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return (
|
|
105
|
+
<FormFieldGroup description="Controlled NumberInput example">
|
|
106
|
+
<Checkbox
|
|
107
|
+
checked={showArrows}
|
|
108
|
+
label="Show arrows"
|
|
109
|
+
onChange={toggleShowArrows}
|
|
110
|
+
/>
|
|
111
|
+
<Checkbox
|
|
112
|
+
checked={disabled}
|
|
113
|
+
label="Disabled"
|
|
114
|
+
onChange={toggleDisabled}
|
|
115
|
+
/>
|
|
116
|
+
<Checkbox
|
|
117
|
+
checked={readOnly}
|
|
118
|
+
label="Read only"
|
|
119
|
+
onChange={toggleReadOnly}
|
|
120
|
+
/>
|
|
121
|
+
<Checkbox
|
|
122
|
+
checked={inline}
|
|
123
|
+
label="Inline display"
|
|
124
|
+
onChange={toggleInline}
|
|
125
|
+
/>
|
|
126
|
+
<NumberInput
|
|
127
|
+
renderLabel={`How old are you? (${MIN}-${MAX})`}
|
|
128
|
+
display={inline ? 'inline-block' : null}
|
|
129
|
+
messages={messages}
|
|
130
|
+
onBlur={handleBlur}
|
|
131
|
+
onChange={handleChange}
|
|
132
|
+
onDecrement={handleDecrement}
|
|
133
|
+
onIncrement={handleIncrement}
|
|
134
|
+
placeholder="Age (in years)"
|
|
135
|
+
interaction={
|
|
136
|
+
disabled ? 'disabled' : readOnly ? 'readonly' : 'enabled'
|
|
137
|
+
}
|
|
138
|
+
isRequired
|
|
139
|
+
showArrows={showArrows}
|
|
140
|
+
value={value}
|
|
141
|
+
/>
|
|
142
|
+
</FormFieldGroup>
|
|
143
|
+
)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
render(<Example />)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
> Note: `NumberInput` accepts a string or number as its `value`. However, the value returned by the `onChange` callback is always a string and should be converted to a number before attempting to augment it.
|
|
150
|
+
|
|
151
|
+
You can see here most of the visual states of the component.
|
|
152
|
+
|
|
153
|
+
```js
|
|
154
|
+
---
|
|
155
|
+
type: example
|
|
156
|
+
---
|
|
157
|
+
<Flex gap='medium' direction='column'>
|
|
158
|
+
<NumberInput
|
|
159
|
+
renderLabel='normal'
|
|
160
|
+
placeholder="placeholder"
|
|
161
|
+
/>
|
|
162
|
+
<NumberInput
|
|
163
|
+
interaction='disabled'
|
|
164
|
+
renderLabel='disabled'
|
|
165
|
+
placeholder="placeholder"
|
|
166
|
+
/>
|
|
167
|
+
<NumberInput
|
|
168
|
+
interaction='readonly'
|
|
169
|
+
renderLabel='readonly'
|
|
170
|
+
placeholder="placeholder"
|
|
171
|
+
/>
|
|
172
|
+
<NumberInput
|
|
173
|
+
renderLabel='with error message'
|
|
174
|
+
placeholder="placeholder"
|
|
175
|
+
messages={[{ text: 'This is an error.', type: 'error' }]}
|
|
176
|
+
/>
|
|
177
|
+
<NumberInput
|
|
178
|
+
renderLabel='with success message'
|
|
179
|
+
placeholder="placeholder"
|
|
180
|
+
messages={[{ text: 'Great success!', type: 'success' }]}
|
|
181
|
+
/>
|
|
182
|
+
<NumberInput
|
|
183
|
+
renderLabel='large size (default is "medium")'
|
|
184
|
+
placeholder="placeholder"
|
|
185
|
+
size='large'
|
|
186
|
+
/>
|
|
187
|
+
</Flex>
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Guidelines
|
|
191
|
+
|
|
192
|
+
```js
|
|
193
|
+
---
|
|
194
|
+
type: embed
|
|
195
|
+
---
|
|
196
|
+
<Guidelines>
|
|
197
|
+
<Figure recommendation="yes" title="Do">
|
|
198
|
+
<Figure.Item>Use when you need increment/decrement functionality</Figure.Item>
|
|
199
|
+
<Figure.Item>Use labels at the top or to the left of the input field</Figure.Item>
|
|
200
|
+
</Figure>
|
|
201
|
+
<Figure recommendation="no" title="Don't">
|
|
202
|
+
<Figure.Item>Place labels or text strings to the right of the input field</Figure.Item>
|
|
203
|
+
</Figure>
|
|
204
|
+
</Guidelines>
|
|
205
|
+
```
|
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License (MIT)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import {
|
|
26
|
+
useState,
|
|
27
|
+
useRef,
|
|
28
|
+
useCallback,
|
|
29
|
+
useImperativeHandle,
|
|
30
|
+
forwardRef,
|
|
31
|
+
useEffect
|
|
32
|
+
} from 'react'
|
|
33
|
+
import keycode from 'keycode'
|
|
34
|
+
|
|
35
|
+
import { FormField } from '@instructure/ui-form-field/latest'
|
|
36
|
+
import {
|
|
37
|
+
ChevronUpInstUIIcon,
|
|
38
|
+
ChevronDownInstUIIcon
|
|
39
|
+
} from '@instructure/ui-icons'
|
|
40
|
+
import {
|
|
41
|
+
pickProps,
|
|
42
|
+
callRenderProp,
|
|
43
|
+
getInteraction,
|
|
44
|
+
useDeterministicId,
|
|
45
|
+
passthroughProps
|
|
46
|
+
} from '@instructure/ui-react-utils'
|
|
47
|
+
|
|
48
|
+
import { useStyle } from '@instructure/emotion'
|
|
49
|
+
|
|
50
|
+
import generateStyle from './styles'
|
|
51
|
+
|
|
52
|
+
import type { NumberInputProps } from './props'
|
|
53
|
+
import { Renderable } from '@instructure/shared-types'
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
---
|
|
57
|
+
category: components
|
|
58
|
+
id: NumberInput
|
|
59
|
+
---
|
|
60
|
+
**/
|
|
61
|
+
const NumberInput = forwardRef<NumberInputHandle, NumberInputProps>(
|
|
62
|
+
(props, ref) => {
|
|
63
|
+
const {
|
|
64
|
+
messages = [],
|
|
65
|
+
isRequired = false,
|
|
66
|
+
showArrows = true,
|
|
67
|
+
size = 'medium',
|
|
68
|
+
display = 'block',
|
|
69
|
+
textAlign = 'start',
|
|
70
|
+
inputMode = 'numeric',
|
|
71
|
+
allowStringValue = false,
|
|
72
|
+
renderLabel,
|
|
73
|
+
placeholder,
|
|
74
|
+
value,
|
|
75
|
+
width,
|
|
76
|
+
renderIcons,
|
|
77
|
+
margin,
|
|
78
|
+
inputRef: inputRefProp,
|
|
79
|
+
onFocus,
|
|
80
|
+
onBlur,
|
|
81
|
+
onChange,
|
|
82
|
+
onKeyDown,
|
|
83
|
+
onDecrement,
|
|
84
|
+
onIncrement,
|
|
85
|
+
id: idProp,
|
|
86
|
+
themeOverride,
|
|
87
|
+
...rest
|
|
88
|
+
} = props
|
|
89
|
+
// these are icon tokens
|
|
90
|
+
type ArrowButtonColors =
|
|
91
|
+
| 'actionSecondaryBaseColor'
|
|
92
|
+
| 'actionSecondaryHoverColor'
|
|
93
|
+
| 'actionSecondaryActiveColor'
|
|
94
|
+
| 'actionSecondaryDisabledColor'
|
|
95
|
+
const [upButtonState, setUpButtonState] = useState<ArrowButtonColors>(
|
|
96
|
+
'actionSecondaryBaseColor'
|
|
97
|
+
)
|
|
98
|
+
const [downButtonState, setDownButtonState] = useState<ArrowButtonColors>(
|
|
99
|
+
'actionSecondaryBaseColor'
|
|
100
|
+
)
|
|
101
|
+
// Refs
|
|
102
|
+
const containerRef = useRef<Element | null>(null)
|
|
103
|
+
const inputRef = useRef<HTMLInputElement | null>(null)
|
|
104
|
+
|
|
105
|
+
// Deterministic ID generation
|
|
106
|
+
const [deterministicId, setDeterministicId] = useState<string | undefined>()
|
|
107
|
+
const getId = useDeterministicId('NumberInput')
|
|
108
|
+
useEffect(() => {
|
|
109
|
+
setDeterministicId(getId())
|
|
110
|
+
}, []) // Empty deps array - only run once on mount
|
|
111
|
+
const id = idProp || deterministicId
|
|
112
|
+
|
|
113
|
+
// Computed values
|
|
114
|
+
const invalid =
|
|
115
|
+
!!messages &&
|
|
116
|
+
messages.some(
|
|
117
|
+
(message) => message.type === 'error' || message.type === 'newError'
|
|
118
|
+
)
|
|
119
|
+
const success =
|
|
120
|
+
!!messages && messages.some((message) => message.type === 'success')
|
|
121
|
+
|
|
122
|
+
const interaction = getInteraction({ props })
|
|
123
|
+
if (
|
|
124
|
+
interaction === 'disabled' &&
|
|
125
|
+
upButtonState !== 'actionSecondaryDisabledColor'
|
|
126
|
+
) {
|
|
127
|
+
setUpButtonState('actionSecondaryDisabledColor')
|
|
128
|
+
setDownButtonState('actionSecondaryDisabledColor')
|
|
129
|
+
} else if (
|
|
130
|
+
interaction === 'enabled' &&
|
|
131
|
+
downButtonState !== 'actionSecondaryBaseColor'
|
|
132
|
+
) {
|
|
133
|
+
setUpButtonState('actionSecondaryBaseColor')
|
|
134
|
+
setDownButtonState('actionSecondaryBaseColor')
|
|
135
|
+
}
|
|
136
|
+
// Styles - useStyle will pass these to generateStyle(componentTheme, params as props, params as state)
|
|
137
|
+
// We need to provide all values that generateStyle needs from both props and state
|
|
138
|
+
const styles = useStyle({
|
|
139
|
+
generateStyle,
|
|
140
|
+
themeOverride,
|
|
141
|
+
params: {
|
|
142
|
+
size,
|
|
143
|
+
textAlign,
|
|
144
|
+
interaction,
|
|
145
|
+
invalid,
|
|
146
|
+
success
|
|
147
|
+
},
|
|
148
|
+
componentId: 'NumberInput',
|
|
149
|
+
displayName: 'NumberInput',
|
|
150
|
+
useTokensFrom: 'TextInput'
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
// Event handlers
|
|
154
|
+
const handleInputRef = useCallback(
|
|
155
|
+
(element: HTMLInputElement | null) => {
|
|
156
|
+
inputRef.current = element
|
|
157
|
+
|
|
158
|
+
if (typeof inputRefProp === 'function') {
|
|
159
|
+
inputRefProp(element)
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
[inputRefProp]
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
const handleRef = useCallback((el: Element | null) => {
|
|
166
|
+
containerRef.current = el
|
|
167
|
+
}, [])
|
|
168
|
+
|
|
169
|
+
const handleFocus = useCallback(
|
|
170
|
+
(event: React.FocusEvent<HTMLInputElement>) => {
|
|
171
|
+
if (typeof onFocus === 'function') {
|
|
172
|
+
onFocus(event)
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
[onFocus]
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
const handleBlur = useCallback(
|
|
179
|
+
(event: React.FocusEvent<HTMLInputElement>) => {
|
|
180
|
+
if (typeof onBlur === 'function') {
|
|
181
|
+
onBlur(event)
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
[onBlur]
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
const handleChange = useCallback(
|
|
188
|
+
(event: React.ChangeEvent<HTMLInputElement>) => {
|
|
189
|
+
if (typeof onChange === 'function') {
|
|
190
|
+
onChange(event, event.target.value)
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
[onChange]
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
const handleKeyDown = useCallback(
|
|
197
|
+
(event: React.KeyboardEvent<HTMLInputElement>) => {
|
|
198
|
+
if (typeof onKeyDown === 'function') {
|
|
199
|
+
onKeyDown(event)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (event.keyCode === keycode.codes.down) {
|
|
203
|
+
event.preventDefault()
|
|
204
|
+
if (typeof onDecrement === 'function') {
|
|
205
|
+
onDecrement(event)
|
|
206
|
+
}
|
|
207
|
+
} else if (event.keyCode === keycode.codes.up) {
|
|
208
|
+
event.preventDefault()
|
|
209
|
+
if (typeof onIncrement === 'function') {
|
|
210
|
+
onIncrement(event)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
[onKeyDown, onDecrement, onIncrement]
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
const arrowClicked = useCallback(
|
|
218
|
+
(
|
|
219
|
+
event: React.MouseEvent<HTMLButtonElement>,
|
|
220
|
+
callback:
|
|
221
|
+
| NumberInputProps['onIncrement']
|
|
222
|
+
| NumberInputProps['onDecrement']
|
|
223
|
+
) => {
|
|
224
|
+
event.preventDefault()
|
|
225
|
+
if (interaction === 'enabled') {
|
|
226
|
+
inputRef.current?.focus()
|
|
227
|
+
|
|
228
|
+
if (typeof callback === 'function') {
|
|
229
|
+
callback(event)
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
[interaction]
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
const handleClickUpArrow = useCallback(
|
|
237
|
+
(event: React.MouseEvent<HTMLButtonElement>) => {
|
|
238
|
+
setUpButtonState('actionSecondaryActiveColor')
|
|
239
|
+
arrowClicked(event, onIncrement)
|
|
240
|
+
},
|
|
241
|
+
[arrowClicked, onIncrement]
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
const handleClickDownArrow = useCallback(
|
|
245
|
+
(event: React.MouseEvent<HTMLButtonElement>) => {
|
|
246
|
+
setDownButtonState('actionSecondaryActiveColor')
|
|
247
|
+
arrowClicked(event, onDecrement)
|
|
248
|
+
},
|
|
249
|
+
[arrowClicked, onDecrement]
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
// Expose imperative API via ref
|
|
253
|
+
useImperativeHandle(
|
|
254
|
+
ref,
|
|
255
|
+
() => ({
|
|
256
|
+
focus: () => {
|
|
257
|
+
inputRef.current?.focus()
|
|
258
|
+
},
|
|
259
|
+
get id() {
|
|
260
|
+
return id
|
|
261
|
+
},
|
|
262
|
+
get invalid() {
|
|
263
|
+
return invalid
|
|
264
|
+
},
|
|
265
|
+
get interaction() {
|
|
266
|
+
return interaction
|
|
267
|
+
},
|
|
268
|
+
get value() {
|
|
269
|
+
return inputRef.current?.value
|
|
270
|
+
}
|
|
271
|
+
}),
|
|
272
|
+
[id, invalid, interaction]
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
// Render methods
|
|
276
|
+
const renderArrows = (customIcons?: {
|
|
277
|
+
increase: Renderable
|
|
278
|
+
decrease: Renderable
|
|
279
|
+
}) => {
|
|
280
|
+
return (
|
|
281
|
+
<span css={styles?.arrowContainer}>
|
|
282
|
+
{/* eslint-disable jsx-a11y/mouse-events-have-key-events */}
|
|
283
|
+
<button
|
|
284
|
+
aria-hidden
|
|
285
|
+
css={styles?.arrow}
|
|
286
|
+
onMouseDown={handleClickUpArrow}
|
|
287
|
+
onMouseOver={() => setUpButtonState('actionSecondaryHoverColor')}
|
|
288
|
+
onMouseOut={() => setUpButtonState('actionSecondaryBaseColor')}
|
|
289
|
+
tabIndex={-1}
|
|
290
|
+
type="button"
|
|
291
|
+
>
|
|
292
|
+
{customIcons?.increase ? (
|
|
293
|
+
callRenderProp(customIcons.increase)
|
|
294
|
+
) : (
|
|
295
|
+
<ChevronUpInstUIIcon size="sm" color={upButtonState} />
|
|
296
|
+
)}
|
|
297
|
+
</button>
|
|
298
|
+
|
|
299
|
+
<button
|
|
300
|
+
aria-hidden
|
|
301
|
+
css={styles?.arrow}
|
|
302
|
+
onMouseDown={handleClickDownArrow}
|
|
303
|
+
onMouseOver={() => setDownButtonState('actionSecondaryHoverColor')}
|
|
304
|
+
onMouseOut={() => setDownButtonState('actionSecondaryBaseColor')}
|
|
305
|
+
tabIndex={-1}
|
|
306
|
+
type="button"
|
|
307
|
+
>
|
|
308
|
+
{customIcons?.decrease ? (
|
|
309
|
+
callRenderProp(customIcons.decrease)
|
|
310
|
+
) : (
|
|
311
|
+
<ChevronDownInstUIIcon size="sm" color={downButtonState} />
|
|
312
|
+
)}
|
|
313
|
+
</button>
|
|
314
|
+
{/* eslint-enable jsx-a11y/mouse-events-have-key-events */}
|
|
315
|
+
</span>
|
|
316
|
+
)
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const label = callRenderProp(renderLabel)
|
|
320
|
+
|
|
321
|
+
const passedProps = passthroughProps(rest)
|
|
322
|
+
|
|
323
|
+
// Don't render until we have an ID
|
|
324
|
+
if (!id) {
|
|
325
|
+
return null
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return (
|
|
329
|
+
<FormField
|
|
330
|
+
{...pickProps(props, FormField.allowedProps)}
|
|
331
|
+
label={label}
|
|
332
|
+
inline={display === 'inline-block'}
|
|
333
|
+
id={id}
|
|
334
|
+
elementRef={handleRef}
|
|
335
|
+
margin={margin}
|
|
336
|
+
isRequired={isRequired}
|
|
337
|
+
disabled={interaction === 'disabled'}
|
|
338
|
+
readOnly={interaction === 'readonly'}
|
|
339
|
+
data-cid="NumberInput"
|
|
340
|
+
>
|
|
341
|
+
<span css={styles?.inputWidth} style={width ? { width } : undefined}>
|
|
342
|
+
<span css={styles?.inputContainer}>
|
|
343
|
+
<input
|
|
344
|
+
{...passedProps}
|
|
345
|
+
css={styles?.input}
|
|
346
|
+
aria-invalid={invalid ? 'true' : undefined}
|
|
347
|
+
id={id}
|
|
348
|
+
type={allowStringValue ? 'text' : 'number'}
|
|
349
|
+
inputMode={inputMode}
|
|
350
|
+
placeholder={interaction === 'enabled' ? placeholder : undefined}
|
|
351
|
+
ref={handleInputRef}
|
|
352
|
+
required={isRequired}
|
|
353
|
+
value={value}
|
|
354
|
+
disabled={interaction === 'disabled'}
|
|
355
|
+
readOnly={interaction === 'readonly'}
|
|
356
|
+
onFocus={handleFocus}
|
|
357
|
+
onBlur={handleBlur}
|
|
358
|
+
onChange={handleChange}
|
|
359
|
+
onKeyDown={handleKeyDown}
|
|
360
|
+
/>
|
|
361
|
+
{showArrows && interaction !== 'readonly'
|
|
362
|
+
? renderArrows(renderIcons)
|
|
363
|
+
: null}
|
|
364
|
+
</span>
|
|
365
|
+
</span>
|
|
366
|
+
</FormField>
|
|
367
|
+
)
|
|
368
|
+
}
|
|
369
|
+
)
|
|
370
|
+
|
|
371
|
+
NumberInput.displayName = 'NumberInput'
|
|
372
|
+
|
|
373
|
+
export interface NumberInputHandle {
|
|
374
|
+
focus: () => void
|
|
375
|
+
readonly id: string | undefined
|
|
376
|
+
readonly invalid: boolean
|
|
377
|
+
readonly interaction: ReturnType<typeof getInteraction>
|
|
378
|
+
readonly value: string | undefined
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
export default NumberInput
|
|
382
|
+
export { NumberInput }
|