Implement options and default value in select/checkbox/radio extra vars

This commit is contained in:
Kijin Sung 2024-10-08 21:57:59 +09:00
parent db4103b732
commit 782df3a42e
3 changed files with 41 additions and 11 deletions

View file

@ -150,7 +150,6 @@ class Value
'type' => $this->type, 'type' => $this->type,
'value' => self::_getTypeValue($this->type, $this->value), 'value' => self::_getTypeValue($this->type, $this->value),
'default' => self::_getTypeValue($this->type, $this->default), 'default' => self::_getTypeValue($this->type, $this->default),
'options' => $this->getOptions(),
'input_name' => $this->parent_type === 'document' ? ('extra_vars' . $this->idx) : ($this->input_name ?: $this->eid), 'input_name' => $this->parent_type === 'document' ? ('extra_vars' . $this->idx) : ($this->input_name ?: $this->eid),
'input_id' => $this->input_id ?: '', 'input_id' => $this->input_id ?: '',
]); ]);
@ -158,13 +157,24 @@ class Value
} }
/** /**
* Check if the current value can have options. * Get the default value.
* *
* @return bool * @return mixed
*/ */
public function canHaveOptions(): bool public function getDefaultValue()
{ {
return isset(self::OPTION_TYPES[$this->type]); if (!$this->canHaveOptions())
{
return $this->default;
}
elseif (is_array($this->options))
{
return $this->default;
}
else
{
return null;
}
} }
/** /**
@ -193,6 +203,16 @@ class Value
} }
} }
/**
* Check if the current value can have options.
*
* @return bool
*/
public function canHaveOptions(): bool
{
return isset(self::OPTION_TYPES[$this->type]);
}
/** /**
* Get the next temporary ID. * Get the next temporary ID.
* *

View file

@ -1,10 +1,15 @@
@php
$has_value = is_array($value);
$default_value = $definition->getDefaultValue();
@endphp
@if ($parent_type === 'member') @if ($parent_type === 'member')
<div class="rx_ev_{{ $type }}" style="padding-top:5px"> <div class="rx_ev_{{ $type }}" style="padding-top:5px">
@foreach ($default ?? [] as $v) @foreach ($definition->getOptions() as $v)
@php @php
$column_suffix = $type === 'checkbox' ? '[]' : ''; $column_suffix = $type === 'checkbox' ? '[]' : '';
$tempid = $definition->getNextTempID(); $tempid = $definition->getNextTempID();
$is_checked = is_array($value) && in_array(trim($v), $value); $is_checked = $has_value ? in_array($v, $value) : ($v === $default_value);
@endphp @endphp
<label for="{{ $tempid }}"> <label for="{{ $tempid }}">
<input type="{{ $type }}" name="{{ $input_name . $column_suffix }}" <input type="{{ $type }}" name="{{ $input_name . $column_suffix }}"
@ -19,11 +24,11 @@
</div> </div>
@else @else
<ul class="rx_ev_{{ $type }}"> <ul class="rx_ev_{{ $type }}">
@foreach ($default ?? [] as $v) @foreach ($definition->getOptions() as $v)
@php @php
$column_suffix = $type === 'checkbox' ? '[]' : ''; $column_suffix = $type === 'checkbox' ? '[]' : '';
$tempid = $definition->getNextTempID(); $tempid = $definition->getNextTempID();
$is_checked = is_array($value) && in_array(trim($v), $value); $is_checked = $has_value ? in_array($v, $value) : ($v === $default_value);
@endphp @endphp
<li> <li>
<input type="{{ $type }}" name="{{ $input_name . $column_suffix }}" <input type="{{ $type }}" name="{{ $input_name . $column_suffix }}"

View file

@ -1,10 +1,15 @@
@php
$has_value = is_array($value);
$default_value = $definition->getDefaultValue();
@endphp
<select name="{{ $input_name }}" <select name="{{ $input_name }}"
id="{{ $input_id }}"|if="$input_id" class="select rx_ev_select" id="{{ $input_id }}"|if="$input_id" class="select rx_ev_select"
style="{{ $definition->style }}"|if="$definition->style" style="{{ $definition->style }}"|if="$definition->style"
@required(toBool($definition->is_required)) @required(toBool($definition->is_required))
@disabled(toBool($definition->is_disabled)) @disabled(toBool($definition->is_disabled))
@readonly(toBool($definition->is_readonly))> @readonly(toBool($definition->is_readonly))>
@foreach ($default ?: [] as $v) @foreach ($definition->getOptions() as $v)
<option value="{{ $v }}" @selected(is_array($value) && in_array(trim($v), $value))>{{ $v }}</option> <option value="{{ $v }}" @selected($has_value ? in_array($v, $value) : ($v === $default_value))>{{ $v }}</option>
@endforeach @endforeach
</select> </select>