Skip to content

Commit

Permalink
Fix filter input types for integer, bigInt, decimal and float field…
Browse files Browse the repository at this point in the history
…s in the UI (keystonejs#7930)

Co-authored-by: Gautam Singh <5769869+gautamsi@users.noreply.github.com>
Co-authored-by: Daniel Cousens <dcousens@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 26, 2022
1 parent 4c08c11 commit 109db89
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-grapes-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': patch
---

Fix `in` and `not_in` filter views for `integer`, `bigInt`, `decimal` and `float` fields
21 changes: 10 additions & 11 deletions packages/core/src/fields/types/bigInt/views/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,19 +227,18 @@ export const controller = (
validate: value =>
validate(value, validation, config.label, hasAutoIncrementDefault) === undefined,
filter: {
Filter(props) {
Filter({ autoFocus, type, onChange, value }) {
return (
<TextInput
// this should not be type=number since it shoud allow commas so the one of/not one of
// filters work but really the whole filtering UI needs to be fixed and just removing type=number
// while doing nothing else would probably make it worse since anything would be allowed in the input
// so when a user applies the filter, the query would return an error
type="number"
onChange={event => {
props.onChange(event.target.value.replace(/[^\d,\s-]/g, ''));
if (type === 'in' || type === 'not_in') {
onChange(event.target.value.replace(/[^\d,\s-]/g, ''));
return;
}
onChange(event.target.value.replace(/[^\d\s-]/g, ''));
}}
value={props.value}
autoFocus={props.autoFocus}
value={value}
autoFocus={autoFocus}
/>
);
},
Expand All @@ -248,8 +247,8 @@ export const controller = (
const valueWithoutWhitespace = value.replace(/\s/g, '');
const parsed =
type === 'in' || type === 'not_in'
? valueWithoutWhitespace.split(',').map(x => BigInt(x))
: BigInt(valueWithoutWhitespace);
? valueWithoutWhitespace.split(',')
: valueWithoutWhitespace;
if (type === 'not') {
return { [config.path]: { not: { equals: parsed } } };
}
Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/fields/types/decimal/views/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,19 @@ export const controller = (
}),
validate: val => validate(val, validation, config.label) === undefined,
filter: {
Filter(props) {
Filter({ autoFocus, type, onChange, value }) {
return (
<TextInput
onChange={event => {
props.onChange(event.target.value.replace(/[^\d\.,\s-]/g, ''));
if (type === 'in' || type === 'not_in') {
onChange(event.target.value.replace(/[^\d,\s-]/g, ''));
return;
}

onChange(event.target.value.replace(/[^\d\s-]/g, ''));
}}
value={props.value}
autoFocus={props.autoFocus}
value={value}
autoFocus={autoFocus}
/>
);
},
Expand Down
12 changes: 8 additions & 4 deletions packages/core/src/fields/types/float/views/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,18 @@ export const controller = (
serialize: value => ({ [config.path]: value.value }),
validate: value => validate(value, config.fieldMeta.validation, config.label) === undefined,
filter: {
Filter(props) {
Filter({ autoFocus, type, onChange, value }) {
return (
<TextInput
onChange={event => {
props.onChange(event.target.value.replace(/[^\d\.,\s-]/g, ''));
if (type === 'in' || type === 'not_in') {
onChange(event.target.value.replace(/[^\d\.,\s-]/g, ''));
return;
}
onChange(event.target.value.replace(/[^\d\.\s-]/g, ''));
}}
value={props.value}
autoFocus={props.autoFocus}
value={value}
autoFocus={autoFocus}
/>
);
},
Expand Down
19 changes: 10 additions & 9 deletions packages/core/src/fields/types/integer/views/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,20 @@ export const controller = (
config.fieldMeta.defaultValue === 'autoincrement'
) === undefined,
filter: {
Filter(props) {
Filter({ autoFocus, type, onChange, value }) {
return (
<TextInput
// this should not be type=number since it shoud allow commas so the one of/not one of
// filters work but really the whole filtering UI needs to be fixed and just removing type=number
// while doing nothing else would probably make it worse since anything would be allowed in the input
// so when a user applies the filter, the query would return an error
type="number"
type="text"
onChange={event => {
props.onChange(event.target.value.replace(/[^\d,\s-]/g, ''));
if (type === 'in' || type === 'not_in') {
onChange(event.target.value.replace(/[^\d,\s-]/g, ''));
return;
}

onChange(event.target.value.replace(/[^\d\s-]/g, ''));
}}
value={props.value}
autoFocus={props.autoFocus}
value={value}
autoFocus={autoFocus}
/>
);
},
Expand Down

0 comments on commit 109db89

Please sign in to comment.