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 #26 from RobertoNegro/master
Browse files Browse the repository at this point in the history
Update laravel mix 6, resource fields taken from the specific resource on update instead of empty generic one, allow custom colors
  • Loading branch information
royduin authored Apr 25, 2022
2 parents 307fbfd + 34301b1 commit 158ada8
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 26 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ composer require royduin/laravel-nova-field-dynamic-select

## Usage

Class has 4 special methods on top of default Select from Laravel Nova.
Class has 5 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.
- `colors` (optional) to choose which colors to use when highlighting options. Accepts an array with 4 different hexadecimal colors:
- `highlightBgColor`: Color of the background when the cursor is over an option
- `highlightTextColor`: Color of the text when the cursor is over an option
- `selectedHighlightBgColor`: Color of the background when the cursor is over a selected option
- `selectedHighlightTextColor`: Color of the text when the cursor is over a selected option


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 All @@ -33,6 +38,12 @@ public function fields(Request $request)
DynamicSelect::make('Country', 'country')
->options(['US' => 'United States', 'UK' => 'United Kingdom'])
->rules('required')
->colors([
'highlightBgColor' => '#0f0',
'highlightTextColor' => '#fff',
'selectedHighlightBgColor' => '#f00',
'selectedHighlightTextColor' => '#fff',
])
,
DynamicSelect::make('Provider', 'provider')
Expand Down
1 change: 1 addition & 0 deletions dist/css/field.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 2 additions & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions dist/js/field.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*!
* Determine if an object is a Buffer
*
* @author Feross Aboukhadijeh <https://feross.org>
* @license MIT
*/

