Skip to content

Commit

Permalink
SHARED(Frontend): More intuitive autocomplete behaviour.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoivisto committed Oct 20, 2023
1 parent b82c28b commit 2347971
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
6 changes: 6 additions & 0 deletions frontend/packages/shared/CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Released]

## [1.9.29] - 2023-10-20

### Changed

- Changed behaviour of ComboBox autocomplete: spaces are trimmed from search input and matching is only performed against the start of option labels

## [1.9.28] - 2023-10-05

### Changed
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@opetushallitus/kieli-ja-kaantajatutkinnot.shared",
"version": "1.9.28",
"version": "1.9.29",
"description": "Shared Frontend Package",
"exports": {
"./components": "./src/components/index.tsx",
Expand Down
18 changes: 17 additions & 1 deletion frontend/packages/shared/src/components/ComboBox/ComboBox.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
Autocomplete,
AutocompleteProps,
createFilterOptions,
FilterOptionsState,
FormControl,
FormHelperText,
TextField,
Expand All @@ -23,7 +25,11 @@ interface ComboBoxProps {

type AutoCompleteComboBox = Omit<
AutocompleteProps<AutocompleteValue, false, false, false>,
'options' | 'renderInput'
| 'options'
| 'renderInput'
| 'getOptionLabel'
| 'isOptionEqualToValue'
| 'filterOptions'
>;

const compareOptionLabels = (a: ComboBoxOption, b: ComboBoxOption) => {
Expand All @@ -47,6 +53,14 @@ const isOptionEqualToValue = (
}
};

const filterOptions: (
options: Array<AutocompleteValue>,
state: FilterOptionsState<AutocompleteValue>
) => Array<AutocompleteValue> = createFilterOptions({
matchFrom: 'start',
trim: true,
});

export const valueAsOption = (value: string) => ({
value: value,
label: value,
Expand Down Expand Up @@ -74,6 +88,7 @@ export const ComboBox = ({
getOptionLabel={getOptionLabel}
isOptionEqualToValue={isOptionEqualToValue}
options={values}
filterOptions={filterOptions}
renderInput={(params) => (
<TextField
{...params}
Expand Down Expand Up @@ -119,6 +134,7 @@ export const LabeledComboBox = ({
getOptionLabel={getOptionLabel}
isOptionEqualToValue={isOptionEqualToValue}
options={values}
filterOptions={filterOptions}
renderInput={(params) => (
<TextField
{...params}
Expand Down

0 comments on commit 2347971

Please sign in to comment.