isGrouped = $condition;
return $this;
}
public function isGrouped(): bool
{
return (bool) $this->evaluate($this->isGrouped);
}
public function getPublishedViewOverrideCheckPath(): ?string
{
if ($this->isGrouped()) {
return static::GROUPED_VIEW;
}
return 'filament-forms::components.toggle-buttons.index';
}
public function toEmbeddedHtml(): string
{
if ($this->isGrouped()) {
return $this->toGroupedEmbeddedHtml();
}
$gridDirection = $this->getGridDirection() ?? GridDirection::Column;
$id = $this->getId();
$isDisabled = $this->isDisabled();
$isInline = $this->isInline();
$isMultiple = $this->isMultiple();
$statePath = $this->getStatePath();
$areButtonLabelsHidden = $this->areButtonLabelsHidden();
$wireModelAttribute = $this->applyStateBindingModifiers('wire:model');
$extraInputAttributeBag = $this->getExtraInputAttributeBag()->class(['fi-fo-toggle-buttons-input']);
$isAutofocused = $this->isAutofocused();
$containerAttributes = $this->getExtraAttributeBag();
if (! $isInline) {
$containerAttributes = $containerAttributes->grid($this->getColumns(), $gridDirection);
}
$containerAttributes = $containerAttributes
->merge([
'aria-labelledby' => "{$id}-label",
'role' => $isMultiple ? 'group' : 'radiogroup',
], escape: false)
->class([
'fi-fo-toggle-buttons',
'fi-inline' => $isInline,
]);
$first = true;
ob_start(); ?>
toHtml() ?>>
getOptions() as $value => $label) { ?>
isOptionDisabled($value, $label);
$color = $this->getColor($value) ?? 'primary';
$icon = $this->getIcon($value);
$tooltip = $this->getTooltip($value);
$buttonAttributes = (new FilamentComponentAttributeBag)
->merge([
'aria-disabled' => $shouldOptionBeDisabled ? 'true' : null,
'aria-label' => $areButtonLabelsHidden ? e(trim(strip_tags((string) $label))) : null,
'disabled' => $shouldOptionBeDisabled && blank($tooltip),
'for' => e($inputId),
], escape: false)
->class([
'fi-btn',
'fi-size-md',
'fi-disabled' => $shouldOptionBeDisabled,
])
->color(ButtonComponent::class, $color);
?>
autofocus
disabled
id="= e($inputId) ?>"
name="= e($id) ?>"
type="= $isMultiple ? 'checkbox' : 'radio' ?>"
value="= e($value) ?>"
= $wireModelAttribute ?>="= e($statePath) ?>"
= $extraInputAttributeBag->toHtml() ?>
/>
wrapEmbeddedHtml(ob_get_clean(), extraWrapperAttributes: ['class' => 'fi-fo-toggle-buttons-wrp', 'tabindex' => '-1'], labelTag: 'div');
}
protected function toGroupedEmbeddedHtml(): string
{
$id = $this->getId();
$isDisabled = $this->isDisabled();
$isMultiple = $this->isMultiple();
$statePath = $this->getStatePath();
$areButtonLabelsHidden = $this->areButtonLabelsHidden();
$wireModelAttribute = $this->applyStateBindingModifiers('wire:model');
$extraInputAttributeBag = $this->getExtraInputAttributeBag()->class(['fi-fo-toggle-buttons-input']);
$containerAttributes = $this->getExtraAttributeBag()
->merge([
'aria-labelledby' => "{$id}-label",
'role' => $isMultiple ? 'group' : 'radiogroup',
], escape: false)
->class(['fi-fo-toggle-buttons fi-btn-group']);
ob_start(); ?>
toHtml() ?>>
getOptions() as $value => $label) { ?>
isOptionDisabled($value, $label);
$color = $this->getColor($value) ?? 'primary';
$icon = $this->getIcon($value);
$tooltip = $this->getTooltip($value);
$buttonAttributes = (new FilamentComponentAttributeBag)
->merge([
'aria-disabled' => $shouldOptionBeDisabled ? 'true' : null,
'aria-label' => $areButtonLabelsHidden ? e(trim(strip_tags((string) $label))) : null,
'disabled' => $shouldOptionBeDisabled && blank($tooltip),
'for' => e($inputId),
], escape: false)
->class([
'fi-btn',
'fi-btn-group-btn',
'fi-size-md',
'fi-disabled' => $shouldOptionBeDisabled,
])
->color(ButtonComponent::class, $color);
?>
disabled
id="= e($inputId) ?>"
name="= e($id) ?>"
type="= $isMultiple ? 'checkbox' : 'radio' ?>"
value="= e($value) ?>"
wire:loading.attr="disabled"
= $wireModelAttribute ?>="= e($statePath) ?>"
= $extraInputAttributeBag->toHtml() ?>
/>
wrapEmbeddedHtml(ob_get_clean(), extraWrapperAttributes: ['class' => 'fi-fo-toggle-buttons-wrp', 'tabindex' => '-1'], labelTag: 'div');
}
public function boolean(?string $trueLabel = null, ?string $falseLabel = null): static
{
$this->options([
1 => $trueLabel ?? __('filament-forms::components.toggle_buttons.boolean.true'),
0 => $falseLabel ?? __('filament-forms::components.toggle_buttons.boolean.false'),
]);
$this->colors([
1 => 'success',
0 => 'danger',
]);
$this->icons([
1 => FilamentIcon::resolve(FormsIconAlias::COMPONENTS_TOGGLE_BUTTONS_BOOLEAN_TRUE) ?? Heroicon::Check,
0 => FilamentIcon::resolve(FormsIconAlias::COMPONENTS_TOGGLE_BUTTONS_BOOLEAN_FALSE) ?? Heroicon::XMark,
]);
$this->stateCast(app(BooleanStateCast::class, ['isStoredAsInt' => true]));
return $this;
}
public function inline(bool | Closure $condition = true): static
{
$this->isInline = $condition;
return $this;
}
public function isInline(): bool
{
return (bool) $this->evaluate($this->isInline);
}
public function hiddenButtonLabels(bool | Closure $condition = true): static
{
$this->areButtonLabelsHidden = $condition;
return $this;
}
public function areButtonLabelsHidden(): bool
{
return (bool) $this->evaluate($this->areButtonLabelsHidden);
}
public function multiple(bool | Closure $condition = true): static
{
$this->isMultiple = $condition;
return $this;
}
public function isMultiple(): bool
{
return (bool) $this->evaluate($this->isMultiple);
}
public function getDefaultState(): mixed
{
$state = parent::getDefaultState();
if (is_bool($state)) {
return $state ? 1 : 0;
}
return $state;
}
public function getEnumDefaultStateCast(): ?StateCast
{
$enum = $this->getEnum();
if (blank($enum)) {
return null;
}
return app(
$this->isMultiple() ? EnumArrayStateCast::class : EnumStateCast::class,
['enum' => $enum],
);
}
/**
* @return array
*/
public function getDefaultStateCasts(): array
{
if ($this->hasCustomStateCasts() || filled($this->getEnum())) {
return parent::getDefaultStateCasts();
}
if ($this->isMultiple()) {
return [app(OptionsArrayStateCast::class)];
}
return [app(OptionStateCast::class, ['isNullable' => true])];
}
/**
* @return ?array
*/
public function getInValidationRuleValues(): ?array
{
$values = parent::getInValidationRuleValues();
if ($values !== null) {
return $values;
}
return array_keys($this->getEnabledOptions());
}
public function hasInValidationOnMultipleValues(): bool
{
return $this->isMultiple();
}
public function hasNullableBooleanState(): bool
{
return true;
}
}