Skip to content

Commit e18a531

Browse files
committed
Support paintext Array for Editable select
1 parent 15a909d commit e18a531

File tree

2 files changed

+63
-25
lines changed

2 files changed

+63
-25
lines changed

src/EditableSelect.js

+40-9
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,45 @@ export default class EditableSelect extends React.Component {
99
name: React.PropTypes.string.isRequired,
1010
onUpdate: React.PropTypes.func.isRequired,
1111
options: React.PropTypes.array.isRequired,
12+
defaultOptionText: React.PropTypes.string,
1213
}
1314
constructor(props) {
1415
super(props);
15-
const text = this.props.options.map((option) => {
16-
return option.value === this.props.value ? option.text : '';
16+
this.setState = this.setState.bind(this);
17+
18+
const options = this.convertOptions(this.props.options);
19+
if (this.props.defaultOptionText) {
20+
options.unshift({ text: this.props.defaultOptionText, value: null});
21+
}
22+
const selected = options.find((opt) => {
23+
if (opt.value === this.props.value) {
24+
return opt;
25+
}
1726
});
27+
28+
let text = 'Invalid value';
29+
30+
if (selected) {
31+
text = selected.text;
32+
} else if (this.props.defaultOptionText) {
33+
text = this.props.defaultOptionText;
34+
}
1835
this.state = {
1936
isEditing: false,
37+
options: options,
2038
text: text,
39+
value: this.props.value,
2140
};
22-
this.setState = this.setState.bind(this);
41+
2342
}
43+
2444
save = (event) => {
2545
event.preventDefault();
26-
const obj = this.refs.select;
27-
this.props.onUpdate(this.props.name, obj.value);
46+
this.props.onUpdate(this.props.name, this.refs.el.value);
2847
this.setState({
2948
isEditing: false,
30-
text: obj.options[obj.selectedIndex].text,
49+
text: this.refs.el.options[this.refs.el.selectedIndex].text,
50+
value: this.refs.el.value,
3151
});
3252
}
3353
cancel = () => {
@@ -36,14 +56,25 @@ export default class EditableSelect extends React.Component {
3656
handleLinkClick = () => {
3757
this.setState({isEditing: true});
3858
}
59+
60+
convertOptions= (options) => {
61+
return options.map((opt) => {
62+
if (typeof opt === 'string'
63+
|| typeof opt === 'number'
64+
|| typeof opt === 'boolean') {
65+
return { text: opt, value: opt};
66+
}
67+
return {text: opt.text, value: opt.value};
68+
});
69+
}
3970
render() {
4071
if (this.state.isEditing) {
41-
const options = this.props.options.map((option, index) => {
42-
return <option key={index} value={option.value}>{option.text}</option>;
72+
const options = this.state.options.map((opt, index) => {
73+
return <option key={index} value={opt.value}>{opt.text}</option>;
4374
});
4475
return (
4576
<XEditable isLoading={false} save={this.save} cancel={this.cancel}>
46-
<select ref='select' className='form-control input-sm' name={this.props.name} defaultValue={this.props.value} >
77+
<select ref='el' className='form-control input-sm' name={this.props.name} defaultValue={this.state.value} >
4778
{options}
4879
</select>
4980
</XEditable>

test/app.jsx

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
3-
import {EditableTextField, EditableSelect, EditableTextArea} from '../index';
3+
import {EditableTextField, EditableSelect, EditableTextArea} from '../src/index';
44

55
const XTextField = class XTextField extends React.Component {
66
constructor(props) {
@@ -30,22 +30,29 @@ const XSelect = class XSelect extends React.Component {
3030
this.setState({value: value});
3131
}
3232
render() {
33-
const options = [
34-
{
35-
text: 'China',
36-
value: 'CN'
37-
}, {
38-
text: 'India',
39-
value: 'IN'
40-
}, {
41-
text: 'United Kingdom (UK)',
42-
value: 'UK'
43-
}, {
44-
text: 'United States of America (USA)',
45-
value: 'USA'
46-
}
33+
// const options = [
34+
// {
35+
// text: 'China',
36+
// value: 'CN'
37+
// }, {
38+
// text: 'India',
39+
// value: 'IN'
40+
// }, {
41+
// text: 'United Kingdom (UK)',
42+
// value: 'UK'
43+
// }, {
44+
// text: 'United States of America (USA)',
45+
// value: 'USA'
46+
// }
47+
// ];
48+
const options2 = [
49+
'Hello',
50+
'World',
51+
'Sky',
52+
'Air',
53+
// 'UK',
4754
];
48-
return (<EditableSelect name='country' onUpdate={this.handleUpdate} value={this.state.value} options={options}/>);
55+
return (<EditableSelect name='country' onUpdate={this.handleUpdate} value={this.state.value} options={options2} defaultOptionText='Not select'/>);
4956
}
5057
};
5158

0 commit comments

Comments
 (0)