Skip to content

Commit c8aa817

Browse files
author
Lucas Frey
committed
Update module to allow any type of collection name, not only blocks
1 parent 3266f62 commit c8aa817

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "directus-conditional-fields",
33
"version": "1.0.0",
4-
"description": "Directus conditional fields",
4+
"description": "Conditional fields interface for Directus",
55
"main": "",
66
"scripts": {
77
"build": "directus-extensions build",

readme.md

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
# Directus conditional fields
22

3-
![Directus conditional fields](https://raw.githubusercontent.com/lucasfrey/directus-conditional-fields/master/directus-conditional-fields.gif "Directus conditional fields")
3+
Conditional fields is a custom extension that allow you to hide and show fields in a collection
44

5-
Conditional fields is a custom extension that allow you to hide and show fields in a collection called `block` (for now).
6-
In order to work, you will need to create a collection with these parameters:
5+
![Directus conditional fields](https://raw.githubusercontent.com/lucasfrey/directus-conditional-fields/master/directus-conditional-fields.gif "Directus conditional fields")
76

7+
# Usage
88
### Type dropdown
9-
The `type` dropdown will host the event listener that will trigger the hide and show fields
10-
11-
### Fields naming convention
9+
Add a `type` dropdown that will host the event listener and will trigger the hide and show fields
10+
e.g:
11+
Type:
12+
- editorial
13+
- image
14+
- quote
15+
- ...
16+
17+
### Field naming convention
1218
Once you have setup your type field, you need to name your fields like the above
13-
14-
*NOTE:* currently the collection fields need to be named `block`, but we could easily update that in the future
19+
(example with a collection named `blocks`, with `blocks`, you will need to start your field name with the singular `block`)
1520

1621
| collection | type | field |
1722
|------------|-----------|---------|
@@ -23,10 +28,12 @@ Once you have setup your type field, you need to name your fields like the above
2328
With that config, if you select the type `editorial` in the dropdown, only the `text` and `intro` fields will appear on the screen.
2429

2530
### Import conditional-fields into the collection
26-
Once you have setup your fields, you can then just add the conditional-fields field so the javascript can do his job on the administration page.
31+
Once you have setup your fields, you can then just add the `conditional-fields` field so the javascript can do his job on the administration page.
32+
*NOTE*: you will have to name the field `conditional-fields`
2733

34+
And that's it !
2835

29-
## Usage
36+
## Build
3037
If you update this module, don't forget to run the production build like this :
3138

3239
```

src/input.vue

+18-16
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,14 @@
99
mixins: [mixin],
1010
mounted() {
1111
// Fetch block options
12-
const { values } = this._props
13-
14-
// Start block system
15-
// Basically we hide/show field relevant to the type of blocks
16-
12+
const { values, fields } = this._props
1713
const fieldsNode = document.querySelectorAll('[data-field]');
18-
const interface = document.querySelector('[data-field="block_interface"]');
14+
const conditionalInterface = document.querySelector('[data-field="conditional_interface"]');
1915
const typeField = document.querySelector('[data-field=type]');
20-
2116
const options = this.getOptions();
2217
2318
// hide this custom interface
24-
if (interface) interface.style.display = 'none'
19+
if (conditionalInterface) conditionalInterface.style.display = 'none'
2520
2621
// hide all fields until the user choose a type
2722
this.hideAll(fieldsNode)
@@ -62,22 +57,25 @@
6257
}
6358
},
6459
getOptions() {
65-
// Fetch block options
60+
// Fetch options
6661
const { fields } = this._props
62+
const collectionName = this.getCollectionName(this.$parent.collection);
6763
68-
// build options
69-
let blockOpts = [];
64+
// Build options
65+
let options = [];
7066
7167
Object.keys(fields).forEach(( key, index ) => {
72-
if (key.startsWith('block_') && key !== "block_interface") {
73-
let splitKeys = key.split("_");
68+
if (key.startsWith(`${collectionName}_`) && key !== "conditional_interface") {
69+
const splitKeys = key.split("_");
70+
const type = splitKeys[1];
7471
75-
if (!blockOpts[splitKeys[1]]) blockOpts[splitKeys[1]] = []
76-
blockOpts[splitKeys[1]].push(key)
72+
if (!options[type]) options[type] = []
73+
74+
options[type].push(key)
7775
}
7876
});
7977
80-
return blockOpts;
78+
return options;
8179
},
8280
showFields(fieldsNode) {
8381
if (fieldsNode) {
@@ -89,6 +87,10 @@
8987
}
9088
}
9189
}
90+
},
91+
getCollectionName(collection) {
92+
// If the collection name ends with an 's', we use the singular name
93+
return collection.endsWith('s') ? collection.substring(0, collection.length - 1) : collection
9294
}
9395
}
9496
}

0 commit comments

Comments
 (0)