Toggle

new Toggle(options) → {Class}

A toggle component to use wherever boolean based form fields are needed.

Parameters:
NameTypeDescription
optionsobject

The options for the component.

Properties
NameTypeDescription
customAttributesobject

Any custom attributes for the component.

customClassesArray

An array of additional classes for the component.

disabledArray

Is the toggle disabled on render?

iconsboolean

Do we use the checkmark and x icons for this instance?

idstring

Id for the component, auto generated if not passed.

initialCheckedboolean

Is it checked on render?

labelstring

The label, required for accessibility.

labelPositionstring

Position for the label, defaults to right.

labelVisibleboolean

Is the label visible?

renderedstring

Is the component already rendered in the dom, eg by php?

renderOnInitstring

Render the component on init of the class?

namestring

Name for the input.

sizestring

Size for the component. Small (size-s), medium (size-m), or large (size-l).

spacingstring

Spacing for the component.

targetstring

The target to render to. Any valid css selector string.

targetPositionstring

The insert position for the component relative to the target.

themestring

Theme for the component, primary or cosmos.

Since
  • 1.1.16
Returns:

The class instance.

Type: 
Class
Example
import Toggle from '@gravityforms/components/html/admin/elements/Toggle';

function Example() {
     const toggleInstance = new Toggle( {
         id: 'example-toggle',
         label: 'Example Toggle',
         name: 'example-toggle',
         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.
     toggleInstance.init();
}

Methods

(static) init() → {void}

Initialize the component.

Since
  • 1.1.16
Returns:
Type: 
void

(static) render() → {void}

Renders the component into the dom.

Since
  • 1.1.16
Returns:
Type: 
void

(static) toggleTemplate(options) → {string}

Generates the markup for a toggle in the admin.

Parameters:
NameTypeDescription
optionsobject

The options for the component template.

Properties
NameTypeDescription
customAttributesobject

Any custom attributes.

customClassesArray

An array of additional classes for the toggle.

disabledboolean

If the toggle is disabled.

iconsboolean

Do we use the checkmark and x icons for this instance.

iconPrefixstring

Prefix for the icon class.

iconBeforestring

Icon to display when toggle is off.

iconAfterstring

Icon to display when toggle is on.

idstring

Id for the toggle, auto generated if not passed.

initialCheckedboolean

Is it checked on render?

labelstring

The label, required for accessibility.

labelPositionstring

Position for the label, defaults to right.

labelVisibleboolean

Is the label visible?

namestring

Name for the input.

sizestring

Size for the toggle. Small (size-s), medium (size-m), or large (size-l).

spacingstring | number | Array | object

The spacing for the component, string, number, object or array.

themestring

Theme for the toggle, primary or cosmos.

ariaDescribedbystring

Class of the element that describes this input.

Since
  • 1.1.16
Returns:
Type: 
string
Example
import { toggleTemplate } from '@gravityforms/components/html/admin/elements/Toggle';

function Example() {
     const toggleHTML = toggleTemplate( options );
     document.body.insertAdjacentHTML( 'beforeend', toggleHTML );
}