Skip to content

Checkbox

Kyle Angelo Galendez edited this page Jun 13, 2024 · 2 revisions

Checkbox

This document details the Checkbox component, which provides functionality for creating and managing customizable checkboxes in a web application.

Functionality

Checkboxes are interactive elements that allow users to select or deselect options. They are commonly used in forms and settings interfaces to toggle states.

Properties

The Checkbox component accepts a configuration object (config) to customize the checkbox's behavior and appearance.

Configuration Parameters

The config object can include the following properties:

Parameter Type Description Optional
id string ID attribute for the checkbox element No
class_name string CSS class(es) for styling the checkbox Yes
target_id string ID of the target element affected by the checkbox state No
group_name string Group name for managing related checkboxes No

Events

The Checkbox component manages a click event to toggle its state and update related elements.

Usage Example

import { Checkbox } from './path/to/Checkbox.js';

const config = {
  id: 'myCheckbox',
  class_name: 'custom-checkbox',
  target_id: 'targetElement',
  group_name: 'checkboxGroup'
};

const myCheckbox = new Checkbox(config).create();
document.body.appendChild(myCheckbox);

Managing Target IDs

When managing the target IDs of checked checkboxes, the Checkbox component stores this information in localStorage. It uses the group name with a suffix of -checkboxes to store and retrieve these IDs. Here's an example of how to retrieve the target IDs:

// If the group_name in the config is 'checkboxGroup', retrieve it as 'checkboxGroup-checkboxes' from localStorage.
const ids = JSON.parse(localStorage.getItem('checkboxGroup-checkboxes')) || [];

console.log(ids) // Use the retrieved ids

Methods

  • create(): Creates the checkbox element based on the provided configuration and attaches event listeners.
  • remove(): Removes the checkbox element from the DOM and cleans up related state management.

Back to Components

Clone this wiki locally