Skip to content

Commit

Permalink
added dependsOnIn and dependsOnNotIn support
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwenzel committed Jun 20, 2022
1 parent 6d86e24 commit 9c16f38
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 60 deletions.
80 changes: 40 additions & 40 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
{
"name": "alexwenzel/nova-dependency-container",
"description": "A Laravel Nova 4 form container for grouping fields that depend on other field values.",
"keywords": [
"laravel",
"nova",
"nova-4",
"field"
],
"authors": [
{
"name": "alexwenzel",
"email": "[email protected]"
"name": "alexwenzel/nova-dependency-container",
"description": "A Laravel Nova 4 form container for grouping fields that depend on other field values.",
"keywords": [
"laravel",
"nova",
"nova-4",
"field"
],
"authors": [
{
"name": "alexwenzel",
"email": "[email protected]"
},
{
"name": "Epartment E-commerce",
"email": "[email protected]"
}
],
"license": "MIT",
"require": {
"php": "^8.0",
"laravel/framework": "^9.0",
"laravel/nova": "^4.0"
},
{
"name": "Epartment E-commerce",
"email": "[email protected]"
}
],
"license": "MIT",
"require": {
"php": "^8.0",
"laravel/framework": "^9.0",
"laravel/nova": "^4.0"
},
"autoload": {
"psr-4": {
"Alexwenzel\\DependencyContainer\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Alexwenzel\\DependencyContainer\\FieldServiceProvider"
]
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
"autoload": {
"psr-4": {
"Alexwenzel\\DependencyContainer\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Alexwenzel\\DependencyContainer\\FieldServiceProvider"
]
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "mix",
"watch": "mix watch",
"watch-poll": "mix watch -- --watch-options-poll=1000",
"hot": "mix watch --hot",
"prod": "npm run production",
"production": "mix --production",
"nova:install": "npm --prefix='../../vendor/laravel/nova' ci"
},
"devDependencies": {
"@vue/compiler-sfc": "^3.2.22",
"laravel-mix": "^6.0.41",
"lodash": "^4.17.21",
"postcss": "^8.3.11",
"vue-loader": "^16.8.3"
}
"private": true,
"scripts": {
"dev": "npm run development",
"development": "mix",
"watch": "mix watch",
"watch-poll": "mix watch -- --watch-options-poll=1000",
"hot": "mix watch --hot",
"prod": "npm run production",
"production": "mix --production",
"nova:install": "npm --prefix='../../vendor/laravel/nova' ci"
},
"devDependencies": {
"@vue/compiler-sfc": "^3.2.22",
"laravel-mix": "^6.0.41",
"lodash": "^4.17.21",
"postcss": "^8.3.11",
"vue-loader": "^16.8.3"
}
}
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ class Page extends Resource

### Dependencies

The package supports four kinds of dependencies:
The package supports this kinds of dependencies:

1. `->dependsOn('field', 'value')`
2. `->dependsOnNot('field', 'value')`
3. `->dependsOnEmpty('field')`
4. `->dependsOnNotEmpty('field')`
5. `->dependsOnNullOrZero('field')`
6. `->dependsOnIn('field', [array])`
7. `->dependsOnNotIn('field', [array])`

These dependencies can be combined by chaining the methods on the `DependencyContainer`:

Expand Down
10 changes: 10 additions & 0 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ export default {
return;
}
if (dependency.hasOwnProperty('in') && dependency.in.includes(dependencyValue)) {
this.dependenciesSatisfied = true;
return;
}
if (dependency.hasOwnProperty('notin') && !dependency.notin.includes(dependencyValue)) {
this.dependenciesSatisfied = true;
return;
}
if (dependency.hasOwnProperty('value') && dependencyValue == dependency.value) {
this.dependenciesSatisfied = true;
return;
Expand Down
42 changes: 42 additions & 0 deletions src/DependencyContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,38 @@ public function dependsOnNullOrZero($field)
]);
}

/**
* Adds a dependency for in
*
* @param $field
* @param $array
* @return $this
*/
public function dependsOnIn($field, $array)
{
return $this->withMeta([
'dependencies' => array_merge($this->meta['dependencies'], [
array_merge($this->getFieldLayout($field), ['in' => $array])
])
]);
}

/**
* Adds a dependency for not in
*
* @param $field
* @param $array
* @return $this
*/
public function dependsOnNotIn($field, $array)
{
return $this->withMeta([
'dependencies' => array_merge($this->meta['dependencies'], [
array_merge($this->getFieldLayout($field), ['notin' => $array])
])
]);
}

/**
* Get layout for a specified field. Dot notation will result in {field}.{property}. If no dot was found it will
* result in {field}.{field}, as it was in previous versions by default.
Expand Down Expand Up @@ -173,6 +205,16 @@ public function resolveForDisplay($resource, $attribute = null)
continue;
}

if (array_key_exists('in', $dependency) && in_array($resource->{$dependency['property']}, $dependency['in'])) {
$this->meta['dependencies'][$index]['satisfied'] = true;
continue;
}

if (array_key_exists('notin', $dependency) && !in_array($resource->{$dependency['property']}, $dependency['notin'])) {
$this->meta['dependencies'][$index]['satisfied'] = true;
continue;
}

if (array_key_exists('value', $dependency)) {
if (is_array($resource)) {
if (isset($resource[$dependency['property']]) && $dependency['value'] == $resource[$dependency['property']]) {
Expand Down

0 comments on commit 9c16f38

Please sign in to comment.