react-chessground
Version:
React warp for chessground <https://github.com/ornicar/chessground>
118 lines (97 loc) • 3.52 kB
JavaScript
function _extends() { _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; }; return _extends.apply(this, arguments); }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import React from 'react';
import PropTypes from 'prop-types';
import { Chessground as NativeChessground } from 'chessground';
var Chessground =
/*#__PURE__*/
function (_React$Component) {
_inheritsLoose(Chessground, _React$Component);
function Chessground() {
return _React$Component.apply(this, arguments) || this;
}
var _proto = Chessground.prototype;
_proto.buildConfigFromProps = function buildConfigFromProps(props) {
var config = {
events: {}
};
Object.keys(Chessground.propTypes).forEach(function (k) {
var v = props[k];
if (typeof v !== 'undefined') {
var match = k.match(/^on([A-Z]\S*)/);
if (match) {
config.events[match[1].toLowerCase()] = v;
} else {
config[k] = v;
}
}
});
return config;
};
_proto.componentDidMount = function componentDidMount() {
this.cg = NativeChessground(this.el, this.buildConfigFromProps(this.props));
};
_proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
this.cg.set(this.buildConfigFromProps(nextProps));
};
_proto.componentWillUnmount = function componentWillUnmount() {
this.cg.destroy();
};
_proto.render = function render() {
var _this = this;
var props = {
style: _extends({}, this.props.style)
};
if (this.props.width) {
props.style.width = this.props.width;
}
if (this.props.height) {
props.style.height = this.props.height;
}
return React.createElement("div", _extends({
ref: function ref(el) {
return _this.el = el;
}
}, props));
};
return Chessground;
}(React.Component);
_defineProperty(Chessground, "propTypes", {
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
fen: PropTypes.string,
orientation: PropTypes.string,
turnColor: PropTypes.string,
check: PropTypes.string,
lastMove: PropTypes.array,
selected: PropTypes.string,
coordinates: PropTypes.bool,
autoCastle: PropTypes.bool,
viewOnly: PropTypes.bool,
disableContextMenu: PropTypes.bool,
resizable: PropTypes.bool,
addPieceZIndex: PropTypes.bool,
highlight: PropTypes.object,
animation: PropTypes.object,
movable: PropTypes.object,
premovable: PropTypes.object,
predroppable: PropTypes.object,
draggable: PropTypes.object,
selectable: PropTypes.object,
onChange: PropTypes.func,
onMove: PropTypes.func,
onDropNewPiece: PropTypes.func,
onSelect: PropTypes.func,
items: PropTypes.object,
drawable: PropTypes.object
});
_defineProperty(Chessground, "defaultProps", {
coordinates: true,
resizable: true,
highlight: {
lastMove: true,
check: true
}
});
export { Chessground as default };