Skip to content

Commit bda1ff1

Browse files
committed
Convert LogIn to use shared Button
1 parent 499c17e commit bda1ff1

File tree

5 files changed

+112
-85
lines changed

5 files changed

+112
-85
lines changed

Diff for: client/common/Button.jsx

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import styled from 'styled-components';
4+
import { Link } from 'react-router';
5+
6+
import { remSize, prop } from '../theme';
7+
8+
// The '&&&' will increase the specificity of the
9+
// component's CSS so that it overrides the more
10+
// general global styles
11+
const StyledButton = styled.button`
12+
&&& {
13+
display: flex;
14+
justify-content: center;
15+
align-items: center;
16+
17+
width: max-content;
18+
text-decoration: none;
19+
20+
background-color: ${prop('buttonColorBackground')};
21+
color: ${prop('buttonColor')};
22+
cursor: pointer;
23+
border: 2px solid ${prop('buttonBorderColor')};
24+
border-radius: 2px;
25+
padding: ${remSize(8)} ${remSize(25)};
26+
line-height: 1;
27+
28+
&:hover:not(:disabled) {
29+
color: ${prop('buttonHoverColor')};
30+
background-color: ${prop('buttonHoverColorBackground')};
31+
}
32+
33+
&:disabled {
34+
color: ${prop('buttonDisabledColor')};
35+
background-color: ${prop('buttonDisabledColorBackground')};
36+
cursor: not-allowed;
37+
}
38+
}
39+
`;
40+
41+
/**
42+
* A Button performs an primary action
43+
*/
44+
const Button = ({
45+
children, href, label, to, type, ...props
46+
}) => {
47+
if (href) {
48+
return <StyledButton as="a" aria-label={label} href={href} {...props}>{children}</StyledButton>;
49+
}
50+
51+
if (to) {
52+
return <StyledButton as={Link} aria-label={label} to={to} {...props}>{children}</StyledButton>;
53+
}
54+
55+
return <StyledButton aria-label={label} type={type} {...props}>{children}</StyledButton>;
56+
};
57+
58+
Button.defaultProps = {
59+
disabled: false,
60+
href: null,
61+
to: null,
62+
type: 'button',
63+
};
64+
65+
Button.propTypes = {
66+
/**
67+
* The visible part of the button, telling the user what
68+
* the action is
69+
*/
70+
children: PropTypes.element.isRequired,
71+
/**
72+
If the button can be activated or not
73+
*/
74+
disabled: PropTypes.bool,
75+
/**
76+
* Specifying an href will use an <a> to link to the URL
77+
*/
78+
href: PropTypes.string,
79+
/*
80+
* An ARIA Label used for accessibility
81+
*/
82+
label: PropTypes.string.isRequired,
83+
/**
84+
* Specifying a to URL will use a react-router Link
85+
*/
86+
to: PropTypes.string,
87+
/**
88+
* If using a button, then type is defines the type of button
89+
*/
90+
type: PropTypes.oneOf(['button', 'submit']),
91+
};
92+
93+
export default Button;

Diff for: client/components/Button/Button.stories.jsx renamed to client/common/Button.stories.jsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import React from 'react';
22
import { action } from '@storybook/addon-actions';
33
import { boolean, text } from '@storybook/addon-knobs';
44

5-
import Button from '.';
5+
import Button from './Button';
66

77
export default {
8-
title: 'Common/Button (JS)',
8+
title: 'Common/Button',
99
component: Button
1010
};
1111

@@ -26,3 +26,11 @@ export const SubmitButton = () => (
2626
export const PrimaryButton = () => <Button label="login" onClick={action('onClick')}>Log In</Button>;
2727

2828
export const DisabledButton = () => <Button disabled label="login" onClick={action('onClick')}>Log In</Button>;
29+
30+
export const AnchorButton = () => (
31+
<Button href="http://p5js.org" label="submit">Actually an anchor</Button>
32+
);
33+
34+
export const ReactRouterLink = () => (
35+
<Button to="./somewhere" label="submit">Actually a Link</Button>
36+
);

Diff for: client/components/Button/Button.stories.mdx

-32
This file was deleted.

Diff for: client/components/Button/index.js

-50
This file was deleted.

Diff for: client/modules/User/components/LoginForm.jsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import PropTypes from 'prop-types';
22
import React from 'react';
3+
4+
import Button from '../../../common/Button';
5+
36
import { domOnlyProps } from '../../../utils/reduxFormUtils';
47

58
function LoginForm(props) {
@@ -30,7 +33,12 @@ function LoginForm(props) {
3033
/>
3134
{password.touched && password.error && <span className="form-error">{password.error}</span>}
3235
</p>
33-
<input type="submit" disabled={submitting || pristine} value="Log In" aria-label="login" />
36+
<Button
37+
aria-label="login"
38+
type="submit"
39+
disabled={submitting || pristine}
40+
>Log In
41+
</Button>
3442
</form>
3543
);
3644
}

0 commit comments

Comments
 (0)