-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add create new option, add query for missing elements in combobobx #6278
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
23fa824
wip: add missing create new option, add query when no elements are found
witoszekdev 4e037a0
add method to transform option, add more comments
witoszekdev e16a88f
add tests
witoszekdev f842dbe
Merge branch 'main' into fix-combobox-search
witoszekdev 92ae6d1
remove failing tests covered by macaw
witoszekdev 183c4f8
Apply suggestions from code review
witoszekdev 38f6b18
update complicated mock for react-intl
witoszekdev 3e409df
update test with better description
witoszekdev 6fefaec
Add changeset
witoszekdev 6ed2736
fix failing test add better description
witoszekdev 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { BasicAttributeRow } from "@dashboard/components/Attributes/BasicAttributeRow"; | ||
| import { getErrorMessage, getSingleDisplayValue } from "@dashboard/components/Attributes/utils"; | ||
| import { | ||
| AttributeValueFragment, | ||
| PageErrorWithAttributesFragment, | ||
| ProductErrorWithAttributesFragment, | ||
| } from "@dashboard/graphql"; | ||
| import { DynamicCombobox, Option } from "@saleor/macaw-ui-next"; | ||
| import { useState } from "react"; | ||
| import { useIntl } from "react-intl"; | ||
|
|
||
| import { AttributeInput } from "./Attributes"; | ||
| import { AttributeRowHandlers } from "./types"; | ||
| import { useAttributeDropdown } from "./useAttributeDropdown"; | ||
|
|
||
| type DropdownRowProps = Pick< | ||
| AttributeRowHandlers, | ||
| "onChange" | "fetchAttributeValues" | "fetchMoreAttributeValues" | ||
| > & { | ||
| attribute: AttributeInput; | ||
| attributeValues: AttributeValueFragment[]; | ||
| disabled: boolean; | ||
| error: ProductErrorWithAttributesFragment | PageErrorWithAttributesFragment; | ||
| onAttributeSelectBlur?: () => void; | ||
| }; | ||
|
|
||
| export const DropdownRow = ({ | ||
| attribute, | ||
| attributeValues, | ||
| disabled, | ||
| error, | ||
| onChange, | ||
| fetchAttributeValues, | ||
| fetchMoreAttributeValues, | ||
| onAttributeSelectBlur, | ||
| }: DropdownRowProps) => { | ||
| const intl = useIntl(); | ||
| const [inputValue, setInputValue] = useState(""); | ||
| const [selectedValue, setSelectedValue] = useState<Option | null>( | ||
| attribute.value[0] | ||
| ? { | ||
| value: attribute.value[0], | ||
| label: getSingleDisplayValue(attribute, attributeValues), | ||
| } | ||
| : null, | ||
| ); | ||
|
|
||
| const { customValueOption, customValueLabel, handleFetchMore, handleInputChange, handleFocus } = | ||
| useAttributeDropdown({ | ||
| inputValue, | ||
| selectedValue, | ||
| fetchOptions: query => fetchAttributeValues(query, attribute.id), | ||
| fetchMore: fetchMoreAttributeValues, | ||
| }); | ||
|
|
||
| const options: Option[] = attributeValues | ||
| .filter(value => value.slug !== null) | ||
| .map(value => ({ | ||
| value: value.slug as string, | ||
| label: value.name ?? value.slug ?? "", | ||
| })); | ||
|
|
||
| const handleOnChange = (option: Option | null) => { | ||
| if (!option) { | ||
| setSelectedValue(null); | ||
| onChange(attribute.id, ""); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const isCustomValue = option.label.includes(customValueLabel); | ||
|
|
||
| if (isCustomValue) { | ||
| const newOption = { label: option.value, value: option.value }; | ||
|
|
||
| setSelectedValue(newOption); | ||
| onChange(attribute.id, option.value); | ||
| setInputValue(""); | ||
| } else { | ||
| setSelectedValue(option); | ||
| onChange(attribute.id, option.value); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <BasicAttributeRow label={attribute.label}> | ||
| <DynamicCombobox | ||
| size="small" | ||
| disabled={disabled} | ||
| options={[...customValueOption, ...options]} | ||
| value={selectedValue} | ||
| error={!!error} | ||
| helperText={getErrorMessage(error, intl)} | ||
| name={`attribute:${attribute.label}`} | ||
| id={`attribute:${attribute.label}`} | ||
| label="" | ||
| onChange={handleOnChange} | ||
| onInputValueChange={value => { | ||
| setInputValue(value); | ||
| handleInputChange(value); | ||
| }} | ||
| onFocus={handleFocus} | ||
| onBlur={onAttributeSelectBlur} | ||
| onScrollEnd={handleFetchMore} | ||
| loading={fetchMoreAttributeValues?.hasMore || fetchMoreAttributeValues?.loading} | ||
witoszekdev marked this conversation as resolved.
Show resolved
Hide resolved
witoszekdev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /> | ||
| </BasicAttributeRow> | ||
| ); | ||
| }; | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
selectedValueis initialized fromattribute.value/attributeValuesonce, but it won’t update if the parent changesattribute.value(e.g. form reset, external update, attributeValues fetched later). This can leave the combobox showing a stale selection. Prefer derivingvaluedirectly from props each render, or add an effect to syncselectedValuewhenattribute.value[0]/attributeValueschange.