This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add InputDropdown. * Update case. * Update. * Add test case. * Use higher precision in sass loader. * Config docs sass precision. * Update.
- Loading branch information
1 parent
60e361d
commit a1dcf2a
Showing
9 changed files
with
242 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
title: InputDropdown Input 下拉框 | ||
date: 2019-08-23 | ||
author: lijianxin1202 | ||
--- | ||
作为 Input 一部分的下拉框 | ||
|
||
## 使用方式 | ||
通常和 Input 一起使用,在 Input 的前面或者后部,用来增强 Input 功能 | ||
|
||
## 基本用法 | ||
|
||
```jsx | ||
() => { | ||
const [value, updateValue] = React.useState('MB'); | ||
return (<div style={{ width: '300px' }}> | ||
<InputGroup> | ||
<FormControl /> | ||
<InputDropdown | ||
pullRight | ||
defaultValue="GB" | ||
onChange={updateValue} | ||
value={value} | ||
options={[ | ||
{ title: 'MB', value: 'MB' }, | ||
{ title: 'GB', value: 'GB' }, | ||
{ title: 'TB', value: 'TB' }, | ||
]} /> | ||
</InputGroup> | ||
</div>); | ||
} | ||
``` | ||
|
||
## API | ||
```jsx previewOnly | ||
<PropTable of="inputDropdown" /> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,9 @@ module.exports = { | |
cssLoaderOptions: { | ||
camelCase: false, | ||
}, | ||
options: { | ||
precision: 8, | ||
}, | ||
}, | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React from 'react'; | ||
import InputDropdown from './index'; | ||
import { mount } from 'enzyme'; | ||
import { InputGroup, FormControl } from 'react-bootstrap'; | ||
|
||
describe('InputDropdown', () => { | ||
it('should render corrent dropdown', () => { | ||
const Demo = () => { | ||
const [value, updateValue] = React.useState('MB'); | ||
return (<div style={{ width: '300px' }}> | ||
<InputGroup> | ||
<FormControl /> | ||
<InputDropdown | ||
pullRight | ||
defaultValue="GB" | ||
onChange={(key: any) => updateValue(key)} | ||
value={value} | ||
options={[ | ||
{ title: 'MB', value: 'MB' }, | ||
{ title: 'GB', value: 'GB' }, | ||
{ title: 'TB', value: 'TB' }, | ||
]} /> | ||
</InputGroup> | ||
</div>); | ||
} | ||
const dropdown = mount(<Demo />); | ||
const button = dropdown.find('button'); | ||
expect(dropdown.find('.dropdown.input-group-btn').length).toBe(1); | ||
button.simulate('click'); | ||
const tb = dropdown.find('a').at(2); | ||
tb.simulate('click'); | ||
expect(dropdown.find('button[value="TB"]').length).toBe(1); | ||
}); | ||
|
||
it('should render corrent with input', () => { | ||
const Demo = () => { | ||
const [value, updateValue] = React.useState('MB'); | ||
return (<div style={{ width: '300px' }}> | ||
<InputGroup> | ||
<FormControl /> | ||
<InputDropdown | ||
pullRight | ||
input={{ value, onChange: updateValue }} | ||
options={[ | ||
{ title: 'MB', value: 'MB' }, | ||
{ title: 'GB', value: 'GB' }, | ||
{ title: 'TB', value: 'TB' }, | ||
]} /> | ||
</InputGroup> | ||
</div>); | ||
} | ||
const dropdown = mount(<Demo />); | ||
const button = dropdown.find('button'); | ||
button.simulate('click'); | ||
const tb = dropdown.find('a').at(2); | ||
tb.simulate('click'); | ||
// @ts-ignore | ||
expect(dropdown.find('button').instance().textContent).toBe('TB '); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { find, get } from 'lodash'; | ||
import { DropdownButton, MenuItem, InputGroup } from 'react-bootstrap'; | ||
import { uuid } from '../../utils'; | ||
import { InputDropdownProps } from '../../interface'; | ||
|
||
const InputDropdown = (props: InputDropdownProps) => { | ||
function getValue() { | ||
const { input, value, defaultValue } = props; | ||
if (input && input.value !== undefined) { | ||
return input.value; | ||
} | ||
return value !== undefined ? value : defaultValue; | ||
} | ||
|
||
function getTitle() { | ||
const { options } = props; | ||
const option = find(options, { value: getValue() }); | ||
return get(option, 'title'); | ||
} | ||
|
||
function handleSelect(eventKey: any) { | ||
const { input, onChange } = props; | ||
if (onChange) { | ||
onChange(eventKey); | ||
} else if (input) { | ||
input.onChange(eventKey); | ||
} | ||
} | ||
const { input, meta, options, ...rest } = props; | ||
return ( | ||
<DropdownButton | ||
id={uuid()} | ||
{...rest} | ||
componentClass={InputGroup.Button} | ||
onSelect={handleSelect} | ||
title={getTitle()} | ||
> | ||
{options && | ||
options.map(option => ( | ||
<MenuItem key={option.value} eventKey={option.value}> | ||
{option.title} | ||
</MenuItem> | ||
))} | ||
</DropdownButton> | ||
); | ||
} | ||
|
||
InputDropdown.propTypes = { | ||
/** | ||
* 下拉选项 | ||
**/ | ||
options: PropTypes.array, | ||
/** | ||
* 下拉框是否右对齐,默认为 true | ||
**/ | ||
pullRight: PropTypes.bool, | ||
/** | ||
* 默认值 | ||
**/ | ||
defaultValue: PropTypes.string, | ||
/** | ||
* value 变化后回调 | ||
**/ | ||
onChange: PropTypes.func, | ||
/** | ||
* value,传入 value 时变为受控组件 | ||
**/ | ||
value: PropTypes.string, | ||
}; | ||
|
||
InputDropdown.defaultProps = { | ||
pullRight: true, | ||
}; | ||
|
||
export default InputDropdown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
import { InputDropdown } from '../src'; | ||
import { FormControl, InputGroup } from 'react-bootstrap'; | ||
|
||
storiesOf('InputDropdown', module) | ||
.add('default', () => { | ||
const Demo = () => { | ||
const [value, updateValue] = React.useState('MB'); | ||
return (<div style={{ width: '300px' }}> | ||
<InputGroup> | ||
<FormControl /> | ||
<InputDropdown | ||
pullRight | ||
defaultValue="GB" | ||
onChange={(key: any) => updateValue(key)} | ||
value={value} | ||
options={[ | ||
{ title: 'MB', value: 'MB' }, | ||
{ title: 'GB', value: 'GB' }, | ||
{ title: 'TB', value: 'TB' }, | ||
]} /> | ||
</InputGroup> | ||
</div>); | ||
} | ||
return <Demo />; | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters