elements_Text_index.js

import { React, PropTypes, classnames } from '@gravityforms/libraries';
import { spacerClasses } from '@gravityforms/utils';

const { forwardRef } = React;

/**
 * @module Text
 * @description Wraps html text with some preset style options in a configurable tag container.
 *
 * @since 1.1.15
 *
 * @param {object}                     props                  Component props.
 * @param {boolean}                    props.asHtml           Whether or not to accept HTML in the content
 * @param {JSX.Element}                props.children         React element children.
 * @param {string}                     props.color            The text color.
 * @param {string}                     props.content          The text content.
 * @param {object}                     props.customAttributes Custom attributes for the component.
 * @param {string|Array|object}        props.customClasses    Custom classes for the component.
 * @param {string}                     props.size             The font size for the heading.
 * @param {string|number|Array|object} props.spacing          The spacing for the component, as a string, number, array, or object.
 * @param {string}                     props.tagName          The tag used for the container element.
 * @param {string}                     props.weight           The font weight for the heading.
 * @param {object|null}                ref                    Ref to the component.
 *
 * @return {JSX.Element} The text component.
 *
 * @example
 * import Text from '@gravityforms/components/react/admin/elements/Text';
 *
 * return <Text content="Hello world" />;
 *
 */
const Text = forwardRef( ( {
	asHtml = false,
	children = null,
	color = 'port',
	content = '',
	customAttributes = {},
	customClasses = [],
	size = 'text-md',
	spacing = '',
	tagName = 'div',
	weight = 'regular',
}, ref ) => {
	const componentProps = {
		className: classnames( {
			'gform-text': true,
			[ `gform-text--color-${ color }` ]: true,
			[ `gform-typography--size-${ size }` ]: true,
			[ `gform-typography--weight-${ weight }` ]: true,
			...spacerClasses( spacing ),
		}, customClasses ),
		ref,
		...customAttributes,
	};

	if ( asHtml ) {
		componentProps.dangerouslySetInnerHTML = { __html: content };
	}

	const Container = tagName;

	return asHtml ? <Container { ...componentProps } /> : (
		<Container { ...componentProps }>
			{ content }
			{ children }
		</Container>
	);
} );

Text.propTypes = {
	asHtml: PropTypes.bool,
	children: PropTypes.oneOfType( [
		PropTypes.arrayOf( PropTypes.node ),
		PropTypes.node,
	] ),
	content: PropTypes.string,
	customAttributes: PropTypes.object,
	customClasses: PropTypes.oneOfType( [
		PropTypes.string,
		PropTypes.array,
		PropTypes.object,
	] ),
	size: PropTypes.string,
	spacing: PropTypes.oneOfType( [
		PropTypes.string,
		PropTypes.number,
		PropTypes.array,
		PropTypes.object,
	] ),
	tagName: PropTypes.string,
	weight: PropTypes.string,
};

Text.displayName = 'Text';

export default Text;