From f729437f9cd1a43a107fd05883cf3e35ca9eef26 Mon Sep 17 00:00:00 2001 From: cheechoo28 <62035044+cheechoo28@users.noreply.github.com> Date: Sun, 4 Dec 2022 16:02:29 +0400 Subject: [PATCH] [@mantine/core] Select: Fix `limit` prop not working when current value matches one of the items from `data` (#3070) * [@mantine/core] Select: fixed the display of values in the dropdown if the prop limit is passed * [@mantine/core] Select: fixed the display of values in the dropdown if the prop limit is passed * [@mantine/core] Select: run prettier Co-authored-by: Denis.Yuhno --- .../src/Select/filter-data/filter-data.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/mantine-core/src/Select/filter-data/filter-data.ts b/src/mantine-core/src/Select/filter-data/filter-data.ts index 1f61b461ebc..b8fd0bbf4c0 100644 --- a/src/mantine-core/src/Select/filter-data/filter-data.ts +++ b/src/mantine-core/src/Select/filter-data/filter-data.ts @@ -26,6 +26,18 @@ export function filterData({ const selected = value != null ? data.find((item) => item.value === value) || null : null; if (selected && !filterDataOnExactSearchMatch && selected?.label === searchValue) { + if (limit) { + if (limit >= data.length) { + return data; + } + const firstIndex = data.indexOf(selected); + const lastIndex = firstIndex + limit; + const firstIndexOffset = lastIndex - data.length; + if (firstIndexOffset > 0) { + return data.slice(firstIndex - firstIndexOffset); + } + return data.slice(firstIndex, lastIndex); + } return data; }