Skip to content

Commit e1c6412

Browse files
Use SimpleDropdown to reduce code duplication (#2670)
* Use SimpleDropdown in NavigationBarLangSelectButton * Remove useState and rename defaultValue to selectedValue * Add key prop to MenuItem by label value * Fix failing tests for NavigationBarLangSelectButton --------- Co-authored-by: sayomaki <[email protected]>
1 parent 02a4dd4 commit e1c6412

File tree

3 files changed

+20
-34
lines changed

3 files changed

+20
-34
lines changed

src/commons/SimpleDropdown.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
import { Button, Menu, MenuItem } from '@blueprintjs/core';
22
import { Popover2 } from '@blueprintjs/popover2';
3-
import { useState } from 'react';
43

54
type OptionType = { value: any; label: string };
65
type Props<T extends OptionType> = {
76
options: T[];
8-
defaultValue?: T['value'];
7+
selectedValue?: T['value'];
98
onClick?: (v: T['value']) => void;
10-
buttonProps?: Partial<React.ComponentProps<typeof Button>>;
9+
buttonProps?: Partial<React.ComponentProps<typeof Button> & { 'data-testid': string }>;
1110
popoverProps?: Partial<React.ComponentProps<typeof Popover2>>;
1211
};
1312

1413
function SimpleDropdown<T extends OptionType>(props: Props<T>) {
15-
const { options, defaultValue, onClick, buttonProps, popoverProps } = props;
16-
const [selectedOption, setSelectedOption] = useState(defaultValue);
14+
const { options, selectedValue, onClick, buttonProps, popoverProps } = props;
1715

1816
const handleClick = (value: T['value']) => {
19-
setSelectedOption(value);
2017
onClick?.(value);
2118
};
2219

2320
const buttonLabel = () => {
24-
const option = options.find(({ value }) => value === selectedOption);
21+
const option = options.find(({ value }) => value === selectedValue);
2522
return option?.label;
2623
};
2724

@@ -32,7 +29,7 @@ function SimpleDropdown<T extends OptionType>(props: Props<T>) {
3229
content={
3330
<Menu>
3431
{options.map(({ value, label }) => (
35-
<MenuItem text={label} onClick={() => handleClick(value)} />
32+
<MenuItem key={label} text={label} onClick={() => handleClick(value)} />
3633
))}
3734
</Menu>
3835
}

src/commons/navigationBar/subcomponents/NavigationBarLangSelectButton.tsx

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Button, Menu, MenuItem, Position } from '@blueprintjs/core';
2-
import { Popover2 } from '@blueprintjs/popover2';
1+
import { Position } from '@blueprintjs/core';
32
import { useState } from 'react';
43
import { useDispatch } from 'react-redux';
54
import {
@@ -11,6 +10,7 @@ import {
1110
SUPPORTED_LANGUAGES,
1211
SupportedLanguage
1312
} from 'src/commons/application/ApplicationTypes';
13+
import SimpleDropdown from 'src/commons/SimpleDropdown';
1414
import { useTypedSelector } from 'src/commons/utils/Hooks';
1515
import { chapterSelect } from 'src/commons/workspace/WorkspaceActions';
1616
import { playgroundConfigLanguage } from 'src/features/playground/PlaygroundActions';
@@ -36,28 +36,17 @@ const NavigationBarLangSelectButton = () => {
3636
};
3737

3838
return (
39-
<Popover2
40-
hasBackdrop
41-
interactionKind="click"
42-
position={Position.BOTTOM_RIGHT}
43-
isOpen={isOpen}
44-
content={
45-
<Menu>
46-
{SUPPORTED_LANGUAGES.map(language => (
47-
<MenuItem key={language} onClick={() => selectLang(language)} text={language} />
48-
))}
49-
</Menu>
50-
}
51-
onClose={() => setIsOpen(false)}
52-
>
53-
<Button
54-
rightIcon="caret-down"
55-
onClick={() => setIsOpen(true)}
56-
data-testid="NavigationBarLangSelectButton"
57-
>
58-
{lang}
59-
</Button>
60-
</Popover2>
39+
<SimpleDropdown
40+
options={SUPPORTED_LANGUAGES.map(lang => ({ value: lang, label: lang }))}
41+
onClick={selectLang}
42+
selectedValue={lang}
43+
popoverProps={{ position: Position.BOTTOM_RIGHT, onClose: () => setIsOpen(false), isOpen }}
44+
buttonProps={{
45+
rightIcon: 'caret-down',
46+
onClick: () => setIsOpen(true),
47+
'data-testid': 'NavigationBarLangSelectButton'
48+
}}
49+
/>
6150
);
6251
};
6352

src/pages/academy/grading/Grading.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ const Grading: React.FC = () => {
110110
<Text>Viewing</Text>
111111
<SimpleDropdown
112112
options={showOptions}
113-
defaultValue={showAllSubmissions}
113+
selectedValue={showAllSubmissions}
114114
onClick={setShowAllSubmissions}
115115
popoverProps={{ position: Position.BOTTOM }}
116116
buttonProps={{ minimal: true, rightIcon: 'caret-down' }}
117117
/>
118118
<Text>submissions from</Text>
119119
<SimpleDropdown
120120
options={groupOptions}
121-
defaultValue={showAllGroups}
121+
selectedValue={showAllGroups}
122122
onClick={setShowAllGroups}
123123
popoverProps={{ position: Position.BOTTOM }}
124124
buttonProps={{ minimal: true, rightIcon: 'caret-down' }}

0 commit comments

Comments
 (0)