-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
chore: Merges latest Select changes #16587
Merged
michael-s-molina
merged 1 commit into
apache:master
from
michael-s-molina:merge-select-changes
Sep 7, 2021
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,8 +49,12 @@ type PickedSelectProps = Pick< | |
| 'autoFocus' | ||
| 'disabled' | ||
| 'filterOption' | ||
| 'labelInValue' | ||
| 'loading' | ||
| 'notFoundContent' | ||
| 'onChange' | ||
| 'onClear' | ||
| 'onFocus' | ||
| 'placeholder' | ||
| 'showSearch' | ||
| 'value' | ||
|
@@ -73,6 +77,7 @@ export interface SelectProps extends PickedSelectProps { | |
allowNewOptions?: boolean; | ||
ariaLabel: string; | ||
header?: ReactNode; | ||
lazyLoading?: boolean; | ||
mode?: 'single' | 'multiple'; | ||
name?: string; // discourage usage | ||
options: OptionsType | OptionsPagePromise; | ||
|
@@ -84,15 +89,11 @@ export interface SelectProps extends PickedSelectProps { | |
const StyledContainer = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
width: 100%; | ||
`; | ||
|
||
const StyledSelect = styled(AntdSelect, { | ||
shouldForwardProp: prop => prop !== 'hasHeader', | ||
})<{ hasHeader: boolean }>` | ||
${({ theme, hasHeader }) => ` | ||
width: 100%; | ||
margin-top: ${hasHeader ? theme.gridUnit : 0}px; | ||
|
||
const StyledSelect = styled(AntdSelect)` | ||
${({ theme }) => ` | ||
&& .ant-select-selector { | ||
border-radius: ${theme.gridUnit}px; | ||
} | ||
|
@@ -163,8 +164,12 @@ const Select = ({ | |
filterOption = true, | ||
header = null, | ||
invertSelection = false, | ||
labelInValue = false, | ||
lazyLoading = true, | ||
loading = false, | ||
mode = 'single', | ||
name, | ||
onChange, | ||
options, | ||
pageSize = DEFAULT_PAGE_SIZE, | ||
placeholder = t('Select ...'), | ||
|
@@ -182,13 +187,13 @@ const Select = ({ | |
); | ||
const [selectValue, setSelectValue] = useState(value); | ||
const [searchedValue, setSearchedValue] = useState(''); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [isLoading, setIsLoading] = useState(loading); | ||
const [isTyping, setIsTyping] = useState(false); | ||
const [error, setError] = useState(''); | ||
const [isDropdownVisible, setIsDropdownVisible] = useState(false); | ||
const [page, setPage] = useState(0); | ||
const [totalCount, setTotalCount] = useState(0); | ||
const [loadingEnabled, setLoadingEnabled] = useState(false); | ||
const [loadingEnabled, setLoadingEnabled] = useState(!lazyLoading); | ||
const fetchedQueries = useRef(new Map<string, number>()); | ||
const mappedMode = isSingleMode | ||
? undefined | ||
|
@@ -197,16 +202,21 @@ const Select = ({ | |
: 'multiple'; | ||
|
||
useEffect(() => { | ||
fetchedQueries.current.clear(); | ||
setSelectOptions( | ||
options && Array.isArray(options) ? options : EMPTY_OPTIONS, | ||
); | ||
}, [options]); | ||
|
||
useEffect(() => { | ||
if (isAsync && value) { | ||
const array: AntdLabeledValue[] = Array.isArray(value) | ||
? (value as AntdLabeledValue[]) | ||
: [value as AntdLabeledValue]; | ||
setSelectValue(value); | ||
}, [value]); | ||
|
||
useEffect(() => { | ||
if (isAsync && selectValue) { | ||
const array: AntdLabeledValue[] = Array.isArray(selectValue) | ||
? (selectValue as AntdLabeledValue[]) | ||
: [selectValue as AntdLabeledValue]; | ||
const options: AntdLabeledValue[] = []; | ||
array.forEach(element => { | ||
const found = selectOptions.find( | ||
|
@@ -220,23 +230,20 @@ const Select = ({ | |
setSelectOptions([...selectOptions, ...options]); | ||
} | ||
} | ||
}, [isAsync, selectOptions, value]); | ||
|
||
useEffect(() => { | ||
setSelectValue(value); | ||
}, [value]); | ||
}, [isAsync, selectOptions, selectValue]); | ||
|
||
const handleTopOptions = useCallback( | ||
(selectedValue: AntdSelectValue | undefined) => { | ||
// bringing selected options to the top of the list | ||
if (selectedValue !== undefined && selectedValue !== null) { | ||
const isLabeledValue = isAsync || labelInValue; | ||
const topOptions: OptionsType = []; | ||
const otherOptions: OptionsType = []; | ||
|
||
selectOptions.forEach(opt => { | ||
let found = false; | ||
if (Array.isArray(selectedValue)) { | ||
if (isAsync) { | ||
if (isLabeledValue) { | ||
found = | ||
(selectedValue as AntdLabeledValue[]).find( | ||
element => element.value === opt.value, | ||
|
@@ -245,7 +252,7 @@ const Select = ({ | |
found = selectedValue.includes(opt.value); | ||
} | ||
} else { | ||
found = isAsync | ||
found = isLabeledValue | ||
? (selectedValue as AntdLabeledValue).value === opt.value | ||
: selectedValue === opt.value; | ||
} | ||
|
@@ -265,10 +272,10 @@ const Select = ({ | |
!topOptions.find( | ||
tOpt => | ||
tOpt.value === | ||
(isAsync ? (val as AntdLabeledValue)?.value : val), | ||
(isLabeledValue ? (val as AntdLabeledValue)?.value : val), | ||
) | ||
) { | ||
if (isAsync) { | ||
if (isLabeledValue) { | ||
const labelValue = val as AntdLabeledValue; | ||
topOptions.push({ | ||
label: labelValue.label, | ||
|
@@ -288,7 +295,7 @@ const Select = ({ | |
} | ||
} | ||
}, | ||
[isAsync, isSingleMode, selectOptions], | ||
[isAsync, isSingleMode, labelInValue, selectOptions], | ||
); | ||
|
||
const handleOnSelect = ( | ||
|
@@ -405,6 +412,10 @@ const Select = ({ | |
const newOptions = [...selectOptions, newOption]; | ||
setSelectOptions(newOptions); | ||
setSelectValue(searchValue); | ||
|
||
if (onChange) { | ||
onChange(searchValue, newOptions); | ||
} | ||
} | ||
} | ||
setSearchedValue(searchValue); | ||
|
@@ -416,6 +427,7 @@ const Select = ({ | |
allowNewOptions, | ||
initialOptions, | ||
isSingleMode, | ||
onChange, | ||
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. @geido |
||
searchedValue, | ||
selectOptions, | ||
], | ||
|
@@ -489,6 +501,12 @@ const Select = ({ | |
} | ||
}, [handleTopOptions, isSingleMode, selectValue]); | ||
|
||
useEffect(() => { | ||
if (loading !== undefined && loading !== isLoading) { | ||
setIsLoading(loading); | ||
} | ||
}, [isLoading, loading]); | ||
|
||
const dropdownRender = ( | ||
originNode: ReactElement & { ref?: RefObject<HTMLElement> }, | ||
) => { | ||
|
@@ -521,12 +539,11 @@ const Select = ({ | |
<StyledContainer> | ||
{header} | ||
<StyledSelect | ||
hasHeader={!!header} | ||
aria-label={ariaLabel || name} | ||
dropdownRender={dropdownRender} | ||
filterOption={handleFilterOption} | ||
getPopupContainer={triggerNode => triggerNode.parentNode} | ||
labelInValue={isAsync} | ||
labelInValue={isAsync || labelInValue} | ||
maxTagCount={MAX_TAG_COUNT} | ||
mode={mappedMode} | ||
onDeselect={handleOnDeselect} | ||
|
@@ -536,6 +553,7 @@ const Select = ({ | |
onSearch={shouldShowSearch ? handleOnSearch : undefined} | ||
onSelect={handleOnSelect} | ||
onClear={() => setSelectValue(undefined)} | ||
onChange={onChange} | ||
options={selectOptions} | ||
placeholder={placeholder} | ||
showSearch={shouldShowSearch} | ||
|
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.
@geido I didn't add
defaultValue
assuming that we can usevalue
.