Skip to content

Commit

Permalink
Add disable button (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbarela committed Feb 7, 2021
1 parent e307046 commit a080593
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/Button/__snapshots__/index.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`Button renders with button style 1`] = `
<div>
<button
class="button-0-2-1 button-d4-0-2-7"
class="button-0-2-1 button-d6-0-2-10 common-0-2-2 common-d7-0-2-11"
type="button"
>
Press Me
Expand All @@ -14,7 +14,7 @@ exports[`Button renders with button style 1`] = `
exports[`Button renders with link style 1`] = `
<div>
<button
class="link-0-2-2 link-d3-0-2-6"
class="button-0-2-1 button-d3-0-2-7 common-0-2-2 common-d4-0-2-8"
type="button"
>
Click Me
Expand All @@ -25,7 +25,7 @@ exports[`Button renders with link style 1`] = `
exports[`Button renders with no style 1`] = `
<div>
<button
class="button-0-2-1 button-d0-0-2-3"
class="button-0-2-1 button-d0-0-2-4 common-0-2-2 common-d1-0-2-5"
type="button"
>
Press Me
Expand Down
49 changes: 43 additions & 6 deletions src/Button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,85 @@ import { createUseStyles, useTheme } from "react-jss";
type ButtonProps = {
onClick?: () => void,
name: string,
style?: string,
disabled?: boolean,
type?: string,
};

export type ButtonStyleProps = {
color?: string,
margin?: string,
marginRight?: string,
marginLeft?: string,
marginTop?: string,
marginBottom?: string,
};
export function Button({
onClick,
name,
disabled,
color,
style,
margin,
marginBottom,
marginLeft,
marginRight,
marginTop,
type,
}: ButtonProps & ButtonStyleProps): React.Node {
const theme = useTheme();
const classes = useButtonStyles({ color });
const classes = useButtonStyles({
color,
disabled,
margin,
marginTop,
marginBottom,
marginLeft,
marginRight,
});

return (
<button type="button" onClick={onClick} className={style && classes[style]}>
<button
type="button"
onClick={onClick}
className={[type && classes[type], classes.common].join(" ")}
disabled={disabled}
>
{name}
</button>
);
}

Button.defaultProps = {
style: "button",
type: "button",
};

export const useButtonStyles: (ButtonStyleProps) => {
button: string,
common: string,
link: string,
} = createUseStyles((theme) => {
return {
button: {
background: theme.button.primary,
borderRadius: "10px",
borderRadius: "0.75rem",
borderStyle: "none",
color: ({ color }) => color || theme.button.color,
opacity: ({ disabled }) => (disabled ? 0.5 : 1),
},
common: {
fontSize: "1rem",
margin: ({ margin }) => margin,
marginLeft: ({ marginLeft }) => marginLeft,
marginRight: ({ marginRight }) => marginRight,
marginTop: ({ marginTop }) => marginTop,
marginBottom: ({ marginBottom }) => marginBottom,
},
link: {
border: 0,
background: "inherit",
textDecoration: "underline",
color: ({ color }) => color || theme.button.primary,
opacity: ({ disabled }) => (disabled ? 0.5 : 1),
padding: 0,
},
};
});
20 changes: 18 additions & 2 deletions src/Form/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type InputLayoutProps = {

type SubmitProps = {
name: string,
disabled?: boolean,
testID?: string,
};

Expand Down Expand Up @@ -191,17 +192,32 @@ export function SelectInput({

export function SubmitInput({
name,
disabled,
color,
margin,
marginRight,
marginLeft,
marginTop,
marginBottom,
testID,
}: SubmitProps & ButtonStyleProps): React.Node {
const classes = useButtonStyles({ color });
const classes = useButtonStyles({
color,
disabled,
margin,
marginRight,
marginLeft,
marginTop,
marginBottom,
});

return (
<input
type="submit"
value={name}
className={classes.button}
className={[classes.button, classes.common].join(" ")}
data-testid={testID}
disabled={disabled}
/>
);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ export type LayoutProps = {

type RowType = {
justifyContent: FlexAlignType,
alignItems: FlexAlignType,
backgroundColor?: string,
children?: React.Node,
className?: string,
};

export function Row({
alignItems,
justifyContent,
backgroundColor,
children,
className,
height,
width,
}: RowType & LayoutProps): React.Node {
const classes = useRowStyles({ backgroundColor, justifyContent });
const classes = useRowStyles({ alignItems, backgroundColor, justifyContent });
const layoutClasses = useLayoutStyles({ height, width });

return (
Expand All @@ -39,13 +41,15 @@ export function Row({
}

Row.defaultProps = {
alignItems: "start",
justifyContent: "space-between",
};

const useRowStyles = createUseStyles({
row: {
display: "flex",
padding: "5px 0",
padding: "0.375rem 0",
alignItems: ({ alignItems }: { alignItems: FlexAlignType }) => alignItems,
justifyContent: ({ justifyContent }: { justifyContent: FlexAlignType }) =>
justifyContent,
backgroundColor: ({ backgroundColor }: { backgroundColor: string }) =>
Expand Down
12 changes: 10 additions & 2 deletions stories/Button/Button.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ and more traditional web link.

export const Template = (args) => (
<div>
<Row justifyContent="start">
<Row justifyContent="start" alignItems="center">
This is a normal button <Button {...args} />
</Row>
<Row justifyContent="start">
This a link button <Button {...args} style="link" />
This a link button{" "}
<Button
{...args}
type="link"
marginLeft="0.5rem"
marginRight="0"
marginTop="0"
marginBottom="0"
/>
</Row>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion stories/Form/Form.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const Template = (args) => (
/>
</Row>
<Row>
<SubmitInput name="Submit" />
<SubmitInput name="Submit" disabled={true} />
</Row>
</Form>
);
Expand Down

0 comments on commit a080593

Please sign in to comment.