Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
Changes to allow <select> to have a value/label hash
Browse files Browse the repository at this point in the history
  • Loading branch information
jamgregory committed Jun 17, 2020
1 parent cc977b6 commit acab03c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
14 changes: 10 additions & 4 deletions dist/editable.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,13 @@ var Select = /*#__PURE__*/function (_TextField) {

_this = _super.call(this, props);

var options = _this.props.options.map(function (value, index) {
var options = _this.props.options.map(function (option, index) {
var value = option.value;
var label = option.label;
return /*#__PURE__*/React.createElement("option", {
key: index + value
}, value);
key: index + value,
value: value
}, label);
});

_this.state = {
Expand Down Expand Up @@ -558,7 +561,10 @@ var Editable = /*#__PURE__*/function (_React$Component) {
if (this.props.disabled) {
p = value;
} else {
a = value;
var selectedOption = this.props.options.filter(function (option) {
return option.value === value;
});
a = this.props.type === "select" ? selectedOption[0].label : value;
}
} else {
p = value;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"edit",
"ajax"
],
"version": "0.6.5-1",
"version": "0.6.5-2",
"main": "dist/editable.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
Expand Down
3 changes: 2 additions & 1 deletion src/Editable.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ export default class Editable extends React.Component{
if(this.props.disabled){
p = value
}else{
a = value
const selectedOption = this.props.options.filter((option) => { return option.value === value });
a = (this.props.type === "select" ? selectedOption[0].label : value)
}
}else{
p = value
Expand Down
8 changes: 5 additions & 3 deletions src/components/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import TextField from './TextField';
export default class Select extends TextField {
constructor(props){
super(props)
const options = this.props.options.map((value, index) => {
return <option key={index + value}>{value}</option>
})
const options = this.props.options.map((option, index) => {
const value = option.value;
const label = option.label;
return <option key={index + value} value={value}>{label}</option>
});
this.state = {
componentClass: 'select',
type: null,
Expand Down
13 changes: 8 additions & 5 deletions src/components/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ export default class TextField extends React.Component {
return (
<React.Fragment>
<FormGroup validationState={this.props.validationText ? 'error' : null} >
<FormControl autoFocus
<FormControl
autoFocus
value={this.props.value ? this.props.value : ""}
onChange={e => this.props.setNewValue(e.target.value)}
componentClass={this.state.componentClass} type={this.state.type}
bsSize="sm" className="mr-1">
{this.state.options}
</FormControl>
componentClass={this.state.componentClass}
type={this.state.type}
bsSize="sm" className="mr-1"
>
{this.state.options}
</FormControl>
<FormControl.Feedback />
<HelpBlock>{this.props.validationText}</HelpBlock>
</FormGroup>
Expand Down
7 changes: 4 additions & 3 deletions stories/0-Welcome.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ export const TextArea = () => <Editable
export const Select = () => <Editable
{...sharedProps}
type="select"
initialValue="hello"
options={[
'Hello',
'Mellow',
'Yellow'
{ label: 'Hello', value: 'hello' },
{ label: 'Mellow', value: 'mellow' },
{ label: 'Yellow', value: 'yell' }
]}
/>;

Expand Down

0 comments on commit acab03c

Please sign in to comment.