Removed GopherJS, basic frontend completed, need backend changes for

torrent storage
This commit is contained in:
2017-11-30 18:12:11 -05:00
parent 67fdef16b1
commit e98ad2cc88
69321 changed files with 5498914 additions and 337 deletions

View File

@@ -0,0 +1,30 @@
import * as React from 'react';
import { StandardProps } from '..';
import { InputProps } from '../Input';
import { InputClassKey } from '../Input/Input';
export interface SelectProps extends StandardProps<
InputProps,
SelectClassKey,
'value'
> {
autoWidth?: boolean;
displayEmpty?: boolean;
input?: React.ReactNode;
native?: boolean;
multiple?: boolean;
MenuProps?: Object;
renderValue?: Function;
value?: Array<string | number> | string | number;
}
type SelectClassKey =
| InputClassKey
| 'select'
| 'selectMenu'
| 'icon'
;
declare const Select: React.ComponentType<SelectProps>;
export default Select;

View File

@@ -0,0 +1,207 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.styles = undefined;
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _warning = require('warning');
var _warning2 = _interopRequireDefault(_warning);
var _SelectInput = require('./SelectInput');
var _SelectInput2 = _interopRequireDefault(_SelectInput);
var _withStyles = require('../styles/withStyles');
var _withStyles2 = _interopRequireDefault(_withStyles);
var _Input = require('../Input');
var _Input2 = _interopRequireDefault(_Input);
var _reactHelpers = require('../utils/reactHelpers');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var babelPluginFlowReactPropTypes_proptype_Element = require('react').babelPluginFlowReactPropTypes_proptype_Element || require('prop-types').any;
// @inheritedComponent Input
var babelPluginFlowReactPropTypes_proptype_ChildrenArray = require('react').babelPluginFlowReactPropTypes_proptype_ChildrenArray || require('prop-types').any; // Import to enforce the CSS injection order
var styles = exports.styles = function styles(theme) {
return {
root: {
position: 'relative',
width: '100%'
},
select: {
'-moz-appearance': 'none', // Remove Firefox custom style
'-webkit-appearance': 'none', // Fix SSR issue
appearance: 'none', // Reset
// When interacting quickly, the text can end up selected.
// Native select can't be selected either.
userSelect: 'none',
padding: '0 ' + theme.spacing.unit * 4 + 'px 2px 0',
width: 'auto',
minWidth: theme.spacing.unit * 2, // So it doesn't collapse.
height: 'calc(1em + ' + (theme.spacing.unit * 2 - 2) + 'px)',
cursor: 'pointer',
'&:focus': {
// Show that it's not an text input
background: theme.palette.type === 'light' ? 'rgba(0, 0, 0, 0.05)' : 'rgba(255, 255, 255, 0.05)',
borderRadius: 0 // Reset Chrome style
},
// Remove Firefox focus border
'&:-moz-focusring': {
color: 'transparent',
textShadow: '0 0 0 #000'
},
// Remove IE11 arrow
'&::-ms-expand': {
display: 'none'
}
},
selectMenu: {
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
overflow: 'hidden',
lineHeight: 'calc(1em + ' + (theme.spacing.unit * 2 - 2) + 'px)'
},
disabled: {
cursor: 'default'
},
icon: {
position: 'absolute',
right: 0,
top: 4,
color: theme.palette.text.secondary,
'pointer-events': 'none' // Don't block pinter events on the select under the icon.
}
};
};
var babelPluginFlowReactPropTypes_proptype_Props = {
/**
* If true, the width of the popover will automatically be set according to the items inside the
* menu, otherwise it will be at least the width of the select input.
*/
autoWidth: require('prop-types').bool,
/**
* The option elements to populate the select with.
* Can be some `MenuItem` when `native` is false and `option` when `native` is true.
*/
children: typeof $ReadOnlyArray === 'function' ? require('prop-types').instanceOf($ReadOnlyArray).isRequired : require('prop-types').any.isRequired,
/**
* Useful to extend the style applied to components.
*/
classes: require('prop-types').object,
/**
* If `true`, the selected item is displayed even if its value is empty.
* You can only use it when the `native` property is `false` (default).
*/
displayEmpty: require('prop-types').bool,
/**
* An `Input` element; does not have to be a material-ui specific `Input`.
*/
input: typeof babelPluginFlowReactPropTypes_proptype_Element === 'function' ? babelPluginFlowReactPropTypes_proptype_Element : require('prop-types').shape(babelPluginFlowReactPropTypes_proptype_Element),
/**
* `classes` property applied to the `Input` element.
*/
InputClasses: require('prop-types').object,
/**
* If `true`, the component will be using a native `select` element.
*/
native: require('prop-types').bool,
/**
* If true, `value` must be an array and the menu will support multiple selections.
* You can only use it when the `native` property is `false` (default).
*/
multiple: require('prop-types').bool,
/**
* Properties applied to the `Menu` element.
*/
MenuProps: require('prop-types').object,
/**
* Render the selected value.
* You can only use it when the `native` property is `false` (default).
*/
renderValue: require('prop-types').func,
/**
* The input value, required for a controlled component.
*/
value: require('prop-types').oneOfType([typeof $ReadOnlyArray === 'function' ? require('prop-types').instanceOf($ReadOnlyArray) : require('prop-types').any, require('prop-types').string, require('prop-types').number])
};
function Select(props) {
var autoWidth = props.autoWidth,
children = props.children,
classes = props.classes,
displayEmpty = props.displayEmpty,
input = props.input,
InputClasses = props.InputClasses,
native = props.native,
multiple = props.multiple,
MenuProps = props.MenuProps,
renderValue = props.renderValue,
other = (0, _objectWithoutProperties3.default)(props, ['autoWidth', 'children', 'classes', 'displayEmpty', 'input', 'InputClasses', 'native', 'multiple', 'MenuProps', 'renderValue']);
// Instead of `Element<typeof Input>` to have more flexibility.
process.env.NODE_ENV !== "production" ? (0, _warning2.default)((0, _reactHelpers.isMuiElement)(input, ['Input']), ['Material-UI: you have provided an invalid value to the `input` property.', 'We expect an element instance of the `Input` component.'].join('\n')) : void 0;
return _react2.default.cloneElement(input, (0, _extends3.default)({
// Most of the logic is implemented in `SelectInput`.
// The `Select` component is a simple API wrapper to expose something better to play with.
inputComponent: _SelectInput2.default,
classes: InputClasses
}, other, {
inputProps: (0, _extends3.default)({}, input ? input.props.inputProps : {}, {
autoWidth: autoWidth,
children: children,
classes: classes,
displayEmpty: displayEmpty,
native: native,
multiple: multiple,
MenuProps: MenuProps,
renderValue: renderValue
})
}));
}
Select.defaultProps = {
autoWidth: false,
displayEmpty: false,
input: _react2.default.createElement(_Input2.default, null),
native: false,
multiple: false
};
Select.muiName = 'Select';
exports.default = (0, _withStyles2.default)(styles, { name: 'MuiSelect' })(Select);

