components/Common/Link.jsx

/**
 * Компонент ссылки
 * @module Link
 * @author Ihor Bielchenko
 * @requires react
 * @requires react#Component
 * @requires Base.js
 */

import React, { Component } from 'react';
import Base from '../../Base.js';

/**
 * Компонент ссылки
 * @extends Component
 */
class Link extends Component {

	/**
	 * Инициализация параметров по умолчанию
	 * @type {Object} 
	 * @inner
	 * @property {Function} onClick Функция, вызывающаяся при клике на ссылку 
	 */
	static defaultProps = {
		onClick: function() {}
	}

	/**
	 * Клик по ссылке и обновление адресной строки
	 * @fires click
	 * @param {Object} e
	 */
	click(e) {
		e.preventDefault();

		if(typeof this.props.href !== 'undefined') {
			Base.changeURL(this.props.href);
		}

		this.props.onClick();
	}

	/**
	 * Render component
	 * @return {Object} jsx object
	 */
	render() {

		var className = 'im-link ';
		if(typeof this.props.className !== 'undefined') {
			className += this.props.className;
		}

		if(typeof this.props.href === 'undefined') {
			return <span className={className}
						onClick={this.click.bind(this)}>
							{typeof this.props.children !== 'undefined' ? 
								this.props.children : 
								''}
					</span>			
		}

		else return <a href={this.props.href} 
						className={className}
						onClick={this.click.bind(this)}>
							{typeof this.props.children !== 'undefined' ? 
								this.props.children : 
								''}
					</a>
	}
}

export default Link;