@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
77 lines (76 loc) • 2.79 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import { Icon } from '../../../components/icons';
import useProperty from '../../../components/utils/useProperty';
const fillStyle = {
position: 'absolute',
left: 0,
top: 0,
right: 0,
bottom: 0,
};
const hostStyle = {
...fillStyle,
boxSizing: 'border-box',
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
};
const inputStyle = {
position: 'relative',
background: 'transparent',
boxSizing: 'border-box',
minWidth: 0,
outline: 'none',
width: '100%',
border: 'none',
};
const disallowedChars = new Set(['e', 'E']);
const preventDisallowedChars = (e) => {
if (disallowedChars.has(e.key)) {
e.preventDefault();
}
};
export const InternalAdaptableNumberEditor = React.forwardRef(function InternalAdaptableNumberEditorFn(props, ref) {
const inputRef = React.useRef(null);
const focus = () => {
inputRef.current?.focus();
};
const [value, setValue] = useProperty(props, 'value', props.defaultValue, {
onChange: (value) => {
if (value !== '' && value == Number(value)) {
value = Number(value);
}
props.onValueChange?.(value);
},
});
React.useEffect(() => {
setValue(props.defaultValue);
}, []);
React.useImperativeHandle(ref, () => {
return {
focus,
setValue,
};
});
const showClear = props.showClearButton && props.emptyValue != null && value != props.emptyValue;
return (_jsxs("div", { style: hostStyle, onKeyDown: (e) => {
if (e.key === 'Tab' && showClear && document.activeElement === inputRef.current) {
e.stopPropagation();
}
}, children: [_jsx("input", { "data-name": "AdaptableNumberEditorInput", type: props.type ?? 'number', value: value, onKeyDown: preventDisallowedChars, onChange: React.useCallback((event) => {
setValue(event.target.value);
}, []), style: inputStyle, ref: inputRef }), showClear ? (_jsx("div", { className: "twa:inline-block twa:mx-1", onClick: (e) => {
if (props.emptyValue != null) {
setValue(props.emptyValue);
e.stopPropagation();
focus();
}
}, onKeyDown: (e) => {
if (props.emptyValue != null && e.key === 'Enter') {
setValue(props.emptyValue);
e.stopPropagation();
focus();
}
}, children: _jsx(Icon, { size: 15, name: "close", tabIndex: 0 }) })) : null] }));
});