1 |
|
2 |
|
3 |
|
4 | import * as React from 'react';
|
5 | import { caretDownEmptyIcon, LabIcon } from '../icon';
|
6 | import { classes } from '../utils';
|
7 | import { DEFAULT_STYLE_CLASS, IElementRefProps } from './interface';
|
8 |
|
9 | export const HTML_SELECT_CLASS = 'jp-HTMLSelect';
|
10 |
|
11 | export interface IOptionProps {
|
12 | |
13 |
|
14 |
|
15 | className?: string;
|
16 |
|
17 | |
18 |
|
19 |
|
20 | disabled?: boolean;
|
21 |
|
22 | |
23 |
|
24 |
|
25 | label?: string;
|
26 |
|
27 | |
28 |
|
29 |
|
30 | value: string | number;
|
31 | }
|
32 |
|
33 | export interface IHTMLSelectProps
|
34 | extends IElementRefProps<HTMLSelectElement>,
|
35 | React.SelectHTMLAttributes<HTMLSelectElement> {
|
36 | defaultStyle?: boolean;
|
37 |
|
38 | iconProps?: LabIcon.IProps;
|
39 |
|
40 | icon?: LabIcon;
|
41 |
|
42 | options?: Array<string | number | IOptionProps>;
|
43 | }
|
44 |
|
45 | export class HTMLSelect extends React.Component<IHTMLSelectProps> {
|
46 | public render(): JSX.Element {
|
47 | const {
|
48 | className,
|
49 | defaultStyle = true,
|
50 | disabled,
|
51 | elementRef,
|
52 | iconProps,
|
53 | icon = caretDownEmptyIcon,
|
54 | options = [],
|
55 | ...htmlProps
|
56 | } = this.props;
|
57 |
|
58 | const cls = classes(
|
59 | HTML_SELECT_CLASS,
|
60 | {
|
61 | [DEFAULT_STYLE_CLASS]: defaultStyle
|
62 | },
|
63 | className
|
64 | );
|
65 |
|
66 | const optionChildren = options.map(option => {
|
67 | const props: IOptionProps =
|
68 | typeof option === 'object' ? option : { value: option };
|
69 | return (
|
70 | <option {...props} key={props.value}>
|
71 | {props.label || props.value}
|
72 | </option>
|
73 | );
|
74 | });
|
75 |
|
76 | return (
|
77 | <div className={cls}>
|
78 | <select
|
79 | disabled={disabled}
|
80 | ref={elementRef}
|
81 | {...htmlProps}
|
82 | multiple={false}
|
83 | >
|
84 | {optionChildren}
|
85 | {htmlProps.children}
|
86 | </select>
|
87 | <icon.react
|
88 | {...{
|
89 | tag: 'span',
|
90 | stylesheet: 'select',
|
91 | right: '7px',
|
92 | top: '5px',
|
93 | ...iconProps
|
94 | }}
|
95 | />
|
96 | </div>
|
97 | );
|
98 | }
|
99 | }
|