-
Notifications
You must be signed in to change notification settings - Fork 33
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
Fix multiselect in ComboBox #161
Open
jakubsob
wants to merge
19
commits into
main
Choose a base branch
from
fix/160-multiselect-combobox
base: main
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 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
dd4e66b
fix: multiselect in ComboBox
a89d632
refactor: extract multiselect logic to function
90bb3bc
fix: first selection in ComboBox
0ee96aa
chore: build js
d4412b1
fix: default value in ComboBox
fa81192
chore: update browserslist
6608a73
feat: support freeform in multiselect ComboBox
52fb740
feat: select freeform option by default in multiselect ComboBox
d0fcec9
chore: build js
7107624
chore: install shiny.react from remote
ddea2ab
fix: e2e tests app
29796a8
fix: display text after server side update
6cadc1a
chore: build js
5b31b1c
feat: install shiny.react in ci
e6ca47c
feat: add github token to install shiny.react from github
3308cbf
fix: js linter errors
3ea5a50
chore: update ci
914518c
fix: ci
f43d10c
refactor: restore e2e test for multiselect dropdown
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 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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,5 +1,26 @@ | ||||||||||
import * as Fluent from '@fluentui/react'; | ||||||||||
import { ButtonAdapter, InputAdapter, debounce } from '@/shiny.react'; | ||||||||||
import { useState } from 'react'; | ||||||||||
|
||||||||||
function handleMultiSelect(option, selectedKeys, propsOptions) { | ||||||||||
const options = new Set(propsOptions.map((item) => item.key)); | ||||||||||
let currentSelectedOptionKeys = (Array.isArray(selectedKeys) ? selectedKeys : [selectedKeys]) | ||||||||||
.filter((key) => options.has(key)); // Some options might have been removed. | ||||||||||
// If option doesn't have selected property it comes from freeform, so it's selected by default. | ||||||||||
const selected = option.selected ?? true; | ||||||||||
currentSelectedOptionKeys = selected | ||||||||||
? [...currentSelectedOptionKeys, option.key] | ||||||||||
: currentSelectedOptionKeys.filter((key) => key !== option.key); | ||||||||||
return currentSelectedOptionKeys; | ||||||||||
} | ||||||||||
|
||||||||||
function getSelectedOptionText(options, value, delimiter = ', ') { | ||||||||||
const selectedKeys = new Set(value); | ||||||||||
return options | ||||||||||
.filter((option) => selectedKeys.has(option.key)) | ||||||||||
.map((option) => option?.text) | ||||||||||
.join(delimiter); | ||||||||||
} | ||||||||||
|
||||||||||
export const ActionButton = ButtonAdapter(Fluent.ActionButton); | ||||||||||
export const CommandBarButton = ButtonAdapter(Fluent.CommandBarButton); | ||||||||||
|
@@ -29,11 +50,26 @@ export const ColorPicker = InputAdapter(Fluent.ColorPicker, (value, setValue) => | |||||||||
onChange: (e, v) => setValue(v.str), | ||||||||||
}), { policy: debounce, delay: 250 }); | ||||||||||
|
||||||||||
export const ComboBox = InputAdapter(Fluent.ComboBox, (value, setValue) => ({ | ||||||||||
selectedKey: value && value.key, | ||||||||||
text: value && value.text, | ||||||||||
onChange: (e, option, i, text) => setValue(option || (text ? { text } : null)), | ||||||||||
}), { policy: debounce, delay: 250 }); | ||||||||||
export const ComboBox = InputAdapter(Fluent.ComboBox, (value, setValue, props) => { | ||||||||||
const [options, setOptions] = useState(props.options); | ||||||||||
return ({ | ||||||||||
selectedKey: value, | ||||||||||
text: getSelectedOptionText(options, value, props.multiSelectDelimiter || ', ') || value, | ||||||||||
Comment on lines
+56
to
+57
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. Should we keep the
Suggested change
|
||||||||||
options, | ||||||||||
onChange: (_event, option, _index, text) => { | ||||||||||
let key = option?.key || text; | ||||||||||
const newOption = option || { key, text }; | ||||||||||
// Add new option if freeform is allowed | ||||||||||
if (props?.allowFreeform && !option && text) { | ||||||||||
setOptions((prevOptions) => [...prevOptions, newOption]); | ||||||||||
} | ||||||||||
if (props.multiSelect) { | ||||||||||
key = handleMultiSelect(newOption, value, options); | ||||||||||
} | ||||||||||
setValue(key); | ||||||||||
}, | ||||||||||
}); | ||||||||||
}, { policy: debounce, delay: 250 }); | ||||||||||
|
||||||||||
export const DatePicker = InputAdapter(Fluent.DatePicker, (value, setValue) => ({ | ||||||||||
value: value ? new Date(value) : undefined, | ||||||||||
|
@@ -45,13 +81,7 @@ export const Dropdown = InputAdapter(Fluent.Dropdown, (value, setValue, props) = | |||||||||
selectedKey: value, | ||||||||||
onChange: (e, v) => { | ||||||||||
if (props.multiSelect) { | ||||||||||
const options = new Set(props.options.map((item) => item.key)); | ||||||||||
let newValue = (Array.isArray(value) ? value : [value]) | ||||||||||
.filter((key) => options.has(key)); // Some options might have been removed. | ||||||||||
newValue = v.selected | ||||||||||
? [...newValue, v.key] | ||||||||||
: newValue.filter((key) => key !== v.key); | ||||||||||
setValue(newValue); | ||||||||||
setValue(handleMultiSelect(v, value, props.options)); | ||||||||||
} else { | ||||||||||
setValue(v.key); | ||||||||||
} | ||||||||||
|
This file contains 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
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.
Why did you remove these tests? They work as intended
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.
Well, I started observing some intermittency in those tests, which I don't quite understand. It seems that now it takes a bit longer to spin up the test app and that's why some fail because components are not yet rendered. For this multiselect when I run it interactively everything works fine, but on the whole Cypress run it tends to fail -- I have big troubles debugging it.
I'd prefer to refactor those tests to be more isolated, as all components being put in one big app seems to be the root cause of this intermittency. But for now I'm a bit stuck with this test, I'll give it one more try
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.
I'm confused @averissimo… Why it passed now when if didn't on multiple runs previously?
I restored multiselect tests, but since it required some changes in shiny.react, here is a PR that enables this fix, PTAL.
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.
I agree with refactoring. But that's out of scope for this PR 😁
The main issue I found was that it was running the test sequentially and keeping the state between tests. (hence the
beforeEach(() => cy.reload())
I had on my diff.I've approved the Appsilon/shiny.react#75