Skip to content

Commit

Permalink
feat(ui-number-input): add back options for string input
Browse files Browse the repository at this point in the history
Closes: INSTUI-4384
  • Loading branch information
HerrTopi committed Dec 2, 2024
1 parent 2586791 commit ce86cdb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
7 changes: 4 additions & 3 deletions packages/ui-number-input/src/NumberInput/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
describes: NumberInput
---

A controlled number input field. Note that this field **does not work
A controlled number input field.
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.
Note that this field **does not work
uncontrolled** - you must pass event handlers if you want it to respond to
user input.

Expand Down Expand Up @@ -300,12 +302,11 @@ type: embed
---
<Guidelines>
<Figure recommendation="yes" title="Do">
<Figure.Item>Use when the input is limited to a number (integer, rational, or irrational expressed with decimal points)</Figure.Item>
<Figure.Item>Use when you need increment/decrement functionality</Figure.Item>
<Figure.Item>Use labels at the top or to the left of the input field</Figure.Item>
</Figure>
<Figure recommendation="no" title="Don't">
<Figure.Item>Place labels or text strings to the right of the input field</Figure.Item>
<Figure.Item>Use for alphanumeric input</Figure.Item>
</Figure>
</Guidelines>
```
8 changes: 5 additions & 3 deletions packages/ui-number-input/src/NumberInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class NumberInput extends Component<NumberInputProps, NumberInputState> {
size: 'medium',
display: 'block',
textAlign: 'start',
inputMode: 'numeric'
inputMode: 'numeric',
allowStringValue: false
}

state: NumberInputState = { hasFocus: false }
Expand Down Expand Up @@ -236,7 +237,8 @@ class NumberInput extends Component<NumberInputProps, NumberInputState> {
showArrows,
value,
width,
styles
styles,
allowStringValue
} = this.props

const { interaction } = this
Expand Down Expand Up @@ -274,7 +276,7 @@ class NumberInput extends Component<NumberInputProps, NumberInputState> {
css={this.props.styles?.input}
aria-invalid={this.invalid ? 'true' : undefined}
id={this.id}
type="number"
type={allowStringValue ? 'text' : 'number'}
inputMode={this.props.inputMode}
placeholder={placeholder}
ref={this.handleInputRef}
Expand Down
11 changes: 9 additions & 2 deletions packages/ui-number-input/src/NumberInput/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ type NumberInputOwnProps = {
* The text alignment of the input.
*/
textAlign?: 'start' | 'center'

/**
* sets the input type to string and allows string as value
*/
allowStringValue?: boolean
}

type NumberInputState = {
Expand Down Expand Up @@ -220,7 +225,8 @@ const propTypes: PropValidators<PropKeys> = {
onIncrement: PropTypes.func,
onKeyDown: PropTypes.func,
inputMode: PropTypes.oneOf(['numeric', 'decimal', 'tel']),
textAlign: PropTypes.oneOf(['start', 'center'])
textAlign: PropTypes.oneOf(['start', 'center']),
allowStringValue: PropTypes.bool
}

const allowedProps: AllowedPropKeys = [
Expand All @@ -243,7 +249,8 @@ const allowedProps: AllowedPropKeys = [
'onIncrement',
'onKeyDown',
'inputMode',
'textAlign'
'textAlign',
'allowStringValue'
]

export type {
Expand Down
2 changes: 1 addition & 1 deletion packages/ui-table/src/Table/Head/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Head extends Component<TableHeadProps> {
let sortable = false
if (firstRow && firstRow.props && firstRow.props.children) {
Children.forEach(firstRow.props.children, (grandchild) => {
if (grandchild.props.onRequestSort) {
if (grandchild?.props?.onRequestSort) {
sortable = true
return
}
Expand Down

0 comments on commit ce86cdb

Please sign in to comment.