View File

@@ -0,0 +1,177 @@
// @flow
// @inheritedComponent Input
import React from 'react';
import type { ChildrenArray, Element } from 'react';
import warning from 'warning';
import SelectInput from './SelectInput';
import withStyles from '../styles/withStyles';
import Input from '../Input'; // Import to enforce the CSS injection order
import { isMuiElement } from '../utils/reactHelpers';
export const styles = (theme: Object) => ({
root: {
position: 'relative',
width: '100%',
},
select: {
'-moz-appearance': 'none', // Remove Firefox custom style
'-webkit-appearance': 'none', // Fix SSR issue
appearance: 'none', // Reset
// When interacting quickly, the text can end up selected.
// Native select can't be selected either.
userSelect: 'none',
padding: `0 ${theme.spacing.unit * 4}px 2px 0`,
width: 'auto',
minWidth: theme.spacing.unit * 2, // So it doesn't collapse.
height: `calc(1em + ${theme.spacing.unit * 2 - 2}px)`,
cursor: 'pointer',
'&:focus': {
// Show that it's not an text input
background:
theme.palette.type === 'light' ? 'rgba(0, 0, 0, 0.05)' : 'rgba(255, 255, 255, 0.05)',
borderRadius: 0, // Reset Chrome style
},
// Remove Firefox focus border
'&:-moz-focusring': {
color: 'transparent',
textShadow: '0 0 0 #000',
},
// Remove IE11 arrow
'&::-ms-expand': {
display: 'none',
},
},
selectMenu: {
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
overflow: 'hidden',
lineHeight: `calc(1em + ${theme.spacing.unit * 2 - 2}px)`,
},
disabled: {
cursor: 'default',
},
icon: {
position: 'absolute',
right: 0,
top: 4,
color: theme.palette.text.secondary,
'pointer-events': 'none', // Don't block pinter events on the select under the icon.
},
});
type ProvidedProps = {
classes: Object,
displayEmpty: boolean,
input: Element<any>,
native: boolean,
multiple: boolean,
};
export type Props = {
/**
* If true, the width of the popover will automatically be set according to the items inside the
* menu, otherwise it will be at least the width of the select input.
*/
autoWidth?: boolean,
/**
* The option elements to populate the select with.
* Can be some `MenuItem` when `native` is false and `option` when `native` is true.
*/
children: $ReadOnlyArray<ChildrenArray<*>>,
/**
* Useful to extend the style applied to components.
*/
classes?: Object,
/**
* If `true`, the selected item is displayed even if its value is empty.
* You can only use it when the `native` property is `false` (default).
*/
displayEmpty?: boolean,
/**
* An `Input` element; does not have to be a material-ui specific `Input`.
*/
input?: Element<any>,
/**
* `classes` property applied to the `Input` element.
*/
InputClasses?: Object,
/**
* If `true`, the component will be using a native `select` element.
*/
native?: boolean,
/**
* If true, `value` must be an array and the menu will support multiple selections.
* You can only use it when the `native` property is `false` (default).
*/
multiple?: boolean,
/**
* Properties applied to the `Menu` element.
*/
MenuProps?: Object,
/**
* Render the selected value.
* You can only use it when the `native` property is `false` (default).
*/
renderValue?: Function,
/**
* The input value, required for a controlled component.
*/
value?: $ReadOnlyArray<string | number> | string | number,
};
function Select(props: ProvidedProps & Props) {
const {
autoWidth,
children,
classes,
displayEmpty,
input,
InputClasses,
native,
multiple,
MenuProps,
renderValue,
...other
} = props;
// Instead of `Element<typeof Input>` to have more flexibility.
warning(
isMuiElement(input, ['Input']),
[
'Material-UI: you have provided an invalid value to the `input` property.',
'We expect an element instance of the `Input` component.',
].join('\n'),
);
return React.cloneElement(input, {
// Most of the logic is implemented in `SelectInput`.
// The `Select` component is a simple API wrapper to expose something better to play with.
inputComponent: SelectInput,
classes: InputClasses,
...other,
inputProps: {
...(input ? input.props.inputProps : {}),
autoWidth,
children,
classes,
displayEmpty,
native,
multiple,
MenuProps,
renderValue,
},
});
}
Select.defaultProps = {
autoWidth: false,
displayEmpty: false,
input: <Input />,
native: false,
multiple: false,
};
Select.muiName = 'Select';
export default withStyles(styles, { name: 'MuiSelect' })(Select);

