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

Commit

Permalink
Merge pull request #14 from subzero10/master
Browse files Browse the repository at this point in the history
Add support for dynamic default value on dynamic select
  • Loading branch information
royduin authored Jun 1, 2021
2 parents ca846f1 + cfba616 commit be3e853
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 39 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ composer require royduin/laravel-nova-field-dynamic-select

## Usage

Class has 3 special methods on top of default Select from Laravel Nova.
- `dependsOn` can take a list of other fields this one depends on.
Class has 4 special methods on top of default Select from Laravel Nova.
- `dependsOn` can take a list of other fields this one depends on. On initial page load the list will be null, so make sure to check for null before accessing.
- `default` can take a list of other fields this one depends on.
- `options` can be either an array or a callable.
- `forAction` to indicate that the dynamic select is running in an action
- `forAction` to indicate that the dynamic select is running in an action.


If its a callable, it will receive array with selected dependency values as first argument and should return an array of items to be shown on the select field.

Expand Down Expand Up @@ -47,6 +49,17 @@ public function fields(Request $request)
return ['A' => 'Fast shipping', 'B' => 'Normal shipping'];
}
})
->default(function ($values) {
if (! $values) {
return null;
}
if ($values['country'] === 'UK') {
return [
'label' => 'Normal shipping',
'value' => 'B'
];
}
})
->rules('required')
,
];
Expand Down
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default {
data() {
return {
defaultValue: null,
options: []
};
},
Expand Down Expand Up @@ -151,15 +152,21 @@ export default {
jsoned[i] = depends[i];
}
this.options = (await Nova.request().post("/nova-vendor/dynamic-select/options/"+this.resourceName, {
const resp = (await Nova.request().post("/nova-vendor/dynamic-select/options/"+this.resourceName, {
attribute: this.field.originalAttribute ? this.field.originalAttribute : this.removeFlexibleContentPrefix(this.field.attribute),
depends: this.getDependValues(dependsOnValue.value, originalDependsOnAttribute),
action: this.field.action,
})).data.options;
})).data;
this.defaultValue = resp.default;
this.options = resp.options;
if(this.value) {
this.value = this.options.find(item => item['value'] == this.value['value']);
}
if (!this.value && this.defaultValue) {
this.value = this.defaultValue;
}
}
},
}
Expand Down
2 changes: 2 additions & 0 deletions src/DynamicSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use RuntimeException;
use Hubertnnn\LaravelNova\Fields\DynamicSelect\Traits\DependsOnAnotherField;
use Hubertnnn\LaravelNova\Fields\DynamicSelect\Traits\HasDynamicDefaultValue;
use Hubertnnn\LaravelNova\Fields\DynamicSelect\Traits\HasDynamicOptions;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Field;
Expand All @@ -12,6 +13,7 @@
class DynamicSelect extends Field
{
use HasDynamicOptions;
use HasDynamicDefaultValue;
use DependsOnAnotherField;

public $component = 'dynamic-select';
Expand Down
84 changes: 51 additions & 33 deletions src/Http/Controllers/OptionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public function index(NovaRequest $request)

/** @var DynamicSelect $field */
$options = $field->getOptions($dependValues);
$default = $field->getDefaultValue($dependValues);

return [
'options' => $options,
'default' => $default
];
}

Expand All @@ -50,37 +52,61 @@ private function getFieldFromResource(NovaRequest $request, $attribute) {
}

if (!isset($field)) {
foreach ($fields as $updateField) {
// Flexible content compatibility:
// https://github.com/whitecube/nova-flexible-content
if ($updateField->component == 'nova-flexible-content') {
foreach ($updateField->meta['layouts'] as $layout) {
foreach ($layout->fields() as $layoutField) {
if ($layoutField->attribute === $attribute) {
$field = $layoutField;
}
}
}
$field = $this->getFieldFromComplexFields($fields, $attribute);
}

return $field;
}

private function getFieldFromAction(NovaRequest $request, $attribute) {
$class = $request->input('action');
$action = new $class();

$fields = $action->fields();
$field = collect($fields)->first(function ($f) use ($attribute) {
return $f->attribute === $attribute;
});

if (!$field) {
$field = $this->getFieldFromComplexFields($fields, $attribute);
}

// Dependency container compatibility:
// https://github.com/epartment/nova-dependency-container
} elseif ($updateField->component == 'nova-dependency-container') {
foreach ($updateField->meta['fields'] as $layoutField) {
return $field;
}

private function getFieldFromComplexFields(array $fields, string $attribute) {
$field = null;

foreach ($fields as $updateField) {
// Flexible content compatibility:
// https://github.com/whitecube/nova-flexible-content
if ($updateField->component == 'nova-flexible-content') {
foreach ($updateField->meta['layouts'] as $layout) {
foreach ($layout->fields() as $layoutField) {
if ($layoutField->attribute === $attribute) {
$field = $layoutField;
}
}
}

// Dependency container compatibility:
// https://github.com/epartment/nova-dependency-container
} elseif ($updateField->component == 'nova-dependency-container') {
foreach ($updateField->meta['fields'] as $layoutField) {
if ($layoutField->attribute === $attribute) {
$field = $layoutField;
}
}

// Conditional container compatibility:
// https://github.com/dcasia/conditional-container
} elseif ($updateField->component === 'conditional-container') {
foreach ($updateField->fields as $layouts) {
if ($layouts->component === 'nova-flexible-content') {
foreach ($layouts->meta['layouts'] as $layout) {
foreach ($layout->fields() as $layoutField) {
if ($layoutField->attribute === $attribute) {
$field = $layoutField;
}
// Conditional container compatibility:
// https://github.com/dcasia/conditional-container
} elseif ($updateField->component === 'conditional-container') {
foreach ($updateField->fields as $layouts) {
if ($layouts->component === 'nova-flexible-content') {
foreach ($layouts->meta['layouts'] as $layout) {
foreach ($layout->fields() as $layoutField) {
if ($layoutField->attribute === $attribute) {
$field = $layoutField;
}
}
}
Expand All @@ -91,12 +117,4 @@ private function getFieldFromResource(NovaRequest $request, $attribute) {

return $field;
}

private function getFieldFromAction(NovaRequest $request, $attribute) {
$class = $request->input('action');
$action = new $class();
return collect($action->fields())->first(function ($f) use ($attribute) {
return $f->attribute === $attribute;
});
}
}
18 changes: 18 additions & 0 deletions src/Traits/HasDynamicDefaultValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php


namespace Hubertnnn\LaravelNova\Fields\DynamicSelect\Traits;

use Closure;

trait HasDynamicDefaultValue
{
public function getDefaultValue($parameters = [])
{
if (is_null($this->value) && $this->defaultCallback instanceof Closure) {
return call_user_func($this->defaultCallback, $parameters);
}

return $this->defaultCallback;
}
}

0 comments on commit be3e853

Please sign in to comment.