Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.0.0 Button.js #632

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
0d6178d
Merge https://github.com/onluiz/react-materialize into 3.0.0-beta.0-B…
onluiz Aug 12, 2018
b8ca981
Add new examples for Buttom component from new materializecss version
onluiz Aug 13, 2018
a26904d
Update Floating Action Button docs
onluiz Aug 13, 2018
853d6cd
Remove Floating Action Buttom from Buttom Docs
onluiz Aug 13, 2018
940627f
Remove unused imports from ButtonsPage and Floating Action Buttons Page
onluiz Aug 13, 2018
ee27853
Add test for button with small styles applied
onluiz Aug 13, 2018
9f14daf
Update Button from new materializecss version
onluiz Aug 13, 2018
12f62b5
Fix typo in smallButton sample
onluiz Aug 14, 2018
ee6b9d7
Fix typo and merge changes
onluiz Aug 14, 2018
a72eee0
Remove faicon from FixedActionButton example
onluiz Aug 15, 2018
e400c1a
Replace jquery in Button component
onluiz Aug 15, 2018
8dc6790
Add flatbutton to docs
onluiz Aug 15, 2018
d53a08b
Add instance destroy for fab button and tooltip
onluiz Aug 18, 2018
93f5a38
Add test for tooltip init and destroy methods
onluiz Aug 18, 2018
08cc808
Fix prettier
onluiz Aug 18, 2018
186dfe1
Fix tooltip prop validation in Button component
onluiz Aug 18, 2018
be5c068
Fix Button.spec tests
onluiz Aug 18, 2018
ec4764d
Remove package-lock.json
onluiz Aug 20, 2018
d275ead
Revert out of context string-helper.js file
onluiz Aug 20, 2018
5c04345
Fix lint
onluiz Aug 20, 2018
fbda675
Fix prettier
onluiz Aug 20, 2018
6c70944
Add default value for tooltipOptions in Button component
onluiz Aug 20, 2018
005f87e
Add tests for FloatingActionButton in Button component
onluiz Aug 21, 2018
2247f8a
Refactor instance destroy of button component
onluiz Aug 25, 2018
b0756f1
Adds master package-lock.json
onluiz Oct 1, 2018
f1455ee
Remove unnecessary condition at Button component
onluiz Oct 29, 2018
589f8f6
Merge button docs
onluiz Oct 31, 2018
614dd1d
Remove nonused page
onluiz Oct 31, 2018
50d8f75
Checkout modal snapshot from beta branch
onluiz Nov 1, 2018
02a39ee
reverting wrong snapshot
onluiz Nov 5, 2018
9b49e92
Merge branch '3.0.0-beta.0' into 3.0.0-beta.0-Button-Component
onluiz Nov 5, 2018
fa5d968
Add regenerate snapshot
onluiz Dec 14, 2018
696f65e
revert unwanted changes
alextrastero Dec 14, 2018
af5cde6
Revert "revert unwanted changes"
alextrastero Dec 14, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 51 additions & 12 deletions docs/src/pages/ButtonsPage.js
Expand Up @@ -7,10 +7,9 @@ import Samples from './Samples';
import ButtonCode from '!raw-loader!Button';

import raisedButton from '../../../examples/RaisedButton';
import fixedActionButton from '../../../examples/FixedActionButton';
import floatingButton from '../../../examples/FloatingButton';
import verticalFAB from '../../../examples/VerticalFAB';
import horizontalFab from '../../../examples/HorizontalFAB';
import buttonAsLink from '../../../examples/ButtonAsLink';

