Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
feat: Add Dropdown component. (#47)
Browse files Browse the repository at this point in the history
* feat: Add Dropdown component.

* Add doc.

* Add author.

* Update dropdown.

* Update author.

* Dropdown children not need required.
  • Loading branch information
lijianxin1202 authored and wangkailang committed Aug 14, 2019
1 parent 2e76a72 commit 2a4ae83
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/content/components/dropdown.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Dropdown 下拉框
date: 2019-08-13
author: lijianxin1202
---
在用户点击标题后展示详细内容供用户操作

## 使用方式
点击标题后弹出详细内容,再次点击隐藏详细内容

## 基本用法
点击标题后弹出内容:
```tsx
<Dropdown title="标题" children={<div style={{ padding: '5px 10px' }}>内容</div>} id="dropdown" />
```

## API
```jsx previewOnly
<PropTable of="dropdown" />
```
13 changes: 13 additions & 0 deletions src/components/Dropdown/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import Dropdown from './index';
import { mount } from 'enzyme';

describe('Dropdown', () => {
it('render simple dropdown', () => {
const dropdown = mount(<Dropdown title="title" children="text" />);
const title = dropdown.find('button');
expect(dropdown.find('div.dropdown').length).toBe(1);
title.simulate('click');
expect(dropdown.find('div.dropdown.open').length).toBe(1);
});
});
41 changes: 41 additions & 0 deletions src/components/Dropdown/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Dropdown as BootstrapDropdown } from 'react-bootstrap';
import { DropdownProps, DropdownDefaultProps } from '../../interface';

const defaultProps: DropdownDefaultProps = {
id: 'CustomDropdown'
}

const Dropdown = (props: DropdownProps) => {
const { className, id, title, children } = props;
return (
<BootstrapDropdown id={id} className={className}>
<BootstrapDropdown.Toggle>{title}</BootstrapDropdown.Toggle>
<BootstrapDropdown.Menu>{children}</BootstrapDropdown.Menu>
</BootstrapDropdown>
);
}

Dropdown.propTypes = {
/**
* 元素 class 名称 string
**/
className: PropTypes.string,
/**
* 下拉框收起时展示内容 string
**/
title: PropTypes.string,
/**
* 元素 id,默认为 CustomDropdown string
**/
id: PropTypes.string,
/**
* 下拉框展开时详细内容 React.ReactNode
**/
children: PropTypes.node,
};

Dropdown.defaultProps = defaultProps;

export default Dropdown;
2 changes: 2 additions & 0 deletions src/components/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Badge from './Badge';
import Dropdown from './Dropdown';
import Icon from './Icon';
import Tooltip from './Tooltip';
import UsageBar from './UsageBar';
Expand All @@ -17,6 +18,7 @@ import { Well } from 'react-bootstrap';

export {
Badge,
Dropdown,
Icon,
Tooltip,
UsageBar,
Expand Down
10 changes: 10 additions & 0 deletions src/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,13 @@ export interface RangePickerState {
value: Moment[] | string[];
open: boolean;
}

export interface DropdownDefaultProps {
id: string;
}

export interface DropdownProps extends DropdownDefaultProps {
className?: string;
title?: string;
children?: React.ReactNode;
}
8 changes: 8 additions & 0 deletions stories/Dropdown.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react';
import { storiesOf } from '@storybook/react';
import { Dropdown } from '../src';

storiesOf('Dropdown', module)
.add('default', () => (
<Dropdown title="标题" children={<div style={{ padding: '5px 10px' }}>内容</div>} id="dropdown" />
))

0 comments on commit 2a4ae83

Please sign in to comment.