Completely updated React, fixed #11, (hopefully)

This commit is contained in:
2018-03-04 19:11:49 -05:00
parent 6e0afd6e2a
commit 34e5f5139a
13674 changed files with 333464 additions and 473223 deletions

View File

@@ -1,17 +1,17 @@
// @flow weak
import React from 'react';
import type { Node } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import withStyles from '../styles/withStyles';
import createSwitch from '../internal/SwitchBase';
import SwitchBase from '../internal/SwitchBase';
export const styles = (theme: Object) => ({
export const styles = theme => ({
root: {
display: 'inline-flex',
width: 62,
position: 'relative',
flexShrink: 0,
// For correct alignment with the text.
verticalAlign: 'middle',
},
bar: {
borderRadius: 7,
@@ -26,7 +26,8 @@ export const styles = (theme: Object) => ({
transition: theme.transitions.create(['opacity', 'background-color'], {
duration: theme.transitions.duration.shortest,
}),
backgroundColor: theme.palette.type === 'light' ? '#000' : '#fff',
backgroundColor:
theme.palette.type === 'light' ? theme.palette.common.black : theme.palette.common.white,
opacity: theme.palette.type === 'light' ? 0.38 : 0.3,
},
icon: {
@@ -36,6 +37,9 @@ export const styles = (theme: Object) => ({
height: 20,
borderRadius: '50%',
},
iconChecked: {
boxShadow: theme.shadows[2],
},
// For SwitchBase
default: {
zIndex: 1,
@@ -45,119 +49,130 @@ export const styles = (theme: Object) => ({
}),
},
checked: {
color: theme.palette.primary[500],
transform: 'translateX(14px)',
'& + $bar': {
backgroundColor: theme.palette.primary[500],
opacity: 0.5,
},
},
checkedPrimary: {
color: theme.palette.primary.main,
'& + $bar': {
backgroundColor: theme.palette.primary.main,
},
},
checkedSecondary: {
color: theme.palette.secondary.main,
'& + $bar': {
backgroundColor: theme.palette.secondary.main,
},
},
disabled: {
color: theme.palette.type === 'light' ? theme.palette.grey[400] : theme.palette.grey[800],
'& + $bar': {
backgroundColor: theme.palette.type === 'light' ? '#000' : '#fff',
backgroundColor:
theme.palette.type === 'light' ? theme.palette.common.black : theme.palette.common.white,
opacity: theme.palette.type === 'light' ? 0.12 : 0.1,
},
'& $icon': {
boxShadow: theme.shadows[1],
},
},
});
const SwitchBase = createSwitch();
function Switch(props) {
const { classes, className, color, ...other } = props;
const icon = <span className={classes.icon} />;
const checkedIcon = <span className={classNames(classes.icon, classes.iconChecked)} />;
const checkedClass = classNames(classes.checked, {
[classes.checkedPrimary]: color === 'primary',
[classes.checkedSecondary]: color === 'secondary',
});
type ProvidedProps = {
classes: Object,
};
return (
<span className={classNames(classes.root, className)}>
<SwitchBase
icon={icon}
classes={{
default: classes.default,
checked: checkedClass,
disabled: classes.disabled,
}}
checkedIcon={checkedIcon}
{...other}
/>
<span className={classes.bar} />
</span>
);
}
export type Props = {
Switch.propTypes = {
/**
* If `true`, the component is checked.
*/
checked?: boolean | string,
/**
* The CSS class name of the root element when checked.
*/
checkedClassName?: string,
checked: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
/**
* The icon to display when the component is checked.
* If a string is provided, it will be used as a font ligature.
*/
checkedIcon?: Node,
checkedIcon: PropTypes.node,
/**
* Useful to extend the style applied to components.
*/
classes?: Object,
classes: PropTypes.object.isRequired,
/**
* @ignore
*/
className?: string,
className: PropTypes.string,
/**
* The color of the component. It supports those theme colors that make sense for this component.
*/
color: PropTypes.oneOf(['primary', 'secondary']),
/**
* @ignore
*/
defaultChecked?: boolean,
defaultChecked: PropTypes.bool,
/**
* If `true`, the switch will be disabled.
*/
disabled?: boolean,
/**
* The CSS class name of the root element when disabled.
*/
disabledClassName?: string,
disabled: PropTypes.bool,
/**
* If `true`, the ripple effect will be disabled.
*/
disableRipple?: boolean,
disableRipple: PropTypes.bool,
/**
* The icon to display when the component is unchecked.
* If a string is provided, it will be used as a font ligature.
*/
icon?: Node,
icon: PropTypes.node,
/**
* The id of the `input` element.
*/
id: PropTypes.string,
/**
* Properties applied to the `input` element.
*/
inputProps?: Object,
inputProps: PropTypes.object,
/**
* Use that property to pass a ref callback to the native input component.
*/
inputRef?: Function,
/*
* @ignore
*/
name?: string,
inputRef: PropTypes.func,
/**
* Callback fired when the state is changed.
*
* @param {object} event The event source of the callback
* @param {boolean} checked The `checked` value of the switch
*/
onChange?: Function,
onChange: PropTypes.func,
/**
* @ignore
* The input component property `type`.
*/
tabIndex?: number | string,
type: PropTypes.string,
/**
* The value of the component.
*/
value?: string,
value: PropTypes.string,
};
function Switch(props: ProvidedProps & Props) {
const { classes, className, ...other } = props;
const icon = <div className={classes.icon} />;
return (
<div className={classNames(classes.root, className)}>
<SwitchBase
icon={icon}
classes={{
default: classes.default,
checked: classes.checked,
disabled: classes.disabled,
}}
checkedIcon={icon}
{...other}
/>
<div className={classes.bar} />
</div>
);
}
Switch.defaultProps = {
color: 'secondary',
};
export default withStyles(styles, { name: 'MuiSwitch' })(Switch);