all files / components/ Dropdown.react.js

89.39% Statements 59/66
88.37% Branches 38/43
91.67% Functions 11/12
64.71% Lines 11/17
10 statements, 1 function, 17 branches Ignored     
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95                                                                                                                                                                          
import R from 'ramda';
import React, {Component, PropTypes} from 'react';
import ReactDropdown from 'react-select';
 
const DELIMETER = ',';
 
/**
 * Dropdown is an interactive dropdown element for selecting one or more
 * items.
 * The values and labels of the dropdown items are specified in the `options`
 * property and the selected item(s) are specified with the `value` property.
 *
 * Use a dropdown when you have many options (more than 5) or when you are
 * constrained for space. Otherwise, you can use RadioItems or a Checklist,
 * which have the benefit of showing the users all of the items at once.
 */
export default class Dropdown extends Component {
    render() {
        const {id, fireEvent, multi, options, value, setProps} = this.props;
        let selectedValue;
        Iif (R.type(value) === 'array') {
            selectedValue = value.join(DELIMETER);
        } else {
            selectedValue = value;
        }
        return (
            <div id={id}>
                <ReactDropdown
                    options={options}
                    value={selectedValue}
                    onChange={selectedOption => {
                        if (multi) {
                            const values = R.pluck('value', selectedOption);
                            setProps({value: values});
                        } else {
                            setProps({value: selectedOption.value});
                        }
                        if (fireEvent) fireEvent('change');
                    }}
                    {...this.props}
                />
            </div>
        );
    }
}
 
Dropdown.propTypes = {
    id: PropTypes.string,
 
    className: PropTypes.string,
    /**
     * If true, the option is disabled
     */
    disabled: PropTypes.bool,
 
    /**
     * If true, the user can select multiple values
     */
    multi: PropTypes.bool,
 
    options: PropTypes.arrayOf(
        PropTypes.shape({
            disabled: PropTypes.bool,
            label: PropTypes.string,
            value: PropTypes.string
        })
    ),
 
    /**
     * The grey, default text shown when no option is selected
     */
    placeholder: PropTypes.string,
 
    /**
     * The value of the input. If `multi` is false (the default)
     * then value is just a string that corresponds to the values
     * provided in the `options` property. If `multi` is true, then
     * multiple values can be selected at once, and `value` is an
     * array of items with values corresponding to those in the
     * `options` prop.
     */
    value: PropTypes.oneOfType([
        PropTypes.string,
        PropTypes.arrayOf(PropTypes.string)
    ]),
 
    /**
     * Dash-assigned callback that gets fired when the input changes
     */
    setProps: PropTypes.func,
 
    dashEvents: PropTypes.oneOf(['change'])
};