web ui: allow only numbers to be entered in number fields
@@ -12,7 +12,7 @@ import TextField from 'material-ui/TextField'
import validateType from './validateType'
import debounce from '../../debounce'
-import { fromElektraBool } from '../../../utils'
+import { fromElektraBool, isNumberType } from '../../../utils'
const DebouncedTextField = debounce(TextField, { timeout: 1500 })
@@ -33,6 +33,7 @@ export default class SimpleTextField extends Component {
const val = this.state.value
const comp = debounce ? DebouncedTextField : TextField
const isBinary = meta && meta.hasOwnProperty('binary')
+ const type = meta && meta['check/type'] || 'any'
return (
<div draggable="true" onDragStart={e => e.preventDefault()}>
@@ -61,6 +62,24 @@ export default class SimpleTextField extends Component {
floatingLabelText: label,
floatingLabelFixed: !!label,
onKeyPress: onKeyPress,
+ onKeyDown: isNumberType(type) && ((e) => {
+ if ([46, 8, 9, 27, 13, 110, 190].includes(e.keyCode) || // allow backspace, delete, etc
+ // allow: ctrl/cmd+A
+ (e.keyCode == 65 && (e.ctrlKey === true || e.metaKey === true)) ||
+ // allow: ctrl/cmd+C
+ (e.keyCode == 67 && (e.ctrlKey === true || e.metaKey === true)) ||
+ // allow: ctrl/cmd+X
+ (e.keyCode == 88 && (e.ctrlKey === true || e.metaKey === true)) ||
+ // allow: home, end, left, right
+ (e.keyCode >= 35 && e.keyCode <= 39)) {
+ // let it happen, don't do anything
+ return
+ }
+ // ensure that it is a number and stop the keypress
+ if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
+ e.preventDefault()
+ }
+ })
})}
</div>
)