View File

@@ -0,0 +1,30 @@
import * as React from 'react';
import { StandardProps } from '..';
export interface SelectInputProps extends StandardProps<{}, SelectInputClassKey> {
autoWidth: boolean;
disabled?: boolean;
native: boolean;
multiple: boolean;
MenuProps?: Object;
name?: string;
onBlur?: React.FocusEventHandler<any>;
onChange?: (event: React.ChangeEvent<{}>, child: React.ReactNode) => void,
onFocus?: React.FocusEventHandler<any>;
readOnly?: boolean;
renderValue?: Function;
selectRef?: Function;
value?: string | number | Array<string | number>;
}
export type SelectInputClassKey =
| 'root'
| 'select'
| 'selectMenu'
| 'disabled'
| 'icon'
;
declare const SelectInput: React.ComponentType<SelectInputProps>;
export default SelectInput;

View File

@@ -0,0 +1,438 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _defineProperty2 = require('babel-runtime/helpers/defineProperty');
var _defineProperty3 = _interopRequireDefault(_defineProperty2);
var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _keycode = require('keycode');
var _keycode2 = _interopRequireDefault(_keycode);
var _warning = require('warning');
var _warning2 = _interopRequireDefault(_warning);
var _Menu = require('../Menu/Menu');
var _Menu2 = _interopRequireDefault(_Menu);
var _Input = require('../Input/Input');
var _ArrowDropDown = require('../svg-icons/ArrowDropDown');
var _ArrowDropDown2 = _interopRequireDefault(_ArrowDropDown);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var babelPluginFlowReactPropTypes_proptype_Node = require('react').babelPluginFlowReactPropTypes_proptype_Node || require('prop-types').any;
var babelPluginFlowReactPropTypes_proptype_Element = require('react').babelPluginFlowReactPropTypes_proptype_Element || require('prop-types').any;
var babelPluginFlowReactPropTypes_proptype_Props = {
/**
* If true, the width of the popover will automatically be set according to the items inside the
* menu, otherwise it will be at least the width of the select input.
*/
autoWidth: require('prop-types').bool.isRequired,
/**
* The option elements to populate the select with.
* Can be some `MenuItem` when `native` is false and `option` when `native` is true.
*/
children: typeof babelPluginFlowReactPropTypes_proptype_Node === 'function' ? babelPluginFlowReactPropTypes_proptype_Node.isRequired ? babelPluginFlowReactPropTypes_proptype_Node.isRequired : babelPluginFlowReactPropTypes_proptype_Node : require('prop-types').shape(babelPluginFlowReactPropTypes_proptype_Node).isRequired,
/**
* Useful to extend the style applied to components.
*/
classes: require('prop-types').object,
/**
* The CSS class name of the select element.
*/
className: require('prop-types').string,
/**
* If `true`, the select will be disabled.
*/
disabled: require('prop-types').bool,
/**
* If `true`, the selected item is displayed even if its value is empty.
* You can only use it when the `native` property is `false` (default).
*/
displayEmpty: require('prop-types').bool.isRequired,
/**
* If `true`, the component will be using a native `select` element.
*/
native: require('prop-types').bool.isRequired,
/**
* If true, `value` must be an array and the menu will support multiple selections.
* You can only use it when the `native` property is `false` (default).
*/
multiple: require('prop-types').bool.isRequired,
/**
* Properties applied to the `Menu` element.
*/
MenuProps: require('prop-types').object,
/**
* Name attribute of the `select` or hidden `input` element.
*/
name: require('prop-types').string,
/**
* @ignore
*/
onBlur: require('prop-types').func,
/**
* Callback function fired when a menu item is selected.
*
* @param {object} event The event source of the callback
* @param {object} child The react element that was selected
*/
onChange: require('prop-types').func,
/**
* @ignore
*/
onFocus: require('prop-types').func,
/**
* @ignore
*/
readOnly: require('prop-types').bool,
/**
* Render the selected value.
* You can only use it when the `native` property is `false` (default).
*/
renderValue: require('prop-types').func,
/**
* Use that property to pass a ref callback to the native select element.
*/
selectRef: require('prop-types').func,
/**
* The value of the component, required for a controlled component.
*/
value: require('prop-types').oneOfType([require('prop-types').string, require('prop-types').number, typeof $ReadOnlyArray === 'function' ? require('prop-types').instanceOf($ReadOnlyArray) : require('prop-types').any])
};
/**
* @ignore - internal component.
*/
var SelectInput = function (_React$Component) {
(0, _inherits3.default)(SelectInput, _React$Component);
function SelectInput() {
var _ref;
var _temp, _this, _ret;
(0, _classCallCheck3.default)(this, SelectInput);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = SelectInput.__proto__ || (0, _getPrototypeOf2.default)(SelectInput)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
anchorEl: null,
open: false
}, _this.ignoreNextBlur = false, _this.handleClick = function (event) {
// Opening the menu is going to blur the. It will be focused back when closed.
_this.ignoreNextBlur = true;
_this.setState({
open: true,
anchorEl: event.currentTarget
});
}, _this.handleRequestClose = function () {
_this.setState({
open: false
});
}, _this.handleItemClick = function (child) {
return function (event) {
if (!_this.props.multiple) {
_this.setState({
open: false
});
}
if (_this.props.onChange) {
var _onChange = _this.props.onChange;
var _value = void 0;
var _target = void 0;
if (event.target) {
_target = event.target;
}
if (_this.props.multiple) {
_value = Array.isArray(_this.props.value) ? [].concat((0, _toConsumableArray3.default)(_this.props.value)) : [];
var itemIndex = _value.indexOf(child.props.value);
if (itemIndex === -1) {
_value.push(child.props.value);
} else {
_value.splice(itemIndex, 1);
}
} else {
_value = child.props.value;
}
event.persist();
event.target = (0, _extends3.default)({}, _target, { value: _value });
_onChange(event, child);
}
};
}, _this.handleBlur = function (event) {
if (_this.ignoreNextBlur === true) {
// The parent components are relying on the bubbling of the event.
event.stopPropagation();
_this.ignoreNextBlur = false;
return;
}
if (_this.props.onBlur) {
_this.props.onBlur(event);
}
}, _this.handleKeyDown = function (event) {
if (_this.props.readOnly) {
return;
}
if (['space', 'up', 'down'].includes((0, _keycode2.default)(event))) {
event.preventDefault();
// Opening the menu is going to blur the. It will be focused back when closed.
_this.ignoreNextBlur = true;
_this.setState({
open: true,
anchorEl: event.currentTarget
});
}
}, _this.handleSelectRef = function (node) {
if (!_this.props.selectRef) {
return;
}
_this.props.selectRef({
node: node,
// By pass the native input as we expose a rich object (array).
value: _this.props.value
});
}, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
}
(0, _createClass3.default)(SelectInput, [{
key: 'render',
value: function render() {
var _this2 = this;
var _props = this.props,
autoWidth = _props.autoWidth,
children = _props.children,
classNameProp = _props.className,
classes = _props.classes,
disabled = _props.disabled,
displayEmpty = _props.displayEmpty,
name = _props.name,
native = _props.native,
multiple = _props.multiple,
_props$MenuProps = _props.MenuProps,
MenuProps = _props$MenuProps === undefined ? {} : _props$MenuProps,
onBlur = _props.onBlur,
onChange = _props.onChange,
onFocus = _props.onFocus,
readOnly = _props.readOnly,
renderValue = _props.renderValue,
selectRef = _props.selectRef,
value = _props.value,
other = (0, _objectWithoutProperties3.default)(_props, ['autoWidth', 'children', 'className', 'classes', 'disabled', 'displayEmpty', 'name', 'native', 'multiple', 'MenuProps', 'onBlur', 'onChange', 'onFocus', 'readOnly', 'renderValue', 'selectRef', 'value']);
if (native) {
process.env.NODE_ENV !== "production" ? (0, _warning2.default)(multiple === false, 'Material-UI: you can not use the `native` and `multiple` properties ' + 'at the same time on a `Select` component.') : void 0;
process.env.NODE_ENV !== "production" ? (0, _warning2.default)(!renderValue, 'Material-UI: the `renderValue` property is not used by the native implementation.') : void 0;
process.env.NODE_ENV !== "production" ? (0, _warning2.default)(!displayEmpty, 'Material-UI: the `displayEmpty` property is not used by the native implementation.') : void 0;
return _react2.default.createElement(
'div',
{ className: classes.root },
_react2.default.createElement(
'select',
(0, _extends3.default)({
className: (0, _classnames2.default)(classes.select, (0, _defineProperty3.default)({}, classes.disabled, disabled), classNameProp),
name: name,
disabled: disabled,
onBlur: onBlur,
onChange: onChange,
onFocus: onFocus,
value: value,
readOnly: readOnly
}, other, {
ref: selectRef
}),
children
),
_react2.default.createElement(_ArrowDropDown2.default, { className: classes.icon })
);
}
if (value === undefined) {
throw new Error('Material-UI: the `value` property is required ' + 'when using the `Select` component with `native=false`.');
}
var display = void 0;
var displaySingle = '';
var displayMultiple = [];
var computeDisplay = false;
// No need to display any value if the field is empty.
if ((0, _Input.isDirty)(this.props) || displayEmpty) {
if (renderValue) {
display = renderValue(value);
} else {
computeDisplay = true;
}
}
var items = _react2.default.Children.map(children, function (child) {
if (!_react2.default.isValidElement(child)) {
return null;
}
var selected = void 0;
if (multiple) {
if (!Array.isArray(value)) {
throw new Error('Material-UI: the `value` property must be an array ' + 'when using the `Select` component with `multiple`.');
}
selected = value.indexOf(child.props.value) !== -1;
if (selected && computeDisplay) {
displayMultiple.push(child.props.children);
}
} else {
selected = value === child.props.value;
if (selected && computeDisplay) {
displaySingle = child.props.children;
}
}
return _react2.default.cloneElement(child, {
role: 'option',
selected: selected,
onClick: _this2.handleItemClick(child)
});
});
if (computeDisplay) {
display = multiple ? displayMultiple.join(', ') : displaySingle;
}
var minimumMenuWidth = this.state.anchorEl != null && !autoWidth ? this.state.anchorEl.clientWidth : undefined;
return _react2.default.createElement(
'div',
{ className: classes.root },
_react2.default.createElement(
'div',
{
className: (0, _classnames2.default)(classes.select, classes.selectMenu, (0, _defineProperty3.default)({}, classes.disabled, disabled), classNameProp),
'aria-pressed': this.state.open ? 'true' : 'false',
tabIndex: disabled ? null : 0,
role: 'button',
'aria-owns': this.state.open ? 'menu-' + (name || '') : null,
'aria-haspopup': 'true',
onKeyDown: this.handleKeyDown,
onBlur: this.handleBlur,
onClick: disabled || readOnly ? null : this.handleClick,
onFocus: onFocus
},
display
),
_react2.default.createElement('input', (0, _extends3.default)({
value: Array.isArray(value) ? value.join(',') : value,
name: name,
readOnly: readOnly
}, other, {
ref: this.handleSelectRef,
type: 'hidden'
})),
_react2.default.createElement(_ArrowDropDown2.default, { className: classes.icon }),
_react2.default.createElement(
_Menu2.default,
(0, _extends3.default)({
id: 'menu-' + (name || ''),
anchorEl: this.state.anchorEl,
open: this.state.open,
onRequestClose: this.handleRequestClose
}, MenuProps, {
MenuListProps: (0, _extends3.default)({}, MenuProps.MenuListProps, {
role: 'listbox'
}),
PaperProps: (0, _extends3.default)({}, MenuProps.PaperProps, {
style: (0, _extends3.default)({
minWidth: minimumMenuWidth
}, MenuProps.PaperProps != null ? MenuProps.PaperProps.style : null)
})
}),
items
)
);
}
}]);
return SelectInput;
}(_react2.default.Component);
SelectInput.muiName = 'SelectInput';
exports.default = SelectInput;

