Skip to content

Commit

Permalink
Merge pull request #113 from irontec/datetime-fast-search
Browse files Browse the repository at this point in the history
Datetime fast search enhancements
  • Loading branch information
mmadariaga authored May 28, 2024
2 parents f1a8438 + 6dbf0ee commit e0e748b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 41 deletions.
2 changes: 1 addition & 1 deletion library/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@irontec/ivoz-ui",
"version": "1.3.9",
"version": "1.3.11",
"description": "UI library used in ivozprovider",
"license": "GPL-3.0",
"main": "index.js",
Expand Down
111 changes: 71 additions & 40 deletions library/src/components/List/Content/FastSearchField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ForwardedRef, forwardRef, useEffect, useState } from 'react';
import EntityService from '../../../services/entity/EntityService';
import { StyledSearchTextField } from '../../../services/form/Field/TextField/TextField.styles';

import { InputBaseComponentProps } from '@mui/material';
import { useStoreActions, useStoreState } from 'store';
import { DropdownArrayChoices, isPropertyFk } from '../../../services';
import { StyledAutocomplete } from '../../../services/form/Field/Autocomplete/Autocomplete.styles';
Expand Down Expand Up @@ -47,6 +48,47 @@ const FastSearchField = (

const [value, setValue] = useState(firstColumnCriteria?.value || '');

const isDatetime = !isFk && firstColumnSpec.format === 'date-time';

const triggerSearchIfChanged = () => {
if (!firstColumnCriteria) {
return;
}

if (firstColumnCriteria.value == value) {
return;
}

if (value !== '') {
firstColumnCriteria.value = encodeURIComponent(value);
}

let match = false;
let matchIdx: string | undefined;
for (const idx in queryStringCriteria) {
if (queryStringCriteria[idx].name !== firstColumnCriteria.name) {
continue;
}

if (queryStringCriteria[idx].type !== firstColumnCriteria.type) {
continue;
}

queryStringCriteria[idx] = firstColumnCriteria;
matchIdx = idx;
match = true;
break;
}

if (!match) {
queryStringCriteria.push(firstColumnCriteria);
} else if (value === '' && matchIdx) {
queryStringCriteria.splice(parseInt(matchIdx, 10), 1);
}

setQueryStringCriteria(queryStringCriteria);
};

const changeHandler: React.ChangeEventHandler<HTMLInputElement> = ({
target,
}) => {
Expand All @@ -60,44 +102,14 @@ const FastSearchField = (
}, [firstColumnCriteria]);

useEffect(() => {
const timeOutId = setTimeout(() => {
if (firstColumnCriteria) {
if (firstColumnCriteria.value == value) {
return;
}

if (value !== '') {
firstColumnCriteria.value = encodeURIComponent(value);
}
if (isDatetime) {
return;
}

let match = false;
let matchIdx: string | undefined;
for (const idx in queryStringCriteria) {
if (queryStringCriteria[idx].name !== firstColumnCriteria.name) {
continue;
}

if (queryStringCriteria[idx].type !== firstColumnCriteria.type) {
continue;
}

queryStringCriteria[idx] = firstColumnCriteria;
matchIdx = idx;
match = true;
break;
}

if (!match) {
queryStringCriteria.push(firstColumnCriteria);
} else if (value === '' && matchIdx) {
queryStringCriteria.splice(parseInt(matchIdx, 10), 1);
}

setQueryStringCriteria(queryStringCriteria);

return;
}
}, 1000);
const timeOutId = setTimeout(() => {
triggerSearchIfChanged();
return;
}, 2000);
return () => clearTimeout(timeOutId);
}, [value, firstColumnCriteria]);

Expand Down Expand Up @@ -126,8 +138,13 @@ const FastSearchField = (
);
}

const type =
firstColumnSpec.format === 'date-time' ? 'datetime-local' : 'text';
let type = 'text';
const inputProps = {} as InputBaseComponentProps;

if (isDatetime) {
type = 'datetime-local';
inputProps.step = 1;
}

return (
<StyledSearchTextField
Expand All @@ -136,10 +153,24 @@ const FastSearchField = (
error={false}
value={value}
errorMsg=''
inputProps={{}}
inputProps={inputProps}
InputProps={{
startAdornment: <SearchIcon />,
}}
onBlur={() => {
if (!isDatetime) {
return;
}

triggerSearchIfChanged();
}}
onKeyDown={(event) => {
if (event.code !== 'Enter') {
return;
}

triggerSearchIfChanged();
}}
placeholder='Search'
hasChanged={false}
onChange={changeHandler}
Expand Down
17 changes: 17 additions & 0 deletions library/src/services/form/Field/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import {
FormHelperText,
OutlinedInput,
TextFieldProps as MuiTextFieldProps,
OutlinedInputProps,
} from '@mui/material';
import { StyledHelpTextTooltip } from '../Shared/HelpText.styles';
import { KeyboardEventHandler } from 'react';

export type TextFieldProps = MuiTextFieldProps & {
hasChanged: boolean;
Expand All @@ -29,6 +31,8 @@ export const TextField = (props: TextFieldProps) => {
required,
onChange,
onBlur,
onKeyDown,
onClick,
error,
errorMsg,
helperText,
Expand All @@ -48,6 +52,18 @@ export const TextField = (props: TextFieldProps) => {
const labelId = `${name}-label`;
const maxRows = multiline ? 6 : undefined;

type passThroughPropsType = Partial<OutlinedInputProps>;
const passThroughProps: passThroughPropsType = {};

if (onKeyDown) {
passThroughProps.onKeyDown = onKeyDown as KeyboardEventHandler<
HTMLTextAreaElement | HTMLInputElement
>;
}
if (onClick) {
passThroughProps.onClick = onClick;
}

return (
<FormControl
variant='standard'
Expand All @@ -72,6 +88,7 @@ export const TextField = (props: TextFieldProps) => {
</label>
)}
<OutlinedInput
{...passThroughProps}
ref={InputProps?.ref}
name={name}
type={type}
Expand Down

0 comments on commit e0e748b

Please sign in to comment.