-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathSelect.js
More file actions
73 lines (64 loc) · 1.76 KB
/
Select.js
File metadata and controls
73 lines (64 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import FormElement from './FormElement';
import { uuid } from './util';
export default class Select extends Component {
constructor() {
super();
this.state = { id: `form-element-${uuid()}` };
}
onChange(e) {
const value = e.target.value;
if (this.props.onChange) {
this.props.onChange(e, value);
}
}
render() {
const id = this.props.id || this.state.id;
const { label, required, error, totalCols, cols, ...props } = this.props;
if (label || required || error || totalCols || cols) {
const formElemProps = { id, label, required, error, totalCols, cols };
return (
<FormElement { ...formElemProps }>
<Select { ...{ ...props, id } } />
</FormElement>
);
}
const { className, children, ...pprops } = props;
delete pprops.onChange;
const selectClassNames = classnames(className, 'slds-select');
return (
<select
id={ id }
className={ selectClassNames }
onChange={ this.onChange.bind(this) }
{ ...pprops }
>
{ children }
</select>
);
}
}
Select.propTypes = {
id: PropTypes.string,
className: PropTypes.string,
label: PropTypes.string,
required: PropTypes.bool,
totalCols: PropTypes.number,
cols: PropTypes.number,
error: FormElement.propTypes.error,
onChange: PropTypes.func,
};
Select.isFormElement = true;
export const Option = (props) => {
const { label, children, ...pprops } = props;
return (<option { ...pprops }>{ label || children }</option>);
};
Option.propTypes = {
children: PropTypes.node,
label: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]),
};