Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(select): select style #583

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions packages/design/components/Select/Select.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { ComponentMeta, ComponentStory } from '@storybook/react';
import type { Meta, StoryObj } from '@storybook/react';
import React from 'react';

import Select from '.';

const componentMeta: ComponentMeta<typeof Select> = {
const componentMeta: Meta<typeof Select> = {
title: 'Modern/Select',
component: Select,
};

export default componentMeta;

const Template: ComponentStory<typeof Select> = () => {
return (
export const Template: StoryObj<typeof Select> = {
render: () => (
<Select
style={{
width: 400,
Expand All @@ -23,7 +23,5 @@ const Template: ComponentStory<typeof Select> = () => {
]}
defaultValue='age'
/>
);
),
};

export const Default = Template.bind({});
20 changes: 19 additions & 1 deletion packages/design/components/Select/__test__/Select.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from '@testing-library/react';
import { fireEvent, render } from '@testing-library/react';
import React from 'react';

import Select from '..';
Expand All @@ -16,4 +16,22 @@ describe('Select Component', () => {
);
expect(container).toMatchSnapshot();
});

it('Open dropdown & Select option', () => {
const onChange = vi.fn();
const element = render(
<Select
defaultValue='name'
options={[
{ label: 'name', value: 'name' },
{ label: 'age', value: 'age' },
]}
onChange={onChange}
/>,
);

fireEvent.click(element.getByText('name'));
fireEvent.click(element.getByText('age'));
expect(onChange).toBeCalledTimes(1);
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 18 additions & 12 deletions packages/design/components/Select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,24 @@ const Select = ({ options, style, defaultValue, className, onChange }: SelectPro
<span>{optionsMap[active]?.label}</span>
<ArrowDown className='bgm-select-arrow' />
</div>
<select
defaultValue={defaultValue}
onChange={(e) => {
handleOptionClick(e.target.value);
}}
>
{options.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
{open ? (
<ul className={cn('bgm-select__dropdown')} defaultValue={defaultValue}>
{options.map((option) => (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥要替换成 ul,感觉语义不太对?

<li
className={cn('bgm-select__dropdown-item')}
key={option.value}
value={option.value}
onClick={() => {
handleOptionClick(option.value);
}}
>
{option.label}
</li>
))}
</ul>
) : (
<></>
)}
</div>
);
};
Expand Down
12 changes: 11 additions & 1 deletion packages/design/components/Select/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
top: 100%;
border: 2px solid @gray-10;
box-sizing: border-box;
padding: 8px 12px;
padding: 10px 0;
width: 100%;
left: 0;
border-radius: 19px;
Expand All @@ -27,6 +27,16 @@
> div {
padding: 9px 0;
}

&-item {
height: 1em;
padding: 10px;
// padding: 0.5em 0;

&:hover {
background-color: @gray-10;
}
}
}

select {
Expand Down