fix: match-sorter for a better drop-down sorting

fix: autocoplete inerOnBlur
fix: revert autocomplete limit api
fix: no need for custom filterOptions
fix: eslint
This commit is contained in:
Mauricio Navarro Miranda 2020-07-25 13:15:53 -05:00 committed by Josh Harvey
parent d1c1734dec
commit ef9f8b49a6
5 changed files with 48 additions and 11 deletions

View file

@ -15831,6 +15831,30 @@
"unquote": "^1.1.0" "unquote": "^1.1.0"
} }
}, },
"match-sorter": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/match-sorter/-/match-sorter-4.2.0.tgz",
"integrity": "sha512-oEvLn8R+a30YZ9l5XdCTkYQuLsOs8frxEqQTAuxoqkQx/qV5pQpx/NqAWvJ5xbYecqfXoF/ZevaIS1+NkbRymg==",
"requires": {
"@babel/runtime": "^7.10.5",
"remove-accents": "0.4.2"
},
"dependencies": {
"@babel/runtime": {
"version": "7.10.5",
"resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.10.5.tgz",
"integrity": "sha512-otddXKhdNn7d0ptoFRHtMLa8LqDxLYwTjB4nYgM1yy5N6gU/MUf8zqyyLltCH3yAVitBzmwK4us+DD0l/MauAg==",
"requires": {
"regenerator-runtime": "^0.13.4"
}
},
"regenerator-runtime": {
"version": "0.13.7",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.7.tgz",
"integrity": "sha512-a54FxoJDIr27pgf7IgeQGxmqUNYrcV338lf/6gH456HZ/PhX+5BcwHXG9ajESmwe6WRO0tAzRUrRmNONWgkrew=="
}
}
},
"material-colors": { "material-colors": {
"version": "1.2.6", "version": "1.2.6",
"resolved": "https://registry.npmjs.org/material-colors/-/material-colors-1.2.6.tgz", "resolved": "https://registry.npmjs.org/material-colors/-/material-colors-1.2.6.tgz",
@ -22230,6 +22254,11 @@
"integrity": "sha1-VNvzd+UUQKypCkzSdGANP/LYiKk=", "integrity": "sha1-VNvzd+UUQKypCkzSdGANP/LYiKk=",
"dev": true "dev": true
}, },
"remove-accents": {
"version": "0.4.2",
"resolved": "https://registry.npmjs.org/remove-accents/-/remove-accents-0.4.2.tgz",
"integrity": "sha1-CkPTqq4egNuRngeuJUsoXZ4ce7U="
},
"remove-trailing-separator": { "remove-trailing-separator": {
"version": "1.1.0", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz", "resolved": "https://registry.npmjs.org/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz",

View file

@ -24,6 +24,7 @@
"graphql-tag": "^2.10.3", "graphql-tag": "^2.10.3",
"jss-plugin-extend": "^10.0.0", "jss-plugin-extend": "^10.0.0",
"libphonenumber-js": "^1.7.50", "libphonenumber-js": "^1.7.50",
"match-sorter": "^4.2.0",
"moment": "2.24.0", "moment": "2.24.0",
"qrcode.react": "0.9.3", "qrcode.react": "0.9.3",
"ramda": "^0.26.1", "ramda": "^0.26.1",

View file

@ -1,13 +1,12 @@
import MAutocomplete, { import MAutocomplete from '@material-ui/lab/Autocomplete'
createFilterOptions import sort from 'match-sorter'
} from '@material-ui/lab/Autocomplete'
import * as R from 'ramda' import * as R from 'ramda'
import React from 'react' import React from 'react'
import TextInput from './TextInput' import TextInput from './TextInput'
const Autocomplete = ({ const Autocomplete = ({
limit = 5, limit = 5, // set limit = null for no limit
options, options,
label, label,
valueProp, valueProp,
@ -43,6 +42,12 @@ const Autocomplete = ({
onChange(evt, rValue) onChange(evt, rValue)
} }
const filterOptions = (options, { inputValue }) =>
sort(options, inputValue, { keys: ['code', 'display'] }).slice(
0,
R.defaultTo(undefined)(limit)
)
return ( return (
<MAutocomplete <MAutocomplete
options={options} options={options}
@ -51,7 +56,7 @@ const Autocomplete = ({
onChange={innerOnChange} onChange={innerOnChange}
getOptionLabel={getLabel} getOptionLabel={getLabel}
forcePopupIcon={false} forcePopupIcon={false}
filterOptions={createFilterOptions({ ignoreAccents: true, limit })} filterOptions={filterOptions}
openOnFocus openOnFocus
autoHighlight autoHighlight
disableClearable disableClearable

View file

@ -8,18 +8,23 @@ const AutocompleteFormik = ({ options, ...props }) => {
const [open, setOpen] = useState(false) const [open, setOpen] = useState(false)
const { name, onBlur, value } = props.field const { name, onBlur, value } = props.field
const { touched, errors, setFieldValue } = props.form const { touched, errors, setFieldValue, setFieldTouched } = props.form
const error = !!(touched[name] && errors[name]) const error = !!(touched[name] && errors[name])
const { initialValues } = useFormikContext() const { initialValues } = useFormikContext()
const innerOptions = const innerOptions =
R.type(options) === 'Function' ? options(initialValues) : options R.type(options) === 'Function' ? options(initialValues) : options
const innerOnBlur = event => {
name && setFieldTouched(name, true)
onBlur && onBlur(event)
}
return ( return (
<Autocomplete <Autocomplete
name={name} name={name}
onChange={(event, item) => setFieldValue(name, item)} onChange={(event, item) => setFieldValue(name, item)}
onBlur={onBlur} onBlur={innerOnBlur}
value={value} value={value}
error={error} error={error}
open={open} open={open}

View file

@ -1,4 +1,3 @@
import { createFilterOptions } from '@material-ui/lab/Autocomplete'
import * as R from 'ramda' import * as R from 'ramda'
import * as Yup from 'yup' import * as Yup from 'yup'
@ -94,9 +93,7 @@ const allFields = getData => {
valueProp: 'code', valueProp: 'code',
getLabel: R.path(['code']), getLabel: R.path(['code']),
multiple: true, multiple: true,
filterOptions: createFilterOptions({ limit: null
stringify: option => `${option.code} ${option.display}`
})
} }
} }
] ]