-
Notifications
You must be signed in to change notification settings - Fork 715
feat(filter): add support for more operators than contains #1750
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
Open
roggervalf
wants to merge
8
commits into
SoftwareBrothers:master
Choose a base branch
from
roggervalf:feat/filter-operators
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
81e3bc3
feat(filter): add support for more operators than contains
roggervalf 0046d1b
refactor: keep input value updated
roggervalf 195c672
refactor: remove other filters for same attribute
roggervalf efd3f9e
refactor: keep filters updated when refreshing page
roggervalf f94028c
chore: update style
roggervalf b60a077
refactor: only apply operator when property type is string
roggervalf c4650b3
feat: support or operator
roggervalf ee6a76e
chore: remove themes package
roggervalf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,65 @@ | ||
import React from 'react' | ||
import { FormGroup, Input, Select } from '@adminjs/design-system' | ||
import { Box, FormGroup, Input, Select } from '@adminjs/design-system' | ||
|
||
import allowOverride from '../../../hoc/allow-override.js' | ||
import { FilterPropertyProps } from '../base-property-props.js' | ||
import PropertyLabel from '../utils/property-label/property-label.js' | ||
import { useTranslation } from '../../../hooks/use-translation.js' | ||
import * as BackendFilter from '../../../../backend/utils/filter/filter.js' | ||
|
||
const { PARAM_SEPARATOR } = BackendFilter | ||
|
||
const Filter: React.FC<FilterPropertyProps> = (props) => { | ||
const { property, onChange, filter } = props | ||
|
||
const possibleKeys = [ | ||
property.path, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keeping contains key as it was before to prevent other adapters to not be able to recognize it |
||
`${property.path}${PARAM_SEPARATOR}equals`, | ||
`${property.path}${PARAM_SEPARATOR}startsWith`, | ||
`${property.path}${PARAM_SEPARATOR}endsWith`, | ||
] | ||
|
||
const [currentKey, currentInput] = Object.entries(filter).find( | ||
([key]) => possibleKeys.includes(key), | ||
) || [] | ||
const currentOperator = (currentKey ? currentKey.split(PARAM_SEPARATOR)[1] : 'contains') || 'contains' | ||
|
||
const { tl } = useTranslation() | ||
|
||
const handleInputChange = (event) => { | ||
onChange(property.path, event.target.value) | ||
} | ||
|
||
const handleInputInComboChange = (event) => { | ||
if (currentOperator === 'contains') { | ||
onChange(property.path, event.target.value) | ||
} else { | ||
const key = `${property.path}${PARAM_SEPARATOR}${currentOperator}` | ||
onChange(key, event.target.value) | ||
} | ||
} | ||
|
||
const handleSelectChange = (selected) => { | ||
const value = selected ? selected.value : '' | ||
onChange(property.path, value) | ||
} | ||
|
||
const handleSelectInComboChange = (selected) => { | ||
const changedKey = ((selected?.value || 'contains') !== 'contains') ? `${property.path}${PARAM_SEPARATOR}${selected.value}` : property.path | ||
|
||
possibleKeys.forEach((key) => { | ||
if (key !== changedKey) { | ||
delete filter[key] | ||
} | ||
}) | ||
onChange(changedKey, currentInput || '') | ||
} | ||
|
||
const renderInput = () => { | ||
const filterKey = `filter-${property.path}` | ||
const value = filter[property.path] || '' | ||
const valueKey = currentOperator === 'contains' ? property.path : `${property.path}${PARAM_SEPARATOR}${currentOperator}` | ||
|
||
const filterKey = `filter-${valueKey}` | ||
const value = filter[valueKey] || '' | ||
if (property.availableValues) { | ||
const availableValues = property.availableValues.map((v) => ({ | ||
...v, | ||
|
@@ -40,6 +78,46 @@ const Filter: React.FC<FilterPropertyProps> = (props) => { | |
/> | ||
) | ||
} | ||
|
||
if (property.type === 'string') { | ||
const operator = { label: currentOperator, value: currentOperator } | ||
return ( | ||
<Box flex flexDirection="row"> | ||
<Box flexGrow={0}> | ||
<Input | ||
name={filterKey} | ||
onChange={handleInputInComboChange} | ||
value={filter[currentKey || property.path] || ''} | ||
/> | ||
</Box> | ||
<Box flexGrow={1}> | ||
<Select | ||
value={operator} | ||
options={[ | ||
{ | ||
label: 'contains', | ||
value: 'contains', | ||
}, | ||
{ | ||
label: 'equals', | ||
value: 'equals', | ||
}, | ||
{ | ||
label: 'startsWith', | ||
value: 'startsWith', | ||
}, | ||
{ | ||
label: 'endsWith', | ||
value: 'endsWith', | ||
}, | ||
]} | ||
onChange={handleSelectInComboChange} | ||
/> | ||
</Box> | ||
</Box> | ||
) | ||
} | ||
|
||
return ( | ||
<Input | ||
name={filterKey} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added as when using example repo https://github.com/SoftwareBrothers/adminjs-example-app fails when this package is linked and this package failed because this dependency does not exist
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will result in a circular dependency issue because
adminjs
is a peer dependency of@adminjs/themes
.adminjs-example-app
is also an outdated repository.Local linking issue can be solved by linking
@adminjs/themes
in your local app and in localadminjs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good to know, I'll remove this package though