♻️ rework of Select UIBlock with labels

This commit is contained in:
Eric
2021-06-04 11:52:13 +02:00
parent 3d338aff33
commit 609d9c7a60
15 changed files with 416 additions and 81 deletions

View File

@@ -8,21 +8,22 @@
namespace Combodo\iTop\Application\UI\Base\Component\Input\Select;
use Combodo\iTop\Application\UI\Base\Component\Input\tInputLabel;
use Combodo\iTop\Application\UI\Base\Layout\UIContentBlock;
class Select extends UIContentBlock
{
use tInputLabel;
// Overloaded constants
public const BLOCK_CODE = 'ibo-select';
public const DEFAULT_HTML_TEMPLATE_REL_PATH = 'base/components/input/select/select';
/** @var string */
/** @var string Input name for the form */
protected $sName;
/** @var string */
protected $sValue;
/** @var bool */
/** @var bool if true submit the form as soon as a change is detected */
protected $bSubmitOnChange = false;
/** @var bool */
/** @var bool Allow multiple selection */
protected $bIsMultiple = false;
@@ -32,6 +33,9 @@ class Select extends UIContentBlock
$this->bIsMultiple = false;
}
/**
* @param SelectOption $oOption Select option UIBlock
*/
public function AddOption(SelectOption $oOption)
{
$this->AddSubBlock($oOption);
@@ -43,7 +47,7 @@ class Select extends UIContentBlock
}
/**
* @param string $sName
* @param string $sName {@see Select::$sName}
*
* @return $this
*/
@@ -54,25 +58,8 @@ class Select extends UIContentBlock
return $this;
}
public function GetValue(): ?string
{
return $this->sValue;
}
/**
* @param string|null $sValue
*
* @return $this
*/
public function SetValue(?string $sValue)
{
$this->sValue = $sValue;
return $this;
}
/**
* @return bool
* @return bool {@see Select::$bSubmitOnChange}
*/
public function GetSubmitOnChange(): bool
{
@@ -80,7 +67,7 @@ class Select extends UIContentBlock
}
/**
* @param bool $bSubmitOnChange
* @param bool $bSubmitOnChange {@see Select::$bSubmitOnChange}
*
* @return $this
*/
@@ -92,7 +79,7 @@ class Select extends UIContentBlock
}
/**
* @return bool
* @return bool {@see Select::$bIsMultiple}
*/
public function IsMultiple(): bool
{
@@ -100,12 +87,10 @@ class Select extends UIContentBlock
}
/**
* @param bool $bIsMultiple
* @param bool $bIsMultiple {@see Select::$bIsMultiple}
*/
public function SetIsMultiple(bool $bIsMultiple): void
{
$this->bIsMultiple = $bIsMultiple;
}
}

View File

@@ -17,8 +17,10 @@ class SelectUIBlockFactory extends AbstractUIBlockFactory
public const UI_BLOCK_CLASS_NAME = Select::class;
/**
* @param string $sName
* @param string|null $sId
* Create a default Select input
*
* @param string $sName {@see Select::$sName}
* @param string|null $sId {@see UIBlock::$sId}
*
* @return \Combodo\iTop\Application\UI\Base\Component\Input\Select\Select
*/
@@ -31,24 +33,23 @@ class SelectUIBlockFactory extends AbstractUIBlockFactory
}
/**
* Create a Select input with a label
*
* If you need to have a real field with a label, you might use a {@link Field} component instead
*
* @param string $sName
* @param string $sLabel
* @param string|null $sId
* @param string $sName {@see Select::$sName}
* @param string $sLabel {@see Select::$sLabel}
* @param string|null $sId {@see UIBlock::$sId}
*
* @return \Combodo\iTop\Application\UI\Base\Component\Input\InputWithLabel
* @return \Combodo\iTop\Application\UI\Base\Component\Input\Select\Select
*/
public static function MakeForSelectWithLabel(string $sName, string $sLabel, ?string $sId = null): InputWithLabel
public static function MakeForSelectWithLabel(string $sName, string $sLabel, ?string $sId = null): Select
{
$oInput = new Select($sId);
$oInput->SetName($sName);
$oInput->SetLabel($sLabel);
if (is_null($sId)) {
$sId = $oInput->GetId();
}
return new InputWithLabel($sLabel, $oInput, $sId);
return $oInput;
}
}

View File

@@ -0,0 +1,88 @@
<?php
/**
* @copyright Copyright (C) 2010-2021 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Application\UI\Base\Component\Input;
/**
* Trait tInputLabel Label for input
*
* @package Combodo\iTop\Application\UI\Base\Component\Input
*/
trait tInputLabel
{
/** @var bool If true the label will be positioned before the input */
protected $bIsLabelBefore = true;
/** @var string|null Label to display with the input (null for no label) */
protected $sLabel = null;
/**
* @return bool
*/
public function IsLabelBefore(): bool
{
return $this->bIsLabelBefore;
}
/**
* @param bool $bIsLabelBefore {@see tInputLabel::$bIsLabelBefore}
*
* @return $this
*/
public function SetIsLabelBefore(bool $bIsLabelBefore)
{
$this->bIsLabelBefore = $bIsLabelBefore;
if ($this->bIsLabelBefore) {
$this->AddCSSClass('ibo-input--label-left');
$this->RemoveCSSClass('ibo-input--label-right');
} else {
$this->AddCSSClass('ibo-input--label-right');
$this->RemoveCSSClass('ibo-input--label-left');
}
return $this;
}
/**
* @return string|null
*/
public function GetLabel(): ?string
{
return $this->sLabel;
}
/**
* @param string|null $sLabel {@see tInputLabel::$sLabel}
*
* @return $this
*/
public function SetLabel(?string $sLabel)
{
$this->sLabel = $sLabel;
if (!is_null($sLabel)) {
if ($this->bIsLabelBefore) {
$this->AddCSSClass('ibo-input--label-left');
$this->RemoveCSSClass('ibo-input--label-right');
} else {
$this->AddCSSClass('ibo-input--label-right');
$this->RemoveCSSClass('ibo-input--label-left');
}
} else {
$this->RemoveCSSClass('ibo-input--label-right');
$this->RemoveCSSClass('ibo-input--label-left');
}
return $this;
}
/**
* @return bool
*/
public function HasLabel(): bool
{
return $this->sLabel != null;
}
}