Skip to content

Commit 1b897d3

Browse files
fix: #8510 persistent label for dynamic options in Dropdown
1 parent 75bdea5 commit 1b897d3

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

components/lib/dropdown/Dropdown.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const Dropdown = React.memo(
3131
const virtualScrollerRef = React.useRef(null);
3232
const searchTimeout = React.useRef(null);
3333
const searchValue = React.useRef(null);
34+
const lastSelectedOption = React.useRef(null);
3435
const isLazy = props.virtualScrollerOptions && props.virtualScrollerOptions.lazy;
3536
const hasFilter = ObjectUtils.isNotEmpty(filterState);
3637
const appendTo = props.appendTo || (context && context.appendTo) || PrimeReact.appendTo;
@@ -930,7 +931,24 @@ export const Dropdown = React.memo(
930931
const getSelectedOption = () => {
931932
const index = getSelectedOptionIndex(props.options);
932933

933-
return index !== -1 ? (props.optionGroupLabel ? getOptionGroupChildren(props.options[index.group])[index.option] : props.options[index]) : null;
934+
const currentOption = index !== -1 ? (props.optionGroupLabel ? getOptionGroupChildren(props.options[index.group])[index.option] : props.options[index]) : null;
935+
936+
// selected value not shown if not present in options list #8510
937+
if (currentOption) {
938+
lastSelectedOption.current = currentOption;
939+
940+
return currentOption;
941+
}
942+
943+
if (props.value != null && lastSelectedOption.current != null) {
944+
const key = equalityKey();
945+
946+
if (ObjectUtils.equals(props.value, getOptionValue(lastSelectedOption.current), key)) {
947+
return lastSelectedOption.current;
948+
}
949+
}
950+
951+
return null;
934952
};
935953

936954
React.useImperativeHandle(ref, () => ({

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)