Skip to content

Commit 7f42c84

Browse files
fix(template-icon): close popup on selecting the icon (codesandbox#3759)
* fix(template-icon): close popup on selecting the icon * refactor(template-config): rearrange the props * fix lint Co-authored-by: Sara Vieira <[email protected]>
1 parent 889fe7b commit 7f42c84

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

packages/app/src/app/pages/Sandbox/Editor/Workspace/screens/ProjectInfo/TemplateConfig.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { FunctionComponent, useState } from 'react';
1+
import React, { FunctionComponent, useState, useCallback } from 'react';
22

33
import { useOvermind } from 'app/overmind';
44
import { ListAction, Text, Element } from '@codesandbox/components';
@@ -57,20 +57,26 @@ export const TemplateConfig: FunctionComponent = () => {
5757
},
5858
},
5959
} = useOvermind();
60+
const [popupVisible, setPopupVisible] = useState(false);
6061
const iconPopover = usePopoverState({
6162
placement: 'top',
63+
visible: popupVisible,
6264
});
6365
const [selectedIcon, setSelectedIcon] = useState(
6466
customTemplate.iconUrl || ''
6567
);
6668

6769
const DefaultIcon = getIcon(template);
6870

69-
const setIcon = (key: string) => {
70-
setSelectedIcon(key);
71-
iconPopover.hide();
72-
editTemplate({ ...customTemplate, iconUrl: key });
73-
};
71+
const setIcon = useCallback(
72+
(key: string) => {
73+
setSelectedIcon(key);
74+
setPopupVisible(false);
75+
editTemplate({ ...customTemplate, iconUrl: key });
76+
},
77+
[customTemplate, editTemplate, setSelectedIcon, setPopupVisible]
78+
);
79+
7480
const TemplateIcon = Icons[selectedIcon];
7581

7682
return (
@@ -95,9 +101,8 @@ export const TemplateConfig: FunctionComponent = () => {
95101
{Object.keys(Icons).map((i: string) => {
96102
const TemplateIconMap = Icons[i];
97103
return (
98-
// eslint-disable-next-line
99-
<li onClick={() => setIcon(i)} role="button" tabIndex={0}>
100-
<IconButton>
104+
<li key={i}>
105+
<IconButton onClick={() => setIcon(i)}>
101106
<TemplateIconMap width={24} />
102107
</IconButton>
103108
</li>

0 commit comments

Comments
 (0)