Merge pull request #10586 from tchak/combo-distinct-values
fix(combobox): allow non unique lists of options
This commit is contained in:
commit
09db22f81c
1 changed files with 13 additions and 2 deletions
|
@ -56,7 +56,10 @@ export function useSingleList({
|
||||||
onChange?: (item: Item | null) => void;
|
onChange?: (item: Item | null) => void;
|
||||||
}) {
|
}) {
|
||||||
const [selectedKey, setSelectedKey] = useState(defaultSelectedKey);
|
const [selectedKey, setSelectedKey] = useState(defaultSelectedKey);
|
||||||
const items = useMemo(() => defaultItems || [], [defaultItems]);
|
const items = useMemo(
|
||||||
|
() => (defaultItems ? distinctBy(defaultItems, 'value') : []),
|
||||||
|
[defaultItems]
|
||||||
|
);
|
||||||
const selectedItem = useMemo(
|
const selectedItem = useMemo(
|
||||||
() => items.find((item) => item.value == selectedKey) ?? null,
|
() => items.find((item) => item.value == selectedKey) ?? null,
|
||||||
[items, selectedKey]
|
[items, selectedKey]
|
||||||
|
@ -157,7 +160,10 @@ export function useMultiList({
|
||||||
() => new Set(defaultSelectedKeys ?? [])
|
() => new Set(defaultSelectedKeys ?? [])
|
||||||
);
|
);
|
||||||
const [inputValue, setInputValue] = useState('');
|
const [inputValue, setInputValue] = useState('');
|
||||||
const items = useMemo(() => defaultItems || [], [defaultItems]);
|
const items = useMemo(
|
||||||
|
() => (defaultItems ? distinctBy(defaultItems, 'value') : []),
|
||||||
|
[defaultItems]
|
||||||
|
);
|
||||||
const itemsIndex = useMemo(() => {
|
const itemsIndex = useMemo(() => {
|
||||||
const index = new Map<string, Item>();
|
const index = new Map<string, Item>();
|
||||||
for (const item of items) {
|
for (const item of items) {
|
||||||
|
@ -473,3 +479,8 @@ export function useOnFormReset(onReset?: () => void) {
|
||||||
|
|
||||||
return ref;
|
return ref;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function distinctBy<T>(array: T[], key: keyof T): T[] {
|
||||||
|
const keys = array.map((item) => item[key]);
|
||||||
|
return array.filter((item, index) => keys.indexOf(item[key]) == index);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue