UNPKG

@kensoni/react-input-number

Version:

Handle input number with React. Support Material UI (MUI)

27 lines (26 loc) 872 B
var WHITELIST_KEYS = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', 'Backspace', 'Tab' ]; var COMMAND_KEYS = ['a', 'c', 'x', 'v', 'z', 'r']; export function isWhiteKeys(key, value, comma, disableNegative, integer) { var sep = comma ? ',' : '.'; return WHITELIST_KEYS .concat((integer || existSep(value)) ? [] : [sep]) .concat(!disableNegative && value.value === '' ? ['-'] : []) .includes(key); } export function existSep(value) { return value.toString().includes('.'); } export function revertByChange(value, comma) { if (comma) { return value.replace(/\./g, '').replace(',', '.'); } return value.replace(/,/g, ''); } export function isCommandKey(e) { if (!e.ctrlKey && !e.metaKey) return false; return COMMAND_KEYS.includes(e.key); }