Skip to content

Commit

Permalink
Added a way to put HTML e.g. "Use <strong>keyword</strong> now" into …
Browse files Browse the repository at this point in the history
…a button. (bokuweb#79)
  • Loading branch information
MartinWillitts authored and bokuweb committed Apr 27, 2017
1 parent 4ddcd88 commit 1ec2823
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 7 deletions.
1 change: 1 addition & 0 deletions example/src/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export default class Example extends Component {
<Button color="isPrimary">Primary</Button>
<Button color="isPrimary" state="isActive">Primary Active</Button>
<Button state="isDisabled">Disabled</Button>
<Button color="isLink" safeHtml="The <strong>bold</strong> word"></Button>
<Columns>
<Column size="is2" style={{ background: '#ccc' }}>aaaa</Column>
<Column>bbbb</Column>
Expand Down
140 changes: 140 additions & 0 deletions lib/elements/button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _styles = require('../../build/styles');

var _styles2 = _interopRequireDefault(_styles);

var _helper = require('../helper/helper');

var _dataComponent = require('../dataComponent');

var _dataComponent2 = _interopRequireDefault(_dataComponent);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var Button = function (_DataComponent) {
_inherits(Button, _DataComponent);

function Button() {
_classCallCheck(this, Button);

return _possibleConstructorReturn(this, (Button.__proto__ || Object.getPrototypeOf(Button)).apply(this, arguments));
}

_createClass(Button, [{
key: 'createClassName',
value: function createClassName() {
return [_styles2.default.button, _styles2.default[this.props.size], _styles2.default[this.props.color], _styles2.default[this.props.buttonStyle], _styles2.default[this.props.state], this.props.delete ? _styles2.default.delete : '', this.props.className].join(' ').trim();
}
}, {
key: 'createIconSize',
value: function createIconSize() {
if (this.props.size === 'isLarge') return 'isMedium';
if (this.props.size === 'isSmall') return 'isSmall';
return '';
}
}, {
key: 'renderLeftIcon',
value: function renderLeftIcon() {
return _react2.default.createElement(
'span',
null,
_react2.default.createElement(
'span',
{ className: [_styles2.default.icon, _styles2.default[this.createIconSize()]].join(' ') },
_react2.default.createElement('i', { className: [_styles2.default.fa, this.props.icon].join(' ') })
),
_react2.default.createElement(
'span',
{ style: { lineHeight: this.props.size === 'isLarge' ? '32px' : 'auto' } },
this.props.children
)
);
}
}, {
key: 'renderRightIcon',
value: function renderRightIcon() {
return _react2.default.createElement(
'span',
null,
_react2.default.createElement(
'span',
{ style: { lineHeight: this.props.size === 'isLarge' ? '32px' : 'auto' } },
this.props.children
),
_react2.default.createElement(
'span',
{ className: [_styles2.default.icon, _styles2.default[this.createIconSize()]].join(' ') },
_react2.default.createElement('i', { className: [_styles2.default.fa, this.props.icon].join(' ') })
)
);
}
}, {
key: 'renderIcon',
value: function renderIcon() {
return this.props.isIconRight ? this.renderRightIcon() : this.renderLeftIcon();
}
}, {
key: 'renderBody',
value: function renderBody() {
return this.props.safeHtml ? _react2.default.createElement('span', { dangerouslySetInnerHTML: { __html: this.props.safeHtml } }) : _react2.default.createElement(
'span',
null,
this.props.children
);
}
}, {
key: 'render',
value: function render() {
return _react2.default.createElement(
'button',
_extends({}, (0, _helper.getCallbacks)(this.props), {
style: this.props.style,
className: this.createClassName()
}, this.dataProps()),
this.props.icon ? this.renderIcon() : this.renderBody()
);
}
}]);

return Button;
}(_dataComponent2.default);

Button.propTypes = {
children: _react.PropTypes.string,
className: _react.PropTypes.string,
icon: _react.PropTypes.string,
type: _react.PropTypes.string,
style: _react.PropTypes.object,
size: _react.PropTypes.oneOf(['isSmall', 'isMedium', 'isLarge']),
color: _react.PropTypes.oneOf(['isPrimary', 'isInfo', 'isSuccess', 'isWarning', 'isDanger', 'isLink', 'isWhite', 'isLight', 'isDark', 'isBlack', 'isLink']),
buttonStyle: _react.PropTypes.oneOf(['isOutlined', 'isInverted']),
state: _react.PropTypes.oneOf(['isLoading', 'isActive', 'isDisabled']),
delete: _react.PropTypes.bool,
isIconRight: _react.PropTypes.bool,
data: _react.PropTypes.array,
safeHtml: _react.PropTypes.string
};
Button.defaultProps = {
style: {},
className: ''
};
exports.default = Button;
15 changes: 8 additions & 7 deletions src/elements/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default class Button extends DataComponent {
delete: PropTypes.bool,
isIconRight: PropTypes.bool,
data: PropTypes.array,
safeHtml: PropTypes.string,
};

static defaultProps = {
Expand Down Expand Up @@ -97,6 +98,12 @@ export default class Button extends DataComponent {
: this.renderLeftIcon();
}

renderBody() {
return this.props.safeHtml
? (<span dangerouslySetInnerHTML={{ __html: this.props.safeHtml }}></span>)
: (<span>{this.props.children}</span>);
}

render() {
return (
<button
Expand All @@ -105,13 +112,7 @@ export default class Button extends DataComponent {
className={this.createClassName()}
{...this.dataProps()}
>
<span>
{
this.props.icon
? this.renderIcon()
: this.props.children
}
</span>
{this.props.icon ? this.renderIcon() : this.renderBody()}
</button>
);
}
Expand Down

0 comments on commit 1ec2823

Please sign in to comment.