-
-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #346 from openboxes/select-refactor
Fix Select on Partial Receiving Page by refactoring SelectField
- Loading branch information
Showing
4 changed files
with
96 additions
and
94 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
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 |
---|---|---|
@@ -1,43 +1,121 @@ | ||
import _ from 'lodash'; | ||
import React, { Component } from 'react'; | ||
import ReactSelect from 'react-select'; | ||
import ReactSelect, { Async } from 'react-select-plus'; | ||
import { Overlay } from 'react-overlays'; | ||
import PropTypes from 'prop-types'; | ||
|
||
// eslint-disable-next-line react/prop-types | ||
const Dropdown = ({ children, style, width }) => ( | ||
<div style={{ ...style, position: 'absolute', width }}> | ||
{children} | ||
</div> | ||
); | ||
|
||
class Select extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
value: null, | ||
id: _.uniqueId('select-id_'), | ||
}; | ||
|
||
this.handleChange = this.handleChange.bind(this); | ||
} | ||
|
||
handleChange(value) { | ||
this.setState({ value }); | ||
|
||
if (this.props.onChange) { | ||
this.props.onChange(value); | ||
if (this.props.multi) { | ||
const values = _.map(value, val => | ||
(this.props.objectValue ? JSON.parse(val.value) : val.value)); | ||
this.setState({ value: values }); | ||
this.props.onChange(values); | ||
} else if (value !== null && value !== undefined) { | ||
const val = this.props.objectValue ? JSON.parse(value.value) : value.value; | ||
this.props.onChange(val); | ||
this.setState({ value: val }); | ||
} else { | ||
this.props.onChange(null); | ||
this.setState({ value: null }); | ||
} | ||
} | ||
|
||
render() { | ||
const { | ||
options: selectOptions, value: selectValue = this.state.value, | ||
objectValue = false, multi = false, delimiter = ';', async = false, ...attributes | ||
} = this.props; | ||
|
||
const options = _.map(selectOptions, (value) => { | ||
if (typeof value === 'string') { | ||
return { value, label: value }; | ||
} else if (objectValue) { | ||
return { ...value, value: JSON.stringify(value.value) }; | ||
} | ||
|
||
return value; | ||
}); | ||
|
||
let value = selectValue; | ||
|
||
if (objectValue) { | ||
value = multi ? _.map(selectValue, val => JSON.stringify(val)) | ||
: JSON.stringify(selectValue); | ||
} | ||
|
||
const SelectType = async ? Async : ReactSelect; | ||
|
||
// eslint-disable-next-line react/prop-types | ||
const dropdownComponent = ({ children }) => { | ||
const target = document.getElementById(`${this.state.id}-container`); | ||
return ( | ||
<Overlay | ||
show | ||
placement="bottom" | ||
target={target} | ||
container={document.getElementById('root')} | ||
> | ||
<Dropdown width={target.offsetWidth}> | ||
{children} | ||
</Dropdown> | ||
</Overlay> | ||
); | ||
}; | ||
|
||
return ( | ||
<ReactSelect | ||
value={this.state.value} | ||
{...this.props} | ||
onChange={this.handleChange} | ||
/> | ||
<div id={`${this.state.id}-container`}> | ||
<SelectType | ||
name={this.state.id} | ||
{...attributes} | ||
options={options} | ||
delimiter={delimiter} | ||
value={multi ? _.join(value, delimiter) : value} | ||
onChange={this.handleChange} | ||
dropdownComponent={dropdownComponent} | ||
/> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default Select; | ||
|
||
Select.propTypes = { | ||
options: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, | ||
PropTypes.shape({})])).isRequired, | ||
value: PropTypes.oneOfType([PropTypes.string, | ||
PropTypes.shape({}), PropTypes.any]), | ||
onChange: PropTypes.func, | ||
objectValue: PropTypes.bool, | ||
multi: PropTypes.bool, | ||
async: PropTypes.bool, | ||
delimiter: PropTypes.string, | ||
}; | ||
|
||
Select.defaultProps = { | ||
value: undefined, | ||
onChange: null, | ||
objectValue: false, | ||
multi: false, | ||
async: false, | ||
delimiter: ';', | ||
}; |