Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 68ff19f

Browse files
authored
Fix regression in emoji picker order mangling after clearing filter (#10854)
* Add regression test for emoji picker order mangling after clearing filter * Fix regression in emoji picker order mangling after clearing filter * Iterate * Update src/components/views/emojipicker/EmojiPicker.tsx
1 parent d944422 commit 68ff19f

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

src/components/views/emojipicker/EmojiPicker.tsx

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -268,25 +268,30 @@ class EmojiPicker extends React.Component<IProps, IState> {
268268
} else {
269269
emojis = cat.id === "recent" ? this.recentlyUsed : DATA_BY_CATEGORY[cat.id];
270270
}
271-
emojis = emojis.filter((emoji) => this.emojiMatchesFilter(emoji, lcFilter));
272-
emojis = emojis.sort((a, b) => {
273-
const indexA = a.shortcodes[0].indexOf(lcFilter);
274-
const indexB = b.shortcodes[0].indexOf(lcFilter);
275-
276-
// Prioritize emojis containing the filter in its shortcode
277-
if (indexA == -1 || indexB == -1) {
278-
return indexB - indexA;
279-
}
280-
281-
// If both emojis start with the filter
282-
// put the shorter emoji first
283-
if (indexA == 0 && indexB == 0) {
284-
return a.shortcodes[0].length - b.shortcodes[0].length;
285-
}
286-
287-
// Prioritize emojis starting with the filter
288-
return indexA - indexB;
289-
});
271+
272+
if (lcFilter !== "") {
273+
emojis = emojis.filter((emoji) => this.emojiMatchesFilter(emoji, lcFilter));
274+
// Copy the array to not clobber the original unfiltered sorting
275+
emojis = [...emojis].sort((a, b) => {
276+
const indexA = a.shortcodes[0].indexOf(lcFilter);
277+
const indexB = b.shortcodes[0].indexOf(lcFilter);
278+
279+
// Prioritize emojis containing the filter in its shortcode
280+
if (indexA == -1 || indexB == -1) {
281+
return indexB - indexA;
282+
}
283+
284+
// If both emojis start with the filter
285+
// put the shorter emoji first
286+
if (indexA == 0 && indexB == 0) {
287+
return a.shortcodes[0].length - b.shortcodes[0].length;
288+
}
289+
290+
// Prioritize emojis starting with the filter
291+
return indexA - indexB;
292+
});
293+
}
294+
290295
this.memoizedDataByCategory[cat.id] = emojis;
291296
cat.enabled = emojis.length > 0;
292297
// The setState below doesn't re-render the header and we already have the refs for updateVisibility, so...

test/components/views/emojipicker/EmojiPicker-test.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import React from "react";
17+
import React, { createRef } from "react";
1818
import { render } from "@testing-library/react";
1919
import userEvent from "@testing-library/user-event";
2020

@@ -24,6 +24,26 @@ import { stubClient } from "../../../test-utils";
2424
describe("EmojiPicker", function () {
2525
stubClient();
2626

27+
it("should not mangle default order after filtering", () => {
28+
const ref = createRef<EmojiPicker>();
29+
const { container } = render(
30+
<EmojiPicker ref={ref} onChoose={(str: string) => false} onFinished={jest.fn()} />,
31+
);
32+
33+
// Record the HTML before filtering
34+
const beforeHtml = container.innerHTML;
35+
36+
// Apply a filter and assert that the HTML has changed
37+
//@ts-ignore private access
38+
ref.current!.onChangeFilter("test");
39+
expect(beforeHtml).not.toEqual(container.innerHTML);
40+
41+
// Clear the filter and assert that the HTML matches what it was before filtering
42+
//@ts-ignore private access
43+
ref.current!.onChangeFilter("");
44+
expect(beforeHtml).toEqual(container.innerHTML);
45+
});
46+
2747
it("sort emojis by shortcode and size", function () {
2848
const ep = new EmojiPicker({ onChoose: (str: string) => false, onFinished: jest.fn() });
2949

0 commit comments

Comments
 (0)