UNPKG

2.17 kBTypeScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3
4import * as React from 'react';
5import { caretDownEmptyIcon, LabIcon } from '../icon';
6import { classes } from '../utils';
7import { DEFAULT_STYLE_CLASS, IElementRefProps } from './interface';
8
9export const HTML_SELECT_CLASS = 'jp-HTMLSelect';
10
11export interface IOptionProps {
12 /**
13 * A space-delimited list of class names
14 */
15 className?: string;
16
17 /**
18 * Whether this option is non-interactive.
19 */
20 disabled?: boolean;
21
22 /**
23 * Label text for this option. If omitted, `value` is used as the label.
24 */
25 label?: string;
26
27 /**
28 * Value of this option.
29 */
30 value: string | number;
31}
32
33export 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
45export 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}