Skip to content

Commit fe44762

Browse files
committed
MySelect moved into common
1 parent f0fb692 commit fe44762

File tree

11 files changed

+83
-44
lines changed

11 files changed

+83
-44
lines changed

src/components/atoms/Icon.tsx

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React from 'react';
2+
import { render, screen, fireEvent } from '@testing-library/react';
3+
import { describe, it, expect, vi } from 'vitest';
4+
import MySelect from './MySelect';
5+
import '@testing-library/jest-dom';
6+
7+
describe('MySelect', () => {
8+
const options = [
9+
{ id: '1', name: 'Apple' },
10+
{ id: '2', name: 'Banana' },
11+
];
12+
13+
it('renders options and label', () => {
14+
render(
15+
<MySelect
16+
data={options}
17+
valProp="id"
18+
contentProp="name"
19+
label="Select Fruit"
20+
id="fruit-select"
21+
onChange={() => {}}
22+
/>
23+
);
24+
25+
// label is rendered
26+
expect(screen.getByLabelText('Select Fruit')).toBeInTheDocument();
27+
28+
// options are rendered
29+
expect(screen.getByText('Apple')).toBeInTheDocument();
30+
expect(screen.getByText('Banana')).toBeInTheDocument();
31+
});
32+
33+
it('calls onChange when selection changes', () => {
34+
const handleChange = vi.fn();
35+
36+
render(
37+
<MySelect
38+
data={options}
39+
valProp="id"
40+
contentProp="name"
41+
label="Fruit"
42+
onChange={handleChange}
43+
value="1"
44+
/>
45+
);
46+
47+
const select = screen.getByLabelText('Fruit');
48+
fireEvent.change(select, { target: { value: '2' } });
49+
50+
expect(handleChange).toHaveBeenCalled();
51+
});
52+
});

src/components/common/MySelect/MySelect.tsx

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from "react";
22
import { useTranslation } from "react-i18next";
3-
import "../../styles/my-select.css";
3+
import { Form } from "react-bootstrap";
4+
import "./my-select.css";
45

56
interface Props<T> {
67
data: T[];
@@ -26,31 +27,31 @@ const MySelect = <T,>({
2627
cssClass = "",
2728
}: Props<T>) => {
2829
const { t } = useTranslation();
29-
const cssname = "form-select form-select-sm " + cssClass;
30-
const labelText = label && t(label);
31-
const labelId = id || (label ? `${label}-id` : undefined);
32-
33-
const mappedData = data.map((item) => {
34-
const val = valProp ? String(item[valProp]) : String(item);
35-
const content = contentProp ? String(item[contentProp]) : String(item);
36-
return (
37-
<option key={val} value={val}>
38-
{t(content)}
39-
</option>
40-
);
41-
});
30+
const controlId = id || (label ? `${label}-id` : undefined);
4231

4332
return (
44-
<div className="select-wrapper" style={style}>
33+
<Form.Group controlId={controlId} style={style} className={`mb-2 ${cssClass}`}>
4534
{label && (
46-
<label htmlFor={labelId} style={{ whiteSpace: "nowrap" }}>
47-
{labelText}
48-
</label>
35+
<Form.Label className="mb-1" style={{ whiteSpace: "nowrap" }}>
36+
{t(label)}
37+
</Form.Label>
4938
)}
50-
<select id={labelId} className={cssname} onChange={onChange} value={value}>
51-
{mappedData}
52-
</select>
53-
</div>
39+
<Form.Select
40+
size="sm"
41+
value={value}
42+
onChange={onChange}
43+
>
44+
{data.map((item) => {
45+
const val = valProp ? String(item[valProp]) : String(item);
46+
const content = contentProp ? String(item[contentProp]) : String(item);
47+
return (
48+
<option key={val} value={val}>
49+
{t(content)}
50+
</option>
51+
);
52+
})}
53+
</Form.Select>
54+
</Form.Group>
5455
);
5556
};
5657

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default as MySelect} from './MySelect';

src/components/common/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export {ButtonToggle} from './ButtonToggle';
33
export {Checkbox} from './CheckBox';
44
export {ErrorBoundary} from './ErrorBoundary';
55
export {Loader} from './Loader';
6+
export {MySelect} from './MySelect';
67
export {Select} from './Select';
78
export {SmallCard} from './SmallCard';

src/components/filter/CardFilterWhen.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { observer } from 'mobx-react';
44
import { Accordion, Card } from 'react-bootstrap';
55
import GroupCheckbox from './GroupCheckBox';
66
import { useStore } from '../../stores/storeConfig';
7-
import MySelect from '../atoms/MySelect';
7+
import { MySelect } from '../common';
88
import CustomToggle from './CustomToggle';
99
import '../../styles/accordion.css'
1010

src/components/filter/CardFilterWhere.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { observer } from 'mobx-react';
44
import { Accordion, Card } from 'react-bootstrap';
55
import GroupCheckbox from './GroupCheckBox';
66
import { useStore } from '../../stores/storeConfig';
7-
import MySelect from '../atoms/MySelect';
7+
import { MySelect } from '../common';
88
import CustomToggle from './CustomToggle';
99
import CitySelector from './CitySelector';
1010
import StreetSelector from './StreetSelector';

src/components/groupby/SelectGroupBy.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { observer } from 'mobx-react';
33
import { toJS, reaction } from 'mobx';
44
import { useStore } from '../../stores/storeConfig';
55
import GroupBy from '../../stores/filter/GroupBy';
6-
import MySelect from '../atoms/MySelect';
6+
import { MySelect } from '../common';
77

88
interface IProps {
99
id: string;

src/components/groupby/SelectGroupBy2.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { observer } from 'mobx-react';
44

55
import { useStore } from '../../stores/storeConfig';
66
import GroupBy2 from '../../stores/filter/GroupBy2';
7-
import MySelect from '../atoms/MySelect';
7+
import { MySelect } from '../common';
88

99
interface IProps { id: string }
1010

src/components/groupby/SelectSortBy.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { ChangeEvent, useCallback } from 'react';
22
import { observer } from 'mobx-react-lite';
33
import { useStore } from '../../stores/storeConfig';
4-
import MySelect from '../atoms/MySelect';
4+
import { MySelect } from '../common';
55
import GroupBy from '../../stores/filter/GroupBy';
66

77
interface IProps {

0 commit comments

Comments
 (0)