Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Goto Value Improvements #1072

Merged
merged 10 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/iris-grid/src/GotoRow.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
}

.goto-row-text {
width: 10ch;
min-width: 10ch;
}

.goto-row-close {
Expand Down
36 changes: 30 additions & 6 deletions packages/iris-grid/src/GotoRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
} from '@deephaven/filters';
import { Button, DateTimeInput } from '@deephaven/components';
import { TableUtils } from '@deephaven/jsapi-utils';
import { assertNotNull } from '@deephaven/utils';
import classNames from 'classnames';
import './GotoRow.scss';
import IrisGridModel from './IrisGridModel';
Expand Down Expand Up @@ -86,6 +85,21 @@ function GotoRow({
const res = 'Row number';

const { rowCount } = model;

const handleGotoValueNumberKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
e.stopPropagation();
e.preventDefault();
onGotoValueSubmit();
} else if (
(e.key === 'Backspace' || e.key === 'Delete') &&
(gotoValue === `${Number.POSITIVE_INFINITY}` ||
gotoValue === `${Number.NEGATIVE_INFINITY}`)
Zhou-Ziheng marked this conversation as resolved.
Show resolved Hide resolved
) {
onGotoValueInputChanged('');
}
};

const handleGotoValueKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
e.stopPropagation();
Expand All @@ -96,8 +110,7 @@ function GotoRow({

const index = model.getColumnIndexByName(gotoValueSelectedColumnName);

assertNotNull(index);
const selectedColumn = columns[index];
const selectedColumn = columns[index ?? 0];
Zhou-Ziheng marked this conversation as resolved.
Show resolved Hide resolved

const columnType = selectedColumn?.type;

Expand Down Expand Up @@ -126,13 +139,22 @@ function GotoRow({
<div className="goto-row-input">
<input
ref={gotoValueInputRef}
type="number"
className={classNames('form-control', {
'is-invalid': gotoValueError !== '',
})}
onKeyDown={handleGotoValueKeyDown}
onKeyDown={handleGotoValueNumberKeyDown}
placeholder="value"
onChange={e => onGotoValueInputChanged(e.target.value)}
onChange={e => {
const value = e.target.value.toLowerCase();
// regex tests for
if (/^-?[0-9]*\.?[0-9]*$/.test(e.target.value)) {
Zhou-Ziheng marked this conversation as resolved.
Show resolved Hide resolved
onGotoValueInputChanged(e.target.value);
} else if (value === '-i' || value === '-infinity') {
onGotoValueInputChanged(`${Number.NEGATIVE_INFINITY}`);
} else if (value === 'i' || value === 'infinity') {
onGotoValueInputChanged(`${Number.POSITIVE_INFINITY}`);
}
}}
value={gotoValue}
/>
</div>
Expand All @@ -148,6 +170,7 @@ function GotoRow({
'is-invalid': gotoValueError !== '',
}
)}
defaultValue={gotoValue}
onChange={onGotoValueInputChanged}
/>
</div>
Expand Down Expand Up @@ -202,6 +225,7 @@ function GotoRow({
}}
value={gotoValue}
>
<option aria-label="null value" key="null" value="" />
<option key="true" value="true">
true
</option>
Expand Down
75 changes: 61 additions & 14 deletions packages/iris-grid/src/IrisGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2453,10 +2453,32 @@ export class IrisGrid extends Component<IrisGridProps, IrisGridState> {
this.focusRowInGrid(row);
return;
}

const cursorRow = this.grid?.state.cursorRow;
const cursorColumn = this.grid?.state.cursorColumn;

if (cursorRow == null || cursorColumn == null) {
// if a cell is not selected / grid is not rendered
this.setState({
isGotoShown: !isGotoShown,
gotoRow: '',
gotoValue: '',
gotoRowError: '',
gotoValueError: '',
});
return;
}
// if a row is selected
const { model } = this.props;
const { name, type } = model.columns[cursorColumn];

const cellValue = model.valueForCell(cursorColumn, cursorRow);
const text = IrisGridUtils.convertValueToText(cellValue, type);
this.setState({
isGotoShown: !isGotoShown,
gotoRow: '',
gotoValue: '',
gotoRow: `${cursorRow}`,
gotoValue: text,
gotoValueSelectedColumnName: name,
gotoRowError: '',
gotoValueError: '',
});
Expand Down Expand Up @@ -3219,19 +3241,16 @@ export class IrisGrid extends Component<IrisGridProps, IrisGridState> {
if (inputString === '') {
return;
}
const selectedColumn = IrisGridUtils.getColumnByName(
model.columns,
selectedColumnName
);

const columnIndex = model.getColumnIndexByName(selectedColumnName);
if (columnIndex === undefined) {
if (selectedColumn === undefined) {
return;
}

const selectedColumn = model.columns[columnIndex];

let searchFromRow;

if (this.grid) {
({ selectionEndRow: searchFromRow } = this.grid.state);
}
let searchFromRow = this.grid?.state.cursorRow;

if (searchFromRow == null) {
searchFromRow = 0;
Expand All @@ -3243,11 +3262,13 @@ export class IrisGrid extends Component<IrisGridProps, IrisGridState> {
gotoValueSelectedFilter === FilterType.eqIgnoreCase;

try {
const { formatter } = model;
const columnDataType = TableUtils.getNormalizedType(selectedColumn.type);

let rowIndex;

switch (columnDataType) {
case TableUtils.dataType.CHAR:
case TableUtils.dataType.STRING: {
rowIndex = await model.seekRow(
isBackwards === true ? searchFromRow - 1 : searchFromRow + 1,
Expand All @@ -3261,7 +3282,6 @@ export class IrisGrid extends Component<IrisGridProps, IrisGridState> {
break;
}
case TableUtils.dataType.DATETIME: {
const { formatter } = model;
const [startDate] = DateUtils.parseDateRange(
inputString,
formatter.timeZone
Expand All @@ -3283,7 +3303,13 @@ export class IrisGrid extends Component<IrisGridProps, IrisGridState> {
!TableUtils.isBigDecimalType(selectedColumn.type) &&
!TableUtils.isBigIntegerType(selectedColumn.type)
) {
const inputValue = parseInt(inputString, 10);
let inputValue = parseInt(inputString, 10);
if (inputString === '-Infinity') {
Zhou-Ziheng marked this conversation as resolved.
Show resolved Hide resolved
inputValue = Number.NEGATIVE_INFINITY;
} else if (inputString === 'Infinity') {
inputValue = Number.POSITIVE_INFINITY;
}

rowIndex = await model.seekRow(
searchFromRow,
selectedColumn,
Expand Down Expand Up @@ -3311,7 +3337,11 @@ export class IrisGrid extends Component<IrisGridProps, IrisGridState> {
searchFromRow,
selectedColumn,
dh.ValueType.STRING,
inputString,
TableUtils.makeValue(
selectedColumn.type,
inputString,
formatter.timeZone
),
undefined,
undefined,
isBackwards ?? false
Expand Down Expand Up @@ -3635,6 +3665,23 @@ export class IrisGrid extends Component<IrisGridProps, IrisGridState> {
}

handleGotoValueSelectedColumnNameChanged(columnName: ColumnName): void {
const { model } = this.props;
const cursorRow = this.grid?.state.cursorRow;

if (cursorRow != null) {
const index = model.getColumnIndexByName(columnName);
const column = IrisGridUtils.getColumnByName(model.columns, columnName);
if (index == null || column == null) {
return;
}
const value = model.valueForCell(index, cursorRow);
const text = IrisGridUtils.convertValueToText(value, column.type);
this.setState({
gotoValueSelectedColumnName: columnName,
gotoValue: text,
gotoValueError: '',
});
}
this.setState({
gotoValueSelectedColumnName: columnName,
gotoValueError: '',
Expand Down
49 changes: 49 additions & 0 deletions packages/iris-grid/src/IrisGridUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GridUtils, GridRange, MoveOperation } from '@deephaven/grid';
import dh, { Column, Table, Sort } from '@deephaven/jsapi-shim';
import { TypeValue as FilterTypeValue } from '@deephaven/filters';
import { DateUtils } from '@deephaven/jsapi-utils';
import type { AdvancedFilter } from './CommonTypes';
import { FilterData } from './IrisGrid';
import IrisGridTestUtils from './IrisGridTestUtils';
Expand Down Expand Up @@ -572,3 +573,51 @@ describe('combineFiltersFromList', () => {
);
});
});

describe('convert string to text', () => {
it('converts null to empty string', () => {
expect(IrisGridUtils.convertValueToText(null, 'string')).toEqual('');
});
it('converts empty string', () => {
expect(IrisGridUtils.convertValueToText('', 'string')).toEqual('');
});
it('converts string to stri', () => {
expect(IrisGridUtils.convertValueToText('test', 'string')).toEqual('test');
});
it('converts length 1 string to stri', () => {
expect(IrisGridUtils.convertValueToText('t', 'string')).toEqual('t');
});
it('converts number to strin', () => {
expect(IrisGridUtils.convertValueToText(65, 'string')).toEqual('65');
});
});

describe('convert char to text', () => {
it('converts number to ascii', () => {
expect(IrisGridUtils.convertValueToText(65, 'char')).toEqual('A');
});
it('converts null to empty char', () => {
expect(IrisGridUtils.convertValueToText(null, 'char')).toEqual('');
});
});

describe('convert other column types to text', () => {
it('converts number to string on number column', () => {
expect(IrisGridUtils.convertValueToText(65, 'number')).toEqual('65');
});
it('converts null to empty string on number column', () => {
expect(IrisGridUtils.convertValueToText(null, 'number')).toEqual('');
});
it('converts time correctly on datetime column', () => {
expect(
IrisGridUtils.convertValueToText(
dh.i18n.DateTimeFormat.parse(
DateUtils.FULL_DATE_FORMAT,
'2022-02-03 02:14:59.000000000',
dh.i18n.TimeZone.getTimeZone('NY')
),
'io.deephaven.time.DateTime'
)
).toEqual('2022-02-03 02:14:59.000');
});
});
32 changes: 32 additions & 0 deletions packages/iris-grid/src/IrisGridUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ function isValidIndex(x: number, array: unknown[]): boolean {
return x >= 0 && x < array.length;
}

function isDateWrapper(value: unknown): value is DateWrapper {
return (value as DateWrapper).asDate != null;
}

class IrisGridUtils {
/**
* Exports the state from Grid component to a JSON stringifiable object
Expand Down Expand Up @@ -1671,6 +1675,34 @@ class IrisGridUtils {

return { groups: [...groupMap.values()], maxDepth, groupMap, parentMap };
}

/**
* @param value The value of the cell in a column
* @param columnType The type of the column
* @returns The value of the cell converted to text
*/
static convertValueToText(value: unknown, columnType: string): string {
if (
columnType != null &&
TableUtils.isCharType(columnType) &&
value != null &&
typeof value === 'number'
) {
return String.fromCharCode(value);
}
if (TableUtils.isDateType(columnType) && isDateWrapper(value)) {
const date = new Date(value.asDate());
const offset = date.getTimezoneOffset();
const offsetDate = new Date(date.getTime() - offset * 60 * 1000);
const dateText = offsetDate.toISOString();
const formattedText = dateText.replace('T', ' ').substring(0, 23);
return formattedText;
}
if (value == null) {
return '';
}
return `${value}`;
}
}

export default IrisGridUtils;