Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
Fixed combobox handling of existing selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebronner committed Apr 17, 2020
1 parent 293227c commit 22582d5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [0.11.7] - 2020-04-16
### Fixed
- combobox handling of existing selection.

## [0.11.6] - 2020-03-22
### Fixed
- asset injection to not break view, so that HTTP tests still work.

## [0.11.5] - 2020-03-07
### Added
- searchField option to allow separation of database query search field and display field.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
}
],
"require": {
"fico7489/laravel-eloquent-join": "^4.1",
"illuminate/config": "^7.0",
"illuminate/support": "^7.0",
"jenssegers/model": "^1.3",
Expand Down
3 changes: 2 additions & 1 deletion resources/views/components/tailwind/combobox.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"valueField" => $options["valueField"] ?? "id",
"placeholder" => $options["placeholder"] ?? "",
"createFormView" => $options["createForm"] ?? "",
"query" => $options["query"] ?? ""
"query" => $options["query"] ?? "",
"value" => $value ?? null
])

@if (! $errors->isEmpty() && ! $errors->has($name))
Expand Down
3 changes: 2 additions & 1 deletion src/Combobox.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php namespace GeneaLabs\LaravelCasts;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Combobox extends Dropdown
{
public function __construct(
string $name,
array $list = [],
$value = null,
Model $value = null,
array $options = [],
array $optionOptions = []
) {
Expand Down
23 changes: 20 additions & 3 deletions src/Http/Livewire/Combobox.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace GeneaLabs\LaravelCasts\Http\Livewire;

use Livewire\Component;
use Illuminate\Support\Str;

class Combobox extends Component
{
Expand All @@ -28,7 +29,8 @@ public function mount(
string $placeholder = "",
string $query = "",
string $searchField = "",
string $valueField = "id"
string $valueField = "id",
string $value = ""
) : void {
$this->createFormView = $createFormView ?: "";
$this->fieldName = $fieldName ?: "";
Expand All @@ -39,6 +41,12 @@ public function mount(
$this->searchField = $searchField ?: "";
$this->valueField = $valueField ?: "id";

if ($value) {
$value = json_decode($value, false);
$this->search = $value->{$this->labelField};
$this->selectedValue = $value->{$this->valueField};
}

if ($placeholder) {
$this->placeholder = $placeholder;
}
Expand All @@ -60,9 +68,18 @@ public function render()
}

if ($query) {
if ($this->searchField) {
if (Str::contains($this->searchField, ".")) {
$query = $query->whereJoin($this->searchField, "ILIKE", "%{$this->search}%")
->orderByJoin($this->searchField);
} else {
// TODO: refactor out from if-else.
$query = $query->where($this->searchField, "ILIKE", "%{$this->search}%")
->orderBy($this->searchField);
}
}

$results = $query
->where($this->searchField, "ILIKE", "%{$this->search}%")
->orderBy($this->searchField)
->limit(100)
->get();
}
Expand Down

0 comments on commit 22582d5

Please sign in to comment.