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

tsify more addons modules #1661

Merged
merged 10 commits into from May 24, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-data-grid-addons/package.json
Expand Up @@ -29,7 +29,7 @@
"dependencies": {
"lodash": "^4.17.11",
"prop-types": "^15.7.2",
"react-contextmenu": "^2.10.0",
"react-contextmenu": "^2.11.0",
"react-data-grid": "^7.0.0-alpha.12",
"react-dnd": "^2.6.0",
"react-dnd-html5-backend": "^2.6.0",
Expand Down

This file was deleted.

@@ -0,0 +1,148 @@
import React from 'react';
import { Column, RowData } from 'react-data-grid';

enum RuleType {
Number = 1,
Range = 2,
GreaterThen = 3,
LessThen = 4
}

type Rule =
| { type: RuleType.Range; begin: number; end: number }
| { type: RuleType.GreaterThen | RuleType.LessThen | RuleType.Number; value: number };

interface ChangeEvent {
filterTerm: Rule[] | null;
column: Column;
rawValue: string;
filterValues: typeof filterValues;
}

interface Props {
column: Column;
onChange(event: ChangeEvent): void;
}

export default function NumericFilter({ column, onChange }: Props) {
/** Validates the input */
function handleKeyPress(event: React.KeyboardEvent<HTMLInputElement>) {
const result = /[><,0-9-]/.test(event.key);
if (result === false) {
event.preventDefault();
}
}

function handleChange(event: React.ChangeEvent<HTMLInputElement>) {
const value = event.target.value;
nstepien marked this conversation as resolved.
Show resolved Hide resolved
const filters = getRules(value);
onChange({
filterTerm: filters.length > 0 ? filters : null,
column,
rawValue: value,
filterValues
});
}

const inputKey = `header-filter-${column.key}`;
const columnStyle: React.CSSProperties = {
float: 'left',
marginRight: 5,
maxWidth: '80%'
};
const badgeStyle: React.CSSProperties = {
cursor: 'help'
};

const tooltipText = 'Input Methods: Range (x-y), Greater Then (>x), Less Then (<y)';

return (
<div>
<div style={columnStyle}>
<input
key={inputKey}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing type="text"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input

If this attribute is not specified, the default type adopted is text.

I can add it, but it's not necessary. wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually prefer to add it to be explicit but it is fine in this case

placeholder="e.g. 3,10-15,>20"
className="form-control input-sm"
onChange={handleChange}
onKeyPress={handleKeyPress}
/>
</div>
<div className="input-sm">
<span className="badge" style={badgeStyle} title={tooltipText}>?</span>
</div>
</div>
);
}


function filterValues(row: RowData, columnFilter: { filterTerm: { [key in string]: Rule } }, columnKey: string) {
if (columnFilter.filterTerm == null) {
return true;
}

// implement default filter logic
const value = parseInt(row[columnKey] as string, 10);
for (const ruleKey in columnFilter.filterTerm) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (const rule of columnFilter.filterTerm)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not an iterable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I got confused with filterTerm: Rule[] | null;

const rule = columnFilter.filterTerm[ruleKey];

switch (rule.type) {
case RuleType.Number:
if (rule.value === value) {
return true;
}
break;
case RuleType.GreaterThen:
if (rule.value <= value) {
return true;
}
break;
case RuleType.LessThen:
if (rule.value >= value) {
return true;
}
break;
case RuleType.Range:
if (rule.begin <= value && rule.end >= value) {
return true;
}
break;
default:
break;
}
}

return false;
}

export function getRules(value: string): Rule[] {
if (value === '') {
return [];
}

// handle each value with comma
return value.split(',').map((str): Rule => {
// handle dash
const dashIdx = str.indexOf('-');
if (dashIdx > 0) {
const begin = parseInt(str.slice(0, dashIdx), 10);
const end = parseInt(str.slice(dashIdx + 1), 10);
return { type: RuleType.Range, begin, end };
}

// handle greater then
if (str.includes('>')) {
const begin = parseInt(str.slice(str.indexOf('>') + 1), 10);
return { type: RuleType.GreaterThen, value: begin };
}

// handle less then
if (str.includes('<')) {
const end = parseInt(str.slice(str.indexOf('<') + 1), 10);
return { type: RuleType.LessThen, value: end };
}

// handle normal values
const numericValue = parseInt(str, 10);
return { type: RuleType.Number, value: numericValue };
});
}

This file was deleted.