/**
* @license
* Lodash <https://lodash.com/>
* Copyright OpenJS Foundation and other contributors <https://openjsf.org/>
* Released under MIT license <https://lodash.com/license>
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
* Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
*/
2 changes: 1 addition & 1 deletion dist/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"/js/field.js": "/js/field.js",
"/css/field.css": "/css/field.css"
}
}
24 changes: 14 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"development": "mix",
"watch": "mix watch",
"watch-poll": "mix watch -- --watch-options-poll=1000",
"hot": "mix watch --hot",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
"production": "mix --production"
},
"devDependencies": {
"cross-env": "^5.0.0",
"laravel-mix": "^1.0",
"laravel-nova": "^1.0"
"laravel-mix": "^6.0.43",
"laravel-nova": "^1.12.3",
"resolve-url-loader": "^5.0.0",
"sass": "^1.50.1",
"sass-loader": "^12.6.0",
"vue-loader": "^15.9.8",
"vue-template-compiler": "^2.6.14"
},
"dependencies": {
"vue": "^2.5.0",
"vue-multiselect": "^2.1.0"
"vue-multiselect": "^2.1.4",
"vue": "^2.6.14"
}
}
18 changes: 15 additions & 3 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
:deselectLabel="field.deselectLabel"
:selectedLabel="field.selectedLabel"
:loading="isLoading"
@input="onChange">
</multiselect>
@input="onChange"
:style="`--multiselect-highlight-bg: ${field.highlightBgColor};
--multiselect-highlight-text: ${field.highlightTextColor};
--multiselect-selected-highlight-bg: ${field.selectedHighlightBgColor};
--multiselect-selected-highlight-text: ${field.selectedHighlightTextColor};`"
></multiselect>
</template>
</default-field>
</template>
Expand Down Expand Up @@ -155,7 +159,7 @@ export default {
}
this.isLoading = true;
const resp = (await Nova.request().post("/nova-vendor/dynamic-select/options/"+this.resourceName, {
const resp = (await Nova.request().post("/nova-vendor/dynamic-select/options/"+this.resourceName+(this.resourceId ? ('/'+this.resourceId) : ''), {
attribute: this.field.originalAttribute ? this.field.originalAttribute : this.removeFlexibleContentPrefix(this.field.attribute),
depends: this.getDependValues(dependsOnValue.value, originalDependsOnAttribute),
action: this.field.action,
Expand Down Expand Up @@ -198,4 +202,12 @@ export default {
.multiselect__select:before {
content: none !important;
}
.multiselect__option--selected.multiselect__option--highlight, .multiselect__option--selected.multiselect__option--highlight:after {
background: var(--multiselect-selected-highlight-bg);
color: var(--multiselect-selected-highlight-text);
}
.multiselect__option--highlight, .multiselect__option--highlight:after {
background: var(--multiselect-highlight-bg);
color: var(--multiselect-highlight-text);
}
</style>
8 changes: 4 additions & 4 deletions resources/js/field.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Nova.booting((Vue, router) => {
Vue.component('index-dynamic-select', require('./components/IndexField'));
Vue.component('detail-dynamic-select', require('./components/DetailField'));
Vue.component('form-dynamic-select', require('./components/FormField'));
Nova.booting((Vue, router, store) => {
Vue.component('index-dynamic-select', require('./components/IndexField').default);
Vue.component('detail-dynamic-select', require('./components/DetailField').default);
Vue.component('form-dynamic-select', require('./components/FormField').default);
})
2 changes: 1 addition & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
|
*/

Route::post('/options/{resource}', 'OptionsController@index');
Route::post('/options/{resource}/{resourceId?}', 'OptionsController@index');
24 changes: 23 additions & 1 deletion src/DynamicSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class DynamicSelect extends Field
public $labelKey;
public $multiselect = false;
public $action = null;
public $colors = [
'highlightBgColor' => '#41b883',
'highlightTextColor' => '#fff',
'selectedHighlightBgColor' => '#ff6a6a',
'selectedHighlightTextColor' => '#fff',
];

public function resolve($resource, $attribute = null)
{
Expand Down Expand Up @@ -107,6 +113,18 @@ public function multiselect($multiselect = true)
return $this;
}

/**
* Set the colors
* @param array{highlightBgColor: string, highlightTextColor: string, selectedHighlightBgColor: string, selectedHighlightTextColor: string} $colors
* @return $this
*/
public function colors(array $colors)
{
$this->colors = array_merge($this->colors, $colors ?? []);

return $this;
}

public function labelKey($labelKey)
{
$this->labelKey = $labelKey;
Expand All @@ -133,7 +151,11 @@ public function meta()
'selectedLabel' => __('Selected'),
'labelKey' => $this->labelKey,
'multiselect' => $this->multiselect,
'action' => $this->action
'action' => $this->action,
'highlightBgColor' => $this->colors['highlightBgColor'] ?? '#41b883',
'highlightTextColor' => $this->colors['highlightTextColor'] ?? '#fff',
'selectedHighlightBgColor' => $this->colors['selectedHighlightBgColor'] ?? '#ff6a6a',
'selectedHighlightTextColor' => $this->colors['selectedHighlightTextColor'] ?? '#fff',
], $this->meta);
}
}
6 changes: 5 additions & 1 deletion src/Http/Controllers/OptionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public function index(NovaRequest $request)
}

private function getFieldFromResource(NovaRequest $request, $attribute) {
$resource = $request->newResource();
$resource = $request->newResourceWith(
($request->resourceId
? $request->findModelQuery()->first()
: null) ?? $request->model()
);

$field = null;

Expand Down
13 changes: 10 additions & 3 deletions webpack.mix.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
let mix = require('laravel-mix')

mix.setPublicPath('dist')
.js('resources/js/field.js', 'js')
.sass('resources/sass/field.scss', 'css')
mix
.setPublicPath('dist')
.js('resources/js/field.js', 'js')
.vue({ version: 2 })
.sass('resources/sass/field.scss', 'css')
.webpackConfig({
externals: {
Vue: 'vue',
},
})

0 comments on commit 158ada8

Please sign in to comment.