Overview
The Component class is the abstract base class for all Javaabu Forms components. It extends Laravel’s Illuminate\View\Component and provides core functionality for framework configuration, ID generation, label generation, and view rendering.
Namespace: Javaabu\Forms\Views\Components
Extends: Illuminate\View\Component
Class Definition
abstract class Component extends BaseComponent
Properties
Protected Properties
ID for this component. Automatically generated if not set.
The view to be used for rendering this component.
Public Properties
Framework used for this component (e.g., ‘bootstrap5’, ‘tailwind’).
Constructor
public function __construct(string $framework = '')
The CSS framework to use. If not provided, defaults to the value from config('forms.framework').
Creates a new component instance and sets the framework property.
Methods
getFrameworkIcon
public function getFrameworkIcon(string $icon): string
The icon name/class to prefix.
Returns a framework-specific icon string by prepending the configured icon prefix.
Returns: string - The complete icon class string
Example:
$component->getFrameworkIcon('check');
// Returns: "fas fa-check" (if using FontAwesome)
frameworkConfig
public function frameworkConfig(string $config, $default = null)
The configuration key to retrieve.
Default value if configuration is not found.
Retrieves a framework-specific configuration value.
Returns: mixed - The configuration value or default
Example:
$component->frameworkConfig('icon-prefix');
// Returns: "fas fa" (for FontAwesome)
getView
public function getView(): string
Returns the view path template with framework placeholder.
Returns: string - View path like forms::{framework}.input
render
Renders the component by replacing the framework placeholder in the view path.
Returns: string - The complete view path
public function id(): string
Generates and returns a unique ID for this component. The ID is generated once and cached.
Returns: string - The component ID
Generation Logic:
- If the component has a
name property, generates ID from the name
- Otherwise, generates a random 4-character string
Example:
// For input with name="user[email]"
$component->id(); // Returns: "user-email"
generateIdByName
protected function generateIdByName(): string
Generates an ID from the component’s name attribute by converting brackets to dashes.
Returns: string - The generated ID
Example:
// For name="settings[notifications][]"
$this->generateIdByName(); // Returns: "settings-notifications"
stringToId
public function stringToId($text): string
The text to convert to an ID.
Converts a string to a valid HTML ID by replacing brackets with dashes.
Returns: string - The converted ID
label
public function label(): string
Generates and returns a human-readable label for this component. The label is generated once and cached.
Returns: string - The component label
Example:
// For input with name="user_email"
$component->label(); // Returns: "User Email"
generateLabelByName
protected function generateLabelByName(): string
Generates a human-readable label from the component’s name attribute.
Returns: string - The generated label
Processing Steps:
- Removes brackets from name
- Converts to camelCase
- Converts to snake_case
- Replaces underscores with spaces
- Converts to Title Case
- Translates the result
Example:
// For name="user_email"
$this->generateLabelByName(); // Returns: "User Email"
convertBracketsToDots
protected static function convertBracketsToDots(string $name): string
The name with bracket notation.
Converts bracket notation to dot notation for array access.
Returns: string - The converted name
Example:
Component::convertBracketsToDots('user[profile][email]');
// Returns: "user.profile.email"
Usage Example
use Javaabu\Forms\Views\Components\Component;
class CustomInput extends Component
{
protected string $view = 'custom-input';
public string $name;
public string $label;
public function __construct(
string $name,
string $label = '',
string $framework = ''
) {
parent::__construct($framework);
$this->name = $name;
$this->label = $label;
}
public function render()
{
$id = $this->id();
$label = $this->label();
return parent::render();
}
}