|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @link https://craftcms.com/ |
| 4 | + * @copyright Copyright (c) Pixel & Tonic, Inc. |
| 5 | + * @license https://craftcms.github.io/license/ |
| 6 | + */ |
| 7 | + |
| 8 | +namespace craft\fields; |
| 9 | + |
| 10 | +use Craft; |
| 11 | +use craft\base\ElementInterface; |
| 12 | +use craft\base\SortableFieldInterface; |
| 13 | +use craft\fields\data\SingleOptionFieldData; |
| 14 | +use craft\helpers\Cp; |
| 15 | +use craft\helpers\Html; |
| 16 | + |
| 17 | +/** |
| 18 | + * RadioButtons represents a Radio Buttons field. |
| 19 | + * |
| 20 | + * @author Pixel & Tonic, Inc. <[email protected]> |
| 21 | + * @since 3.0.0 |
| 22 | + */ |
| 23 | +class ButtonGroup extends BaseOptionsField implements SortableFieldInterface |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @inheritdoc |
| 27 | + */ |
| 28 | + protected static bool $optionIcons = true; |
| 29 | + |
| 30 | + /** |
| 31 | + * @inheritdoc |
| 32 | + */ |
| 33 | + public static function displayName(): string |
| 34 | + { |
| 35 | + return Craft::t('app', 'Button Group'); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @inheritdoc |
| 40 | + */ |
| 41 | + public static function icon(): string |
| 42 | + { |
| 43 | + return 'hand-pointer'; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @var bool Whether buttons should only show their icons, hiding their text labels |
| 48 | + */ |
| 49 | + public bool $iconsOnly = false; |
| 50 | + |
| 51 | + /** |
| 52 | + * @inheritdoc |
| 53 | + */ |
| 54 | + public function getSettingsHtml(): ?string |
| 55 | + { |
| 56 | + return parent::getSettingsHtml() . |
| 57 | + Cp::lightswitchFieldHtml([ |
| 58 | + 'label' => Craft::t('app', 'Icons only'), |
| 59 | + 'instructions' => Craft::t('app', 'Whether buttons should only show their icons, hiding their text labels.'), |
| 60 | + 'name' => 'iconsOnly', |
| 61 | + 'on' => $this->iconsOnly, |
| 62 | + ]); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @inheritdoc |
| 67 | + */ |
| 68 | + public function useFieldset(): bool |
| 69 | + { |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @inheritdoc |
| 75 | + */ |
| 76 | + protected function inputHtml(mixed $value, ?ElementInterface $element, bool $inline): string |
| 77 | + { |
| 78 | + return $this->_inputHtml($value, $element, false); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @inheritdoc |
| 83 | + */ |
| 84 | + public function getStaticHtml(mixed $value, ElementInterface $element): string |
| 85 | + { |
| 86 | + return $this->_inputHtml($value, $element, true); |
| 87 | + } |
| 88 | + |
| 89 | + private function _inputHtml(SingleOptionFieldData $value, ?ElementInterface $element, bool $static): string |
| 90 | + { |
| 91 | + /** @var SingleOptionFieldData $value */ |
| 92 | + if (!$value->valid) { |
| 93 | + Craft::$app->getView()->setInitialDeltaValue($this->handle, null); |
| 94 | + } |
| 95 | + |
| 96 | + $id = $this->getInputId(); |
| 97 | + |
| 98 | + $html = Html::beginTag('div', ['class' => 'btngroup-container']) . |
| 99 | + Html::beginTag('div', [ |
| 100 | + 'id' => $id, |
| 101 | + 'class' => ['btngroup', 'btngroup--exclusive'], |
| 102 | + 'aria' => [ |
| 103 | + 'labelledby' => $this->getLabelId(), |
| 104 | + ], |
| 105 | + ]); |
| 106 | + |
| 107 | + $value = $this->encodeValue($value); |
| 108 | + |
| 109 | + foreach ($this->translatedOptions(true, $value, $element) as $option) { |
| 110 | + $selected = $option['value'] === $value; |
| 111 | + |
| 112 | + if ($this->iconsOnly && !empty($option['icon'])) { |
| 113 | + $labelHtml = Html::tag('div', Cp::iconSvg($option['icon']), [ |
| 114 | + 'class' => 'cp-icon', |
| 115 | + 'aria' => [ |
| 116 | + 'label' => $option['label'], |
| 117 | + ], |
| 118 | + ]); |
| 119 | + } else { |
| 120 | + $labelHtml = Html::encode($option['label']); |
| 121 | + if (!empty($option['icon'])) { |
| 122 | + $labelHtml = Html::beginTag('div', ['class' => ['flex', 'flex-inline', 'gap-xs']]) . |
| 123 | + Html::tag('div', Cp::iconSvg($option['icon']), [ |
| 124 | + 'class' => 'cp-icon', |
| 125 | + ]) . |
| 126 | + Html::tag('div', $labelHtml) . |
| 127 | + Html::endTag('div'); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + $html .= Cp::buttonHtml([ |
| 132 | + 'labelHtml' => $labelHtml, |
| 133 | + 'type' => 'button', |
| 134 | + 'class' => array_filter([ |
| 135 | + $selected ? 'active' : null, |
| 136 | + ]), |
| 137 | + 'disabled' => $static, |
| 138 | + 'attributes' => [ |
| 139 | + 'aria' => [ |
| 140 | + 'pressed' => $selected ? 'true' : 'false', |
| 141 | + ], |
| 142 | + 'data' => [ |
| 143 | + 'value' => $option['value'], |
| 144 | + ], |
| 145 | + ], |
| 146 | + ]); |
| 147 | + } |
| 148 | + |
| 149 | + $html .= Html::endTag('div') . // .btngroup |
| 150 | + Html::endTag('div') . // .btngroup-container |
| 151 | + Html::hiddenInput($this->handle, $value, [ |
| 152 | + 'id' => "{$id}-input", |
| 153 | + ]); |
| 154 | + |
| 155 | + $view = Craft::$app->getView(); |
| 156 | + $view->registerJsWithVars(fn($id) => <<<JS |
| 157 | +(() => { |
| 158 | + new Craft.Listbox($('#' + $id), { |
| 159 | + onChange: (selectedOption) => { |
| 160 | + $('#' + $id + '-input').val(selectedOption.data('value')); |
| 161 | + }, |
| 162 | + }); |
| 163 | +})(); |
| 164 | +JS, [ |
| 165 | + $view->namespaceInputId($id), |
| 166 | + ]); |
| 167 | + |
| 168 | + return $html; |
| 169 | + } |
| 170 | +} |
0 commit comments