Skip to content

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
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"homepage": "https://github.com/SoftwareBrothers/adminjs#readme",
"dependencies": {
"@adminjs/design-system": "^4.1.0",
"@adminjs/themes": "^1.0.1",
Copy link
Author

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

Copy link
Contributor

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 local adminjs

Copy link
Author

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

"@babel/core": "^7.23.9",
"@babel/parser": "^7.23.9",
"@babel/plugin-syntax-import-assertions": "^7.23.3",
Expand Down
84 changes: 81 additions & 3 deletions src/frontend/components/property-type/default-type/filter.tsx
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,
Copy link
Author

Choose a reason for hiding this comment

The 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,
Expand All @@ -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}
Expand Down
Loading