Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit 93f2982

Browse files
authored
fix: LSDV-5066: Filter row validation to set filter just if value is added (#1372)
* fix: LSDV-5066: Filter row validation to set filter just if value is added * fix when user deletes a filter row --------- Co-authored-by: juliosgarbi <[email protected]>
1 parent 9578b82 commit 93f2982

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

src/components/Filter/Filter.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ export const Filter: FC<FilterInterface> = ({
6565

6666
newList.splice(index, 1);
6767

68+
if (newList[0]) {
69+
newList[0].logic = 'and';
70+
}
71+
6872
onChange(FilterItems(filterData, newList));
6973

7074
return newList;

src/components/Filter/FilterDropdown.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const FilterDropdown: FC<FilterDropdownInterface> = ({
4949
backgroundColor: '#fafafa',
5050
...(style ?? {}),
5151
}}
52-
onChange={(value) => onChange(items.findIndex((item) => (item.key ?? item.label) === value))}
52+
onChange={(value) => onChange(value)}
5353
size='small'
5454
>
5555
{items.map(renderOptions)}

src/components/Filter/FilterRow.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ export const FilterRow: FC<FilterRowInterface> = ({
4040
}, [_selectedField]);
4141

4242
useEffect(() => {
43-
if(!isDefined(_selectedOperation) || _selectedOperation < 0) return;
44-
const _filterInputs = FilterInputs?.[availableFilters[_selectedField].type][_selectedOperation];
43+
const _operationItems = FilterInputs?.[availableFilters[_selectedField].type];
44+
const _operation = _operationItems.findIndex((item:any) => (item.key ?? item.label) === _selectedOperation);
45+
46+
if(!isDefined(_operation) || _operation < 0) return;
47+
const _filterInputs = FilterInputs?.[availableFilters[_selectedField].type][_operation];
4548

4649
onChange(index, { operation: _filterInputs?.key });
4750
setInputComponent(_filterInputs?.input);
@@ -57,7 +60,7 @@ export const FilterRow: FC<FilterRowInterface> = ({
5760
dataTestid={'logic-dropdown'}
5861
style={{ width: '60px' }}
5962
onChange={(value: any) => {
60-
onChange(index, { logic:logicItems[value].key });
63+
onChange(index, { logic:value });
6164
}}
6265
/>
6366
)}
@@ -69,9 +72,9 @@ export const FilterRow: FC<FilterRowInterface> = ({
6972
dataTestid={'field-dropdown'}
7073
style={{ width: '140px' }}
7174
onChange={(value: any) => {
72-
setSelectedField(value);
75+
setSelectedField(availableFilters.findIndex((item:any) => (item.key ?? item.label) === value));
7376

74-
onChange(index, { value:'' });
77+
onChange(index, { value:null });
7578
}}
7679
/>
7780
</Elem>

src/components/Filter/filter-util.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export const FilterItems = (items: any[], filterList: FilterListInterface[]) =>
3838
const _filteredList = [[...items]];
3939

4040
for(let i = 0; i < filterList.length; i++) {
41+
if (!filterList[i].value && filterList[i].operation !== 'empty') continue;
42+
4143
if (filterList[i].logic === 'and') { // 0 is equal to AND, 1 is equal to OR
4244
_filteredList[_filteredList.length - 1] = FilterItemsByOperation(_filteredList[_filteredList.length - 1], filterList[i]);
4345
} else {

tests/functional/specs/outliner/filter.cy.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,54 @@ describe('Filter outliner scenario', () => {
252252
cy.contains('There are 4 hidden regions').should('be.visible');
253253

254254
});
255+
256+
it('Shouldnt show all items if second filter is set to OR and doesnt have any value added', () => {
257+
LabelStudio.init({
258+
config,
259+
task,
260+
});
261+
262+
LabelStudio.setFeatureFlagsOnPageLoad({
263+
[FF_LSDV_3025]: true,
264+
});
265+
266+
cy.get('[data-testid="filter-button"]').click();
267+
cy.contains('Add Filter').click();
268+
cy.get('[data-testid="operation-dropdown"]').click();
269+
cy.contains('contains').click();
270+
cy.get('[data-testid="filter-input"]').type('Moonwalker ');
271+
cy.contains('There are 2 hidden regions').should('be.visible');
272+
cy.contains('Add Another Filter').click();
273+
cy.get('[data-testid="logic-dropdown"]').click();
274+
cy.get('.lsf-select__list').contains('Or').click();
275+
276+
Sidebar.hasRegions(2);
277+
});
278+
279+
it('Should remove the first filter rule if its deleted', () => {
280+
LabelStudio.init({
281+
config,
282+
task,
283+
});
284+
285+
LabelStudio.setFeatureFlagsOnPageLoad({
286+
[FF_LSDV_3025]: true,
287+
});
288+
289+
cy.get('[data-testid="filter-button"]').click();
290+
cy.contains('Add Filter').click();
291+
cy.get('[data-testid="operation-dropdown"]').click();
292+
cy.contains('contains').click();
293+
cy.get('[data-testid="filter-input"]').type('Moonwalker ');
294+
cy.contains('There are 2 hidden regions').should('be.visible');
295+
cy.contains('Add Another Filter').click();
296+
cy.get('[data-testid="logic-dropdown"]').click();
297+
cy.get('.lsf-select__list').contains('Or').click();
298+
cy.contains('Select value').click();
299+
cy.get('.lsf-select__dropdown.lsf-visible > .lsf-select__list').contains('contains').click();
300+
cy.get('[data-testid="filter-input"]').eq(1).type('Planet');
301+
Sidebar.hasRegions(3);
302+
cy.get('[data-testid="delete-row-0"]').click();
303+
Sidebar.hasRegions(1);
304+
});
255305
});

0 commit comments

Comments
 (0)