Skip to content

Commit

Permalink
select improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jul 14, 2022
1 parent 9b61e11 commit 285c2a8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/Fields/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Cone\Root\Http\Requests\RootRequest;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;

class Select extends Field
{
Expand Down Expand Up @@ -42,6 +43,27 @@ public function nullable(bool $value = true): static
return $this;
}

/**
* Determine if the field is nullable.
*
* @return bool
*/
public function isNullable(): bool
{
return $this->nullable;
}

/**
* Set the multiple attribute.
*
* @param bool $value
* @return $this
*/
public function multiple(bool $value = true): static
{
return $this->setAttribute('multiple', $value);
}

/**
* Set the options attribute.
*
Expand Down Expand Up @@ -99,7 +121,11 @@ public function resolveFormat(RootRequest $request, Model $model): mixed
$this->resolveOptions($request, $model), 'formatted_value', 'value'
);

return $options[$value] ?? $value;
$value = array_map(static function (mixed $value) use ($options): mixed {
return $options[$value] ?? $value;
}, Arr::wrap($value));

return implode(', ', $value);
};
}

Expand Down
20 changes: 20 additions & 0 deletions tests/Fields/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ public function a_select_field_has_select_component()
$this->assertSame('Select', $this->field->getComponent());
}

/** @test */
public function a_select_field_can_be_nullable()
{
$this->assertFalse($this->field->isNullable());

$this->field->nullable();

$this->assertTrue($this->field->isNullable());
}

/** @test */
public function a_select_field_can_be_multiple()
{
$this->assertNull($this->field->multiple);

$this->field->multiple();

$this->assertTrue($this->field->multiple);
}

/** @test */
public function a_select_field_has_options()
{
Expand Down

0 comments on commit 285c2a8

Please sign in to comment.