select builder prompt add

This commit is contained in:
Билай Станислав 2024-12-26 12:40:39 +03:00
parent 3dd054b298
commit 55624f9b67
2 changed files with 72 additions and 12 deletions

View File

@ -8,11 +8,29 @@ use itguild\forms\inputs\Select;
class SelectBuilder
{
public static function build(string $name, array $params = [])
// public static function build(string $name, array $params = [])
// {
// $value = $params['value'] ?? null;
// unset($params['value']);
// $options = $params['options'] ?? [];
// unset($params['options']);
//
// return new Select(name: $name, options: $options, value: $value, paramsArray: $params);
// }
public static function build(string $name, array $params = []): Select
{
$value = $params['value'] ?? null;
unset($params['value']);
if (isset($params['prompt'])) {
$options['prompt'] = $params['prompt'] ?? null;
foreach ($params['options'] as $key => $val) {
$options[$key] = $val;
}
} else {
$options = $params['options'] ?? [];
}
unset($params['options']);
return new Select(name: $name, options: $options, value: $value, paramsArray: $params);

View File

@ -2,6 +2,8 @@
namespace itguild\forms\traits;
use itguild\forms\debug\Debug;
trait CreateOption
{
/**
@ -9,16 +11,46 @@ trait CreateOption
* @param $value
* @return string
*/
// public function createOption(array $options, $value = null): string
// {
// $optionsString = "";
// if (is_array($value)) {
// foreach ($options as $val => $title) {
// $selected = in_array($title, $value) ? "selected" : "";
// $optionsString .= "<option $selected value='$val'>$title</option>";
// }
// } else {
// foreach ($options as $val => $title) {
// if (is_numeric($value)) {
// $selected = (int)$value === $val ? "selected" : "";
// } else {
// $selected = $value === $val ? "selected" : "";
// }
// $optionsString .= "<option $selected value='$val'>$title</option>";
// }
// }
//
// return $optionsString;
// }
public function createOption(array $options, $value = null): string
{
$optionsString = "";
if ($value) {
if (isset($options['prompt'])) {
$prompt = $options['prompt'];
$optionsString .= "<option value='''>$prompt</option>";
}
if (is_array($value)) {
foreach ($options as $val => $title) {
if ($val === 'prompt') continue;
$selected = in_array($title, $value) ? "selected" : "";
$optionsString .= "<option $selected value='$val'>$title</option>";
}
} else {
foreach ($options as $val => $title) {
if ($val === 'prompt') continue;
if (is_numeric($value)) {
$selected = (int)$value === $val ? "selected" : "";
} else {
@ -27,6 +59,16 @@ trait CreateOption
$optionsString .= "<option $selected value='$val'>$title</option>";
}
}
} else {
if (isset($options['prompt'])) {
$prompt = $options['prompt'];
$optionsString .= "<option value='' selected='selected'>$prompt</option>";
}
foreach ($options as $val => $title) {
if ($val === 'prompt') continue;
$optionsString .= "<option value='$val'>$title</option>";
}
}
return $optionsString;
}