const ButtonsPage = () => (
<Row>
Expand All @@ -35,27 +34,67 @@ const ButtonsPage = () => (
</ReactPlayground>
</Col>
<h4 className='col s12'>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about an example of a Flat button, corresponding to the one in materializecss?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Fixed Action Button
Flat
</h4>
<p className='col s12 caption'>
Flat buttons are used to reduce excessive layering. For example, flat buttons are usually used for actions within a card or modal so there aren't too many overlapping shadows.
</p>
<Col s={12}>
<ReactPlayground code={Samples.fixedActionButton}>
{fixedActionButton}
</ReactPlayground>
<ReactPlayground code={Samples.flatButton} />
</Col>
<h4 className='col s12'>
Horizontal FAB
Submit Button
</h4>
<p className='col s12 caption'>
When you use a button to submit a form, instead of using a input tag, use a button tag with a type submit
</p>
<Col s={12}>
<ReactPlayground code={Samples.horizontalFab}>
{horizontalFab}
<ReactPlayground code={Samples.submitButton} />
</Col>
<h4 className='col s12'>
Large Button
</h4>
<p className='col s12 caption'>
This button has a larger height for buttons that need more attention.
</p>
<Col s={12}>
<ReactPlayground code={Samples.largeButton} />
</Col>
<h4 className='col s12'>
Small Button
</h4>
<p className='col s12 caption'>
When mouse and keyboard are the primary input methods, this smaller button is useful for denser UI layouts.
</p>
<Col s={12}>
<ReactPlayground code={Samples.smallButton} />
</Col>
<h4 className='col s12'>
Disabled Button
</h4>
<p className='col s12 caption'>
This style can be applied to all button types
</p>
<Col s={12}>
<ReactPlayground code={Samples.disabledButton} />
</Col>
<h4 className='col s12'>
Fixed Action Button
</h4>
<p className='col s12 caption'>
If you want a fixed floating action button, you can add multiple actions that will appear on hover. Our demo is in the bottom righthand corner of the page.
</p>
<Col s={12}>
<ReactPlayground code={Samples.verticalFAB}>
{verticalFAB}
</ReactPlayground>
</Col>
<h4 className='col s12'>
Button as Link
Horizontal FAB
</h4>
<Col s={12}>
<ReactPlayground code={Samples.buttonAsLink}>
{buttonAsLink}
<ReactPlayground code={Samples.horizontalFab}>
{horizontalFab}
</ReactPlayground>
</Col>
<Col s={12}>
Expand Down
3 changes: 2 additions & 1 deletion docs/src/pages/Samples.js
Expand Up @@ -17,7 +17,7 @@ export default {
collectionWithHref: require('!raw-loader!../../../examples/CollectionWithHref.js'),
disabledButton: require('!raw-loader!../../../examples/DisabledButton.js'),
dropdown: require('!raw-loader!../../../examples/DropdownBasic.js'),
fixedActionButton: require('!raw-loader!../../../examples/FixedActionButton.js'),
verticalFAB: require('!raw-loader!../../../examples/VerticalFAB.js'),
flatButton: require('!raw-loader!../../../examples/FlatButton.js'),
floatingButton: require('!raw-loader!../../../examples/FloatingButton.js'),
buttonAsLink: require('!raw-loader!../../../examples/ButtonAsLink.js'),
Expand Down Expand Up @@ -50,6 +50,7 @@ export default {
sideNav: require('!raw-loader!../../../examples/SideNavBasic.js'),
sliders: require('!raw-loader!../../../examples/Sliders.js'),
submitButton: require('!raw-loader!../../../examples/SubmitButton.js'),
smallButton: require('!raw-loader!../../../examples/SmallButton.js'),
tab: require('!raw-loader!../../../examples/Tab.js'),
table: require('!raw-loader!../../../examples/Table.js'),
toast: require('!raw-loader!../../../examples/Toast.js'),
Expand Down
10 changes: 10 additions & 0 deletions examples/SmallButton.js
@@ -0,0 +1,10 @@
import React from 'react';
import Button from '../src/Button';
import Icon from '../src/Icon';

export default
<div>
<Button node='a' waves='light' small>Button</Button>
<Button node='a' waves='light' small>Button<Icon left>cloud</Icon></Button>
<Button node='a' waves='light' small>Button<Icon right>cloud</Icon></Button>
</div>;
2 changes: 1 addition & 1 deletion examples/FixedActionButton.js → examples/VerticalFAB.js
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import Button from '../src/Button';

export default
<Button floating fab='vertical' faicon='fa fa-plus' className='red' large style={{bottom: '45px', right: '24px'}}>
<Button floating fab='vertical' className='red' large style={{bottom: '45px', right: '24px'}} icon='mode_edit'>
<Button floating icon='insert_chart' className='red'/>
<Button floating icon='format_quote' className='yellow darken-1'/>
<Button floating icon='publish' className='green'/>
Expand Down
35 changes: 26 additions & 9 deletions src/Button.js
Expand Up @@ -13,12 +13,21 @@ class Button extends Component {
}

componentDidMount() {
const { tooltip, tooltipOptions } = this.props;
if (
typeof $ !== 'undefined' &&
(typeof tooltip !== 'undefined' || typeof tooltipOptions !== 'undefined')
) {
$(this._btnEl).tooltip(tooltipOptions);
if (!M) return;

const { tooltip, tooltipOptions = {}, fab } = this.props;
if (tooltip) {
this.instance = M.Tooltip.init(this._btnEl, tooltipOptions);
}

if (fab) {
this.instance = M.FloatingActionButton.init(this._floatingActionBtn);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to check for this._floatingActionBtn on L23

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The -10,106 changes are actually from my commit that removes the package.lock. BUT, I may have done something wrong trying to remove the file. @alextrastero, could you help me on this? What is the proper way to remove the file? Sorry about the trouble.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem! You can checkout any file from any branch like so:
git checkout master package-lock.json

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omg it worked 😄

Now I will work on the others tasks:

  • Remove check this._floatingActionBtn on L23
  • Merge the docs of buttons and FloatingActionButtonsPage

:)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)
@alextrastero


componentWillUnmount() {
if (this.instance) {
this.instance.destroy();
}
}

Expand All @@ -32,6 +41,7 @@ class Button extends Component {
flat,
floating,
large,
small,
disabled,
waves,
tooltip,
Expand All @@ -50,9 +60,12 @@ class Button extends Component {
classes['waves-' + waves] = true;
}

let styles = { flat, floating, large };
let styles = { flat, floating, large, small };
constants.STYLES.forEach(style => {
classes['btn-' + style] = styles[style];
if (styles[style]) {
classes.btn = false;
classes['btn-' + style] = true;
}
});

if (modal) {
Expand Down Expand Up @@ -80,7 +93,10 @@ class Button extends Component {
renderFab(className, mode, clickOnly) {
const classes = cx(mode, clickOnly);
return (
<div className={cx('fixed-action-btn', classes)}>
<div
ref={el => (this._floatingActionBtn = el)}
className={cx('fixed-action-btn', classes)}
>
<a className={className}>{this.renderIcon()}</a>
<ul>
{React.Children.map(this.props.children, child => {
Expand Down Expand Up @@ -108,6 +124,7 @@ Button.propTypes = {
*/
flat: PropTypes.bool,
large: PropTypes.bool,
small: PropTypes.bool,
floating: PropTypes.bool,
/**
* Fixed action button
Expand Down
2 changes: 1 addition & 1 deletion src/constants.js
@@ -1,5 +1,5 @@
export default {
STYLES: ['large', 'floating', 'flat'],
STYLES: ['large', 'small', 'floating', 'flat'],
WAVES: ['light', 'red', 'yellow', 'orange', 'purple', 'green', 'teal'],
SIZES: ['s', 'm', 'l', 'xl'],
PLACEMENTS: ['left', 'center', 'right'],
Expand Down
99 changes: 90 additions & 9 deletions test/Button.spec.js
@@ -1,7 +1,7 @@
import React from 'react';
import { shallow, mount } from 'enzyme';
import Button from '../src/Button';
import mocker from './helper/mocker';
import mocker from './helper/new-mocker';

describe('Button', () => {
let wrapper;
Expand Down Expand Up @@ -31,6 +31,11 @@ describe('Button', () => {
expect(wrapper.find('.btn-large')).toHaveLength(1);
});

test('should apply small styles', () => {
wrapper = shallow(<Button small>Stuff</Button>);
expect(wrapper.find('.btn-small')).toHaveLength(1);
});

test('should apply floating styles', () => {
wrapper = shallow(<Button floating>Stuff</Button>);
expect(wrapper.find('.btn-floating')).toHaveLength(1);
Expand Down Expand Up @@ -69,22 +74,98 @@ describe('Button', () => {
});

describe('with a tooltip', () => {
let tooltip = 'Hello';
let tooltipOptions = {
const tooltipInitMock = jest.fn();
const tooltipInstanceDestroyMock = jest.fn();
const tooltipMock = {
init: (el, options) => {
tooltipInitMock(options);
return {
destroy: tooltipInstanceDestroyMock
};
}
};
const restore = mocker('Tooltip', tooltipMock);
const tooltip = 'Hello';
const tooltipOptions = {
position: 'top',
delay: 50
};

const tooltipMock = jest.fn();
mocker('tooltip', tooltipMock);
wrapper = shallow(<Button tooltip={tooltip}>Stuff</Button>);
beforeEach(() => {
tooltipInitMock.mockClear();
});

afterAll(() => {
restore();
});

test('initializes Button with tooltip', () => {
expect(tooltipMock).toHaveBeenCalled();
wrapper = shallow(<Button tooltip={tooltip}>Stuff</Button>);
expect(tooltipInitMock).toHaveBeenCalled();
});

wrapper = shallow(<Button tooltipOptions={tooltipOptions}>Stuff</Button>);
test('initializes Button with tooltip options', () => {
expect(tooltipMock).toHaveBeenCalledWith(tooltipOptions);
wrapper = shallow(
<Button tooltip={tooltip} tooltipOptions={tooltipOptions}>
Stuff
</Button>
);
expect(tooltipInitMock).toHaveBeenCalledWith(tooltipOptions);
});

test('destroyed when unmounted', () => {
wrapper = shallow(<Button tooltip={tooltip}>Stuff</Button>);
wrapper.unmount();
expect(tooltipInstanceDestroyMock).toHaveBeenCalled();
});
});

describe('with fixed action button', () => {
const fabInitMock = jest.fn();
const fabInstanceDestroyMock = jest.fn();
const fabMock = {
init: (el, options) => {
fabInitMock(options);
return {
destroy: fabInstanceDestroyMock
};
}
};
const restore = mocker('FloatingActionButton', fabMock);
const fab = 'vertical';
let wrapper;

beforeEach(() => {
fabInitMock.mockClear();
wrapper = mount(
<Button
floating
fab={fab}
className="red"
large
style={{
bottom: '45px',
right: '24px'
}}
>
<Button floating icon="insert_chart" className="red" />
<Button floating icon="format_quote" className="yellow darken-1" />
<Button floating icon="publish" className="green" />
<Button floating icon="attach_file" className="blue" />
</Button>
);
});

afterAll(() => {
restore();
});

test('initializes FloatingActionButton instance', () => {
expect(fabInitMock).toHaveBeenCalled();
});
test('destroys FloatingActionButton instance when unmounted', () => {
wrapper.unmount();
expect(fabInstanceDestroyMock).toHaveBeenCalled();
});
});
});
6 changes: 3 additions & 3 deletions test/__snapshots__/Modal.spec.js.snap
Expand Up @@ -62,7 +62,7 @@ exports[`<Modal /> renders a modal has children 1`] = `
waves="green"
>
<button
className="btn waves-effect waves-green btn-flat modal-close"
className="waves-effect waves-green btn-flat modal-close"
disabled={false}
>
Close
Expand Down Expand Up @@ -136,7 +136,7 @@ exports[`<Modal /> renders a trigger renders 1`] = `
waves="green"
>
<button
className="btn waves-effect waves-green btn-flat modal-close"
className="waves-effect waves-green btn-flat modal-close"
disabled={false}
>
Close
Expand Down Expand Up @@ -197,7 +197,7 @@ exports[`<Modal /> without a trigger renders 1`] = `
waves="green"
>
<button
className="btn waves-effect waves-green btn-flat modal-close"
className="waves-effect waves-green btn-flat modal-close"
disabled={false}
>
Close
Expand Down