Skip to content

Commit

Permalink
Support emojis added after initial mount
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 4, 2023
1 parent 3d4799d commit 32392a9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "4.5.8",
"version": "4.5.9",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
5 changes: 4 additions & 1 deletion src/EmojiPickerReact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function EmojiPicker(props: PickerProps) {

// eslint-disable-next-line complexity
export default React.memo(EmojiPicker, (prev, next) => {
const prevCustomEmojis = prev.customEmojis ?? [];
const nextCustomEmojis = next.customEmojis ?? [];
return (
prev.emojiVersion === next.emojiVersion &&
prev.searchPlaceHolder === next.searchPlaceHolder &&
Expand All @@ -40,6 +42,7 @@ export default React.memo(EmojiPicker, (prev, next) => {
prev.height === next.height &&
prev.width === next.width &&
prev.searchDisabled === next.searchDisabled &&
prev.skinTonePickerLocation === next.skinTonePickerLocation
prev.skinTonePickerLocation === next.skinTonePickerLocation &&
prevCustomEmojis.length === nextCustomEmojis.length
);
});
19 changes: 18 additions & 1 deletion src/components/context/PickerConfigContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,31 @@ const ConfigContext = React.createContext<PickerConfigInternal>(
);

export function PickerConfigProvider({ children, ...config }: Props) {
const [mergedConfig] = React.useState(() => mergeConfig(config));
const mergedConfig = useSetConfig(config);

return (
<ConfigContext.Provider value={mergedConfig}>
{children}
</ConfigContext.Provider>
);
}

export function useSetConfig(config: PickerConfig) {
const [mergedConfig, setMergedConfig] = React.useState(() =>
mergeConfig(config)
);

React.useEffect(() => {
if (config.customEmojis?.length !== mergedConfig.customEmojis?.length) {
setMergedConfig(mergeConfig(config));
}
// not gonna...
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [config.customEmojis?.length]);

return mergedConfig;
}

export function usePickerConfig() {
return React.useContext(ConfigContext);
}
16 changes: 14 additions & 2 deletions stories/picker.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta } from '@storybook/react';
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';

import EmojiPicker, {
Emoji,
Expand Down Expand Up @@ -44,9 +44,21 @@ export const AutoTheme = (args: Props) => (
);

export const CustomEmojis = (args: Props) => (
<Template {...args} customEmojis={ customEmojis} />
<Template {...args} customEmojis={customEmojis} />
);

export const CustomEmojisDeffered = (args: Props) => {
const [custom, setCustomEmojis] = useState<any>(undefined);

useEffect(() => {
setTimeout(() => {
setCustomEmojis(customEmojis);
}, 2000);
}, []);

return <Template {...args} customEmojis={custom} />;
};

export const SearchDisabled = (args: Props) => (
<Template {...args} searchDisabled />
);
Expand Down

0 comments on commit 32392a9

Please sign in to comment.