UNPKG

@gravityforms/components

Version:

UI components for use in Gravity Forms development. Both React and vanilla js flavors.

229 lines (212 loc) 7.56 kB
import { consoleInfo, getNode, objectToAttributes, spacerClasses, trigger, uniqueId, } from '@gravityforms/utils'; /** * @function statusIndicatorTemplate * @description Generates the markup for a statusIndicator in the admin. * * @since 1.1.16 * * @param {object} options The options for the component template. * @param {object} options.customAttributes Any custom attributes. * @param {Array} options.customClasses An array of additional classes for the toggle. * @param {boolean} options.hasDot Whether or not to include the svg dot. * @param {string} options.id Id for the statusIndicator, auto generated if not passed. * @param {boolean} options.isStatic Whether or not the indicator should be active (`button`) or static (`span`). * @param {string} options.label Status indicator label. * @param {string} options.size The size of the component. * @param {boolean} options.pill Whether or not to apply fully rounded "pill" css to the indicator. * @param {string|number|Array|object} options.spacing The spacing for the component, string, number, object or array. * @param {string} options.status Type of indicator: `active`, `inactive`, or `error`. * @param {string} options.theme Theme for the toggle, primary or cosmos. * * @return {string} * @example * import { statusIndicatorTemplate } from '@gravityforms/components/html/admin/elements/StatusIndicator'; * * function Example() { * const statusIndicatorTemplateHTML = statusIndicatorTemplateTemplate( options ); * document.body.insertAdjacentHTML( 'beforeend', statusIndicatorTemplateHTML ); * } * */ export const statusIndicatorTemplate = ( { customAttributes = {}, customClasses = [], hasDot = true, id = '', isStatic = false, label = '', pill = true, size = 'sm', spacing = '', status = 'active', theme = 'cosmos', } ) => { const tag = isStatic ? 'span' : 'button'; const componentAttrs = objectToAttributes( { ...customAttributes, id, class: [ 'gform-status-indicator', `gform-status-indicator--size-${ size }`, `gform-status-indicator--theme-${ theme }`, `gform-status--${ status }`, ! pill ? 'gform-status--no-pill' : '', ! hasDot ? 'gform-status--no-icon' : '', isStatic ? 'gform-status--no-hover' : '', ...Object.keys( spacerClasses( spacing ) ), ...customClasses, ], } ); const textSizeTypographyClassMap = { sm: 'gform-typography--size-text-xs', md: 'gform-typography--size-text-sm', lg: 'gform-typography--size-text-sm', }; const textAttrs = objectToAttributes( { class: [ 'gform-status-indicator-status', 'gform-typography--weight-medium', textSizeTypographyClassMap[ size ], ], } ); return ` <${ tag } ${ componentAttrs }> <span ${ textAttrs }>${ label }</span> </${ tag }> `; }; /** * @class StatusIndicator * @description A statusIndicator component . * * @since 1.1.16 * * @borrows statusIndicatorTemplate as statusIndicatorTemplate * * @param {object} options The options for the component. * @param {object} options.customAttributes Any custom attributes for the component. * @param {Array} options.customClasses An array of additional classes for the component. * @param {boolean} options.hasDot Whether or not to include the svg dot. * @param {string} options.id Id for the component, auto generated if not passed. * @param {boolean} options.isStatic Whether or not the indicator should be active (`button`) or static (`span`). * @param {string} options.label Status indicator label. * @param {boolean} options.pill Whether or not to apply fully rounded "pill" css to the indicator. * @param {string} options.rendered Is the component already rendered in the dom, eg by php? * @param {string} options.renderOnInit Render the component on init of the class? * @param {string} options.size Size of the component. * @param {string} options.spacing Spacing for the component. * @param {string} options.status Type of indicator: `active`, `inactive`, or `error`. * @param {string} options.target The target to render to. Any valid css selector string. * @param {string} options.targetPosition The insert position for the component relative to the target. * @param {string} options.theme Theme for the component, primary or cosmos. * * @return {Class} The class instance. * @example * import StatusIndicator from '@gravityforms/components/html/admin/elements/StatusIndicator'; * * function Example() { * const statusIndicatorInstance = new StatusIndicator( { * id: 'example-statusIndicator', * renderOnInit: false, * target: '#example-target', * targetPosition: 'beforeend', * theme: 'cosmos', * } ); * * // Some time later we can render it. This is only done if we set renderOnInit to false. * // If true it will render on initialization. * statusIndicatorInstance.init(); * } * */ export default class StatusIndicator { constructor( options = {} ) { this.options = {}; Object.assign( this.options, { customAttributes: {}, customClasses: [], hasDot: true, id: '', isStatic: false, label: '', pill: true, rendered: false, renderOnInit: true, size: 'sm', spacing: '', status: 'active', target: '', targetPosition: 'afterbegin', theme: 'cosmos', }, options ); /** * @event gform/status_indicator/pre_init * @type {object} * @description Fired before the component has started any internal init functions. A great chance to augment the options. * * @since 1.1.16 * * @property {object} instance The Component class instance. */ trigger( { event: 'gform/status_indicator/pre_init', native: false, data: { instance: this } } ); this.options.id = this.options.id || uniqueId( 'toggle' ); this.elements = {}; if ( this.options.renderOnInit ) { this.init(); } } /** * @memberof StatusIndicator * @description Renders the component into the dom. * * @since 1.1.16 * * @return {void} */ render() { const { rendered, target, targetPosition } = this.options; if ( ! rendered ) { const renderTarget = getNode( target, document, true ); renderTarget.insertAdjacentHTML( targetPosition, statusIndicatorTemplate( this.options ) ); } this.elements.statusIndicator = getNode( `#${ this.options.id }`, document, true ); } /** * @memberof StatusIndicator * @description Initialize the component. * * @fires gform/status_indicator/post_render * * @since 1.1.16 * * @return {void} */ init() { this.render(); /** * @event gform/status_indicator/post_render * @type {object} * @description Fired when the component has completed rendering and all class init functions have completed. * * @since 1.1.16 * * @property {object} instance The Component class instance. */ trigger( { event: 'gform/status_indicator/post_render', native: false, data: { instance: this } } ); consoleInfo( `Gravity Forms Admin: Initialized statusIndicator component.` ); } }