@@ -12,35 +12,91 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1212import React from 'react' ;
1313const classNames = require ( 'classnames' ) ;
1414import createChainedFunction from '../utils/create-chained-function' ;
15+ import { ButtonIcon , Icon } from '../SLDSIcons.js' ;
1516import { omit } from 'lodash' ;
1617
1718class Button extends React . Component {
1819
1920 constructor ( props ) {
2021 super ( props ) ;
21- this . state = { } ;
22+ this . state = { active : false } ;
2223 } ;
2324
25+ componentWillMount ( ) {
26+ /*===============================
27+ TODO: refactor so that React doesn't throw warnings in console
28+ for (var key in this.props) {
29+ if(this.props.hasOwnProperty(key) && typeof(this.props[key]) === 'string' && key !== 'label'){
30+ this.props[key] = this.props[key].toLowerCase();
31+ }
32+ }
33+ ===============================*/
34+ }
35+
2436 onClick ( e ) {
2537 this . setState ( { active : ! this . state . active } ) ;
2638 }
2739
2840 getClassName ( ) {
41+ let isStateful = this . props . stateful && this . state . active ? true : false ;
2942 return classNames ( this . props . className , 'slds-button' , {
30- [ `slds-button--${ this . props . flavor } ` ] : this . props . flavor ,
31- [ 'slds-is-selected' ] : this . state . active ,
43+ [ `slds-button--${ this . props . variant } ` ] : this . props . variant ,
44+ [ 'slds-is-selected' ] : isStateful ,
3245 } ) ;
3346 }
3447
48+ renderIcon ( ) {
49+ if ( this . props . iconName ) {
50+ return (
51+ < ButtonIcon
52+ name = { this . props . iconName }
53+ size = { this . props . iconSize }
54+ position = { this . props . iconPosition || 'left' } />
55+ ) ;
56+ }
57+ }
58+
59+
3560 render ( ) {
3661 const props = omit ( 'className' , this . props ) ;
3762 const click = createChainedFunction ( this . props . onClick , this . onClick . bind ( this ) ) ;
38- return (
39- < button className = { this . getClassName ( ) } { ...props } onClick = { click } >
40- { this . props . children }
41- </ button >
42- ) ;
63+
64+ //If the button is only an icon render this:
65+ if ( this . props . variant === 'icon' ) {
66+ return (
67+ < button className = { this . getClassName ( ) } { ...props } onClick = { click } >
68+ < Icon
69+ name = { this . props . iconName }
70+ category = 'utility'
71+ size = { this . props . iconSize }
72+ className = { 'slds-icon' } />
73+ < span className = "slds-assistive-text" > { this . props . label } </ span >
74+ </ button >
75+ ) ;
76+ }
77+ //Else we assume the button has a visible label (with or without an icon):
78+ else {
79+ return (
80+ < button className = { this . getClassName ( ) } { ...props } onClick = { click } >
81+
82+ < span aria-live = "assertive" > { this . props . iconPosition === 'right' ? this . props . label : null } </ span >
83+
84+ { this . renderIcon ( ) }
85+
86+ < span aria-live = "assertive" > { ( this . props . iconPosition === 'left' || ! this . props . iconPosition ) ? this . props . label : null } </ span >
87+
88+ </ button >
89+ ) ;
90+ }
4391 }
4492}
4593
94+ Button . propTypes = {
95+ label : React . PropTypes . string . isRequired ,
96+ variant : React . PropTypes . oneOf ( [ 'base' , 'neutral' , 'brand' , 'icon' ] ) ,
97+ iconName : React . PropTypes . string ,
98+ iconSize : React . PropTypes . oneOf ( [ 'x-small' , 'small' , 'medium' , 'large' ] ) ,
99+ iconPosition : React . PropTypes . oneOf ( [ 'left' , 'right' ] ) ,
100+ }
101+
46102module . exports = Button ;
0 commit comments