UNPKG

@adaptabletools/adaptable

Version:

Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements

140 lines (139 loc) 4.95 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { renderWithAdaptableContext } from '../../../View/renderWithAdaptableContext'; import { InternalAdaptableNumberEditor, } from './InternalAdaptableNumberEditor'; import { KeyCode } from 'ag-grid-enterprise'; import { forwardRef, useImperativeHandle, useRef, useState } from 'react'; function shouldClearExistingValue(params) { return params.eventKey === KeyCode.BACKSPACE || params.eventKey === KeyCode.DELETE; } function isValidChar(char) { return ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].includes(char); } export function getStartValue(params) { if (shouldClearExistingValue(params)) { return ''; } if (params.eventKey && isValidChar(params.eventKey)) { return Number(params.eventKey); } return params.value; } const defaultValueParser = ({ newValue, oldValue: _ }) => { return newValue; }; const style = { position: 'absolute', top: '0px', left: '0px', right: '0px', bottom: '0px', }; export const AdaptableReactNumberEditor = forwardRef((props, ref) => { const [initialValue] = useState(() => getStartValue(props)); const valueRef = useRef(initialValue); const columnId = props.column.getColId(); const adaptable = props.context.__adaptable; const colValueParser = props.column.getColDef().valueParser; const valueParser = typeof colValueParser === 'function' ? colValueParser : defaultValueParser; function onValueChange(value) { value = valueParser ? valueParser({ ...props, oldValue: props.value, newValue: value, }) : value; valueRef.current = value; props.onValueChange?.(value); } const editorRef = useRef(null); useImperativeHandle(ref, () => { return { focusIn() { editorRef.current?.focus(); }, getValue() { return valueRef.current; }, }; }); const editorElement = (_jsx(InternalAdaptableNumberEditor, { defaultValue: initialValue, showClearButton: props.showClearButton ?? true, emptyValue: props.emptyValue ?? '', onValueChange: onValueChange, ref: (editor) => { editorRef.current = editor; editor?.focus(); } })); function onKeyDown(keyDownEvent) { adaptable._emit('CellEditorKeyDown', { keyDownEvent, cellValue: valueRef.current, columnId, updateValueCallback: (updatedValue) => { editorRef.current.setValue(updatedValue); }, }); } return (_jsx("div", { style: style, onKeyDown: onKeyDown, children: renderWithAdaptableContext(editorElement, adaptable) })); }); AdaptableReactNumberEditor.displayName = 'AdaptableReactNumberEditor'; export class AdaptableNumberEditor { value; columnId; el; params; editor; valueParser = defaultValueParser; unmountReactRoot; init(params) { this.value = getStartValue(params); this.params = params; this.columnId = params.column.getColId(); const { valueParser } = params.column.getColDef(); if (typeof valueParser === 'function') { this.valueParser = valueParser; } this.el = document.createElement('div'); Object.keys(style).forEach((key) => { this.el.style[key] = style[key]; }); } getGui() { return this.el; } getValue() { return this.value; } focusIn() { this.editor?.focus(); } focusOut() { } afterGuiAttached() { const adaptable = this.params.context.__adaptable; const defaultValue = this.value; const editorElement = (_jsx(InternalAdaptableNumberEditor, { defaultValue: defaultValue, showClearButton: this.params.showClearButton ?? true, emptyValue: this.params.emptyValue ?? '', onValueChange: this.onValueChange, ref: (editor) => { this.editor = editor; editor?.focus(); } })); this.unmountReactRoot = adaptable.renderReactRoot(renderWithAdaptableContext(editorElement, adaptable), this.el); this.getGui().addEventListener('keydown', (keyDownEvent) => { adaptable._emit('CellEditorKeyDown', { keyDownEvent, cellValue: this.value, columnId: this.columnId, updateValueCallback: (updatedValue) => { this.editor.setValue(updatedValue); }, }); }); } destroy() { this.unmountReactRoot?.(); } onValueChange = (value) => { this.value = this.valueParser ? this.valueParser({ ...this.params, oldValue: this.params.value, newValue: value, }) : value; }; }