Skip to content

Commit

Permalink
Fixing styling issues with variants
Browse files Browse the repository at this point in the history
Add test
  • Loading branch information
duncan-thacker committed Feb 27, 2019
1 parent 5ae5520 commit 7dcc844
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 17 deletions.
3 changes: 2 additions & 1 deletion docsrc/ControlPanel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { TextField, Switch, FormControlLabel, FormGroup, FormControl, InputLabel, Select, MenuItem } from "@material-ui/core";
import { TextField, Switch, FormControlLabel, FormGroup, FormControl, InputLabel, Select, MenuItem, Typography } from "@material-ui/core";
import { string, bool, oneOf } from "prop-types";

function getFieldElement({ propName, propType, label, helperText, options }, value, onUpdate) {
Expand Down Expand Up @@ -47,6 +47,7 @@ export default function ControlPanel({ fields, value, onChange }) {
const handleUpdateValue = (propName, propValue) => onChange({ ...value, [propName]: propValue });
return (
<div style={ { padding: "16px", minWidth: "250px" } }>
<Typography variant="h6">Props</Typography>
{
fields.map(field => (
<FormGroup key={ field.propName }>
Expand Down
2 changes: 1 addition & 1 deletion docsrc/DemoSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function DemoSection({ DemoComponent, title }) {
<FormControlLabel control={ codeSwitch } label="Show/hide source code" />
</div>
<Markdown source={ DemoComponent.__markdown__ } />
<div style={ { width: "100%" } }>
<div style={ { width: "100%", marginTop: "8px" } }>
<DemoComponent />
<Collapse in={ isCodeOpen }>
<SyntaxHighlighter language="jsx" style={ codeStyle }>{ withoutExports(DemoComponent.__source__) }</SyntaxHighlighter>
Expand Down
9 changes: 6 additions & 3 deletions docsrc/Sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { atomDark as codeStyle } from "react-syntax-highlighter/dist/styles/pris
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { string, bool, oneOf } from "prop-types";
import ControlPanel from "./ControlPanel";
import { Paper, Divider } from "@material-ui/core";
import { Paper, Divider, Typography } from "@material-ui/core";

const CONTROL_PANEL_FIELDS = [
{ propName: "label", label: "Label", defaultValue: "Your favourite fruit", propType: string },
Expand Down Expand Up @@ -38,7 +38,8 @@ function generateSource(sandboxProps) {
}).join("\n");
return `
function MyPicker() {
const [ items, setItems ] = useState([]);
//requires React 16.8+
const [ items, setItems ] = useState(ALL_FRUITS);
return <MultiPicker
value={ items }
onChange={ setItems }
Expand All @@ -51,19 +52,21 @@ ${sandboxPropCode}
}

export default function Sandbox() {
const [ items, setItems ] = useState(ALL_FRUITS.slice(0, 2));
const [ items, setItems ] = useState(ALL_FRUITS);
const [ sandboxProps, setSandboxProps ] = useState(DEFAULT_SANDBOX_PROPS);

return (
<Paper>
<div style={ { display: "flex", alignItems: "stretch" } }>
<ControlPanel fields={ CONTROL_PANEL_FIELDS } value={ sandboxProps } onChange={ setSandboxProps } />
<div style={ { flex: "1 1 0" } }>
<Typography variant="h6">Source</Typography>
<SyntaxHighlighter language="jsx" style={ codeStyle }>{ generateSource(sandboxProps) }</SyntaxHighlighter>
</div>
</div>
<Divider />
<div style={ { padding: "32px" } }>
<Typography variant="h6">Result</Typography>
<MultiPicker
value={ items }
onChange={ setItems }
Expand Down
8 changes: 5 additions & 3 deletions src/PickerChips.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from "react";
import ChipWithPopover from "./ChipWithPopover";
import { array, func, object, bool } from "prop-types";
import { array, func, object, bool, string } from "prop-types";
import { materialColorPropType } from "./utils";

const DEFAULT_AVATAR = () => undefined;

function PickerChips({ selectedItems, disabled, color, onDelete, itemToString, itemToLabel, itemToPopover, itemToAvatar = DEFAULT_AVATAR, classes }) {
function PickerChips({ selectedItems, disabled, color, onDelete, itemToString, itemToLabel, itemToPopover, itemToAvatar = DEFAULT_AVATAR, variant, classes }) {
return (
<>
{
Expand All @@ -20,6 +20,7 @@ function PickerChips({ selectedItems, disabled, color, onDelete, itemToString, i
onDelete={ disabled ? undefined : () => onDelete(item) }
avatar={ itemToAvatar(item) }
color={ color }
variant={ variant === "filled" ? "outlined" : "standard" }
/>
)
)
Expand All @@ -37,7 +38,8 @@ PickerChips.propTypes = {
itemToLabel: func,
itemToAvatar: func,
itemToPopover: func,
classes: object
classes: object,
variant: string
};


Expand Down
21 changes: 19 additions & 2 deletions src/PickerInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const styles = theme => ({
InputLabelRoot: {
top: theme.spacing.unit
},
InputLabelFilled: {
top: theme.spacing.unit * 2
},
InputLabelShrink: {
top: 0
},
Expand All @@ -26,12 +29,25 @@ const styles = theme => ({
}
});

const isFilledOrOutlined = variant => ["filled", "outlined"].includes(variant);

function getInputPaddingStyle(variant) {
if ( variant === "outlined" ) {
return { padding: "18.5px 14px" };
}
if ( variant === "filled" ) {
return { padding: "27px 12px 10px" };
}
}

function PickerInput({ value, onChange, startAdornment, classes, fullWidth, label, onBlur, onKeyDown, disabled, error, variant, helperText, required, name, ...otherProps }) {
const InputProps = {
inputProps: {
...otherProps,
className: classes.inputRoot
className: classes.inputRoot,
style: isFilledOrOutlined(variant) ? { padding: "6.5px 0" } : undefined,
},
style: getInputPaddingStyle(variant),
startAdornment,
classes: { root: classes.InputRoot }
};
Expand All @@ -42,7 +58,8 @@ function PickerInput({ value, onChange, startAdornment, classes, fullWidth, labe
shrink: Boolean(value.length || startAdornment),
classes: {
root: classes.InputLabelRoot,
shrink: classes.InputLabelShrink
shrink: classes.InputLabelShrink,
filled: classes.InputLabelFilled
}
};
return (
Expand Down
9 changes: 9 additions & 0 deletions src/__snapshots__/cache.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ exports[`MultiPicker component uses global cache if configured 1`] = `
autoComplete="off"
classes={
Object {
"InputLabelFilled": "PickerInput-InputLabelFilled",
"InputLabelRoot": "PickerInput-InputLabelRoot",
"InputLabelShrink": "PickerInput-InputLabelShrink",
"InputRoot": "PickerInput-InputRoot",
Expand All @@ -94,6 +95,7 @@ exports[`MultiPicker component uses global cache if configured 1`] = `
InputLabelProps={
Object {
"classes": Object {
"filled": "PickerInput-InputLabelFilled",
"root": "PickerInput-InputLabelRoot",
"shrink": "PickerInput-InputLabelShrink",
},
Expand All @@ -113,8 +115,10 @@ exports[`MultiPicker component uses global cache if configured 1`] = `
"autoComplete": "off",
"className": "PickerInput-inputRoot",
"id": "downshift-0-input",
"style": undefined,
},
"startAdornment": false,
"style": undefined,
}
}
fullWidth={false}
Expand Down Expand Up @@ -171,6 +175,7 @@ exports[`MultiPicker component uses global cache if configured 1`] = `
"autoComplete": "off",
"className": "PickerInput-inputRoot",
"id": "downshift-0-input",
"style": undefined,
}
}
onBlur={[Function]}
Expand Down Expand Up @@ -206,6 +211,7 @@ exports[`MultiPicker component uses global cache if configured 1`] = `
"autoComplete": "off",
"className": "PickerInput-inputRoot",
"id": "downshift-0-input",
"style": undefined,
}
}
onBlur={[Function]}
Expand Down Expand Up @@ -242,6 +248,7 @@ exports[`MultiPicker component uses global cache if configured 1`] = `
"autoComplete": "off",
"className": "PickerInput-inputRoot",
"id": "downshift-0-input",
"style": undefined,
}
}
multiline={false}
Expand Down Expand Up @@ -284,6 +291,7 @@ exports[`MultiPicker component uses global cache if configured 1`] = `
"autoComplete": "off",
"className": "PickerInput-inputRoot",
"id": "downshift-0-input",
"style": undefined,
}
}
multiline={false}
Expand Down Expand Up @@ -326,6 +334,7 @@ exports[`MultiPicker component uses global cache if configured 1`] = `
"autoComplete": "off",
"className": "PickerInput-inputRoot",
"id": "downshift-0-input",
"style": undefined,
}
}
muiFormControl={
Expand Down
Loading

0 comments on commit 7dcc844

Please sign in to comment.