View File

@@ -0,0 +1,386 @@
// @flow
import React from 'react';
import type { Element, Node } from 'react';
import classNames from 'classnames';
import keycode from 'keycode';
import warning from 'warning';
import Menu from '../Menu/Menu';
import { isDirty } from '../Input/Input';
import ArrowDropDownIcon from '../svg-icons/ArrowDropDown';
type ProvidedProps = {
classes: Object,
};
export type Props = {
/**
* If true, the width of the popover will automatically be set according to the items inside the
* menu, otherwise it will be at least the width of the select input.
*/
autoWidth: boolean,
/**
* The option elements to populate the select with.
* Can be some `MenuItem` when `native` is false and `option` when `native` is true.
*/
children: Node,
/**
* Useful to extend the style applied to components.
*/
classes?: Object,
/**
* The CSS class name of the select element.
*/
className?: string,
/**
* If `true`, the select will be disabled.
*/
disabled?: boolean,
/**
* If `true`, the selected item is displayed even if its value is empty.
* You can only use it when the `native` property is `false` (default).
*/
displayEmpty: boolean,
/**
* If `true`, the component will be using a native `select` element.
*/
native: boolean,
/**
* If true, `value` must be an array and the menu will support multiple selections.
* You can only use it when the `native` property is `false` (default).
*/
multiple: boolean,
/**
* Properties applied to the `Menu` element.
*/
MenuProps?: Object,
/**
* Name attribute of the `select` or hidden `input` element.
*/
name?: string,
/**
* @ignore
*/
onBlur?: Function,
/**
* Callback function fired when a menu item is selected.
*
* @param {object} event The event source of the callback
* @param {object} child The react element that was selected
*/
onChange?: (event: SyntheticUIEvent<*>, child: Element<any>) => void,
/**
* @ignore
*/
onFocus?: Function,
/**
* @ignore
*/
readOnly?: boolean,
/**
* Render the selected value.
* You can only use it when the `native` property is `false` (default).
*/
renderValue?: Function,
/**
* Use that property to pass a ref callback to the native select element.
*/
selectRef?: Function,
/**
* The value of the component, required for a controlled component.
*/
value?: string | number | $ReadOnlyArray<string | number>,
};
type State = {
open: boolean,
anchorEl: ?HTMLElement,
};
/**
* @ignore - internal component.
*/
class SelectInput extends React.Component<ProvidedProps & Props, State> {
static muiName = 'SelectInput';
state = {
anchorEl: null,
open: false,
};
ignoreNextBlur = false;
handleClick = (event: SyntheticMouseEvent<HTMLElement>) => {
// Opening the menu is going to blur the. It will be focused back when closed.
this.ignoreNextBlur = true;
this.setState({
open: true,
anchorEl: event.currentTarget,
});
};
handleRequestClose = () => {
this.setState({
open: false,
});
};
handleItemClick = (child: Element<any>) => (event: SyntheticMouseEvent<> & { target?: any }) => {
if (!this.props.multiple) {
this.setState({
open: false,
});
}
if (this.props.onChange) {
const { onChange } = this.props;
let value;
let target;
if (event.target) {
target = event.target;
}
if (this.props.multiple) {
value = Array.isArray(this.props.value) ? [...this.props.value] : [];
const itemIndex = value.indexOf(child.props.value);
if (itemIndex === -1) {
value.push(child.props.value);
} else {
value.splice(itemIndex, 1);
}
} else {
value = child.props.value;
}
event.persist();
event.target = { ...target, value };
onChange(event, child);
}
};
handleBlur = (event: SyntheticFocusEvent<>) => {
if (this.ignoreNextBlur === true) {
// The parent components are relying on the bubbling of the event.
event.stopPropagation();
this.ignoreNextBlur = false;
return;
}
if (this.props.onBlur) {
this.props.onBlur(event);
}
};
handleKeyDown = (event: SyntheticKeyboardEvent<HTMLElement>) => {
if (this.props.readOnly) {
return;
}
if (['space', 'up', 'down'].includes(keycode(event))) {
event.preventDefault();
// Opening the menu is going to blur the. It will be focused back when closed.
this.ignoreNextBlur = true;
this.setState({
open: true,
anchorEl: event.currentTarget,
});
}
};
handleSelectRef = (node: ?HTMLElement) => {
if (!this.props.selectRef) {
return;
}
this.props.selectRef({
node,
// By pass the native input as we expose a rich object (array).
value: this.props.value,
});
};
render() {
const {
autoWidth,
children,
className: classNameProp,
classes,
disabled,
displayEmpty,
name,
native,
multiple,
MenuProps = {},
onBlur,
onChange,
onFocus,
readOnly,
renderValue,
selectRef,
value,
...other
} = this.props;
if (native) {
warning(
multiple === false,
'Material-UI: you can not use the `native` and `multiple` properties ' +
'at the same time on a `Select` component.',
);
warning(
!renderValue,
'Material-UI: the `renderValue` property is not used by the native implementation.',
);
warning(
!displayEmpty,
'Material-UI: the `displayEmpty` property is not used by the native implementation.',
);
return (
<div className={classes.root}>
<select
className={classNames(
classes.select,
{
[classes.disabled]: disabled,
},
classNameProp,
)}
name={name}
disabled={disabled}
onBlur={onBlur}
onChange={onChange}
onFocus={onFocus}
value={value}
readOnly={readOnly}
{...other}
ref={selectRef}
>
{children}
</select>
<ArrowDropDownIcon className={classes.icon} />
</div>
);
}
if (value === undefined) {
throw new Error(
'Material-UI: the `value` property is required ' +
'when using the `Select` component with `native=false`.',
);
}
let display;
let displaySingle = '';
const displayMultiple = [];
let computeDisplay = false;
// No need to display any value if the field is empty.
if (isDirty(this.props) || displayEmpty) {
if (renderValue) {
display = renderValue(value);
} else {
computeDisplay = true;
}
}
const items = React.Children.map(children, child => {
if (!React.isValidElement(child)) {
return null;
}
let selected;
if (multiple) {
if (!Array.isArray(value)) {
throw new Error(
'Material-UI: the `value` property must be an array ' +
'when using the `Select` component with `multiple`.',
);
}
selected = value.indexOf(child.props.value) !== -1;
if (selected && computeDisplay) {
displayMultiple.push(child.props.children);
}
} else {
selected = value === child.props.value;
if (selected && computeDisplay) {
displaySingle = child.props.children;
}
}
return React.cloneElement(child, {
role: 'option',
selected,
onClick: this.handleItemClick(child),
});
});
if (computeDisplay) {
display = multiple ? displayMultiple.join(', ') : displaySingle;
}
const minimumMenuWidth =
this.state.anchorEl != null && !autoWidth ? this.state.anchorEl.clientWidth : undefined;
return (
<div className={classes.root}>
<div
className={classNames(
classes.select,
classes.selectMenu,
{
[classes.disabled]: disabled,
},
classNameProp,
)}
data-mui-test="SelectDisplay"
aria-pressed={this.state.open ? 'true' : 'false'}
tabIndex={disabled ? null : 0}
role="button"
aria-owns={this.state.open ? `menu-${name || ''}` : null}
aria-haspopup="true"
onKeyDown={this.handleKeyDown}
onBlur={this.handleBlur}
onClick={disabled || readOnly ? null : this.handleClick}
onFocus={onFocus}
>
{display}
</div>
<input
value={Array.isArray(value) ? value.join(',') : value}
name={name}
readOnly={readOnly}
{...other}
ref={this.handleSelectRef}
type="hidden"
/>
<ArrowDropDownIcon className={classes.icon} />
<Menu
id={`menu-${name || ''}`}
anchorEl={this.state.anchorEl}
open={this.state.open}
onRequestClose={this.handleRequestClose}
{...MenuProps}
MenuListProps={{
...MenuProps.MenuListProps,
role: 'listbox',
}}
PaperProps={{
...MenuProps.PaperProps,
style: {
minWidth: minimumMenuWidth,
...(MenuProps.PaperProps != null ? MenuProps.PaperProps.style : null),
},
}}
>
{items}
</Menu>
</div>
);
}
}
export default SelectInput;

View File

@@ -0,0 +1,2 @@
export { default } from './Select';
export * from './Select';

View File

@@ -0,0 +1,16 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _Select = require('./Select');
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function get() {
return _interopRequireDefault(_Select).default;
}
});
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

View File

@@ -0,0 +1,3 @@
// @flow
export { default } from './Select';