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
feat: Add InputDropdown. #64
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0534009
feat: Add InputDropdown.
448778c
Update case.
483f3a7
Update.
1eb6358
Add test case.
edecf07
Use higher precision in sass loader.
2354d8f
Config docs sass precision.
1fe0c1f
Merge branch 'master' into feat/inputdropdown
6f9ce30
Update.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这么写和原逻辑就不一致了,value 为 0,空字符串都会返回 defaultValue 了。