Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
justinehell committed Sep 13, 2023
1 parent 957f2f8 commit d51cce5
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 172 deletions.
35 changes: 2 additions & 33 deletions src/components/inputs/ArrayInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
} from 'vue';
import { isRequired } from 'src/composables/QuasarFieldRule';
import ItemList from 'components/inputs/ItemList';
import { initSelectOptions } from 'src/composables/InputManager';
const props = defineProps({
attribute: {
Expand All @@ -53,38 +54,6 @@ const localValue = ref(attribute.value.value);
const defaultValues = ref(attribute.value.definition.rules.values || []);
const variables = ref(plugin.value.data.variables || []);
/**
* Initialize the options for the select.
*/
function initOptions() {
const categories = [...new Set(variables.value.map(({ category }) => category))];
const children = categories.map((category) => ({
type: 'category',
name: category,
children: variables.value
.filter((variable) => variable.category === category)
.map((variable) => ({
type: 'item',
name: variable.name,
value: variable.value !== null ? variable.value : variable.defaultValue,
formattedName: variable.formattedName,
})),
}));
options.value = [{
type: 'category',
name: 'default',
children: defaultValues.value.map((value) => ({
type: 'item',
value,
})),
}, {
type: 'category',
name: 'variable',
children,
}];
}
watch(() => arrayInput.value, () => {
if (arrayInput.value) {
arrayInput.value.validate();
Expand All @@ -106,6 +75,6 @@ watch(() => localValue.value, () => {
}, { deep: true });
onMounted(() => {
initOptions();
options.value = initSelectOptions(variables.value, defaultValues.value);
});
</script>
6 changes: 5 additions & 1 deletion src/components/inputs/ItemList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
<q-expansion-item
v-if="item.type === 'category'"
expand-separator
:label="item.name"
dense-toggle
:header-inset-level="0"
:label="$te(item.name) ? $t(item.name) : item.name"
header-class="text-weight-medium"
default-closed
>
<item-list
v-for="child in item.children"
:key="child.name"
:header-inset-level="0.2"
:item="child"
class="item-list"
@select-item="(value) => $emit('select-item', value)"
Expand All @@ -18,6 +21,7 @@
v-else
v-ripple
clickable
class="bg-grey-3"
@click="$emit('select-item', item.formattedName ? item.formattedName : item.value)"
>
<q-item-section
Expand Down
39 changes: 3 additions & 36 deletions src/components/inputs/LinkInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
} from 'vue';
import { isRequired } from 'src/composables/QuasarFieldRule';
import ItemList from 'components/inputs/ItemList';
import { initSelectOptions } from 'src/composables/InputManager';
const props = defineProps({
attribute: {
Expand All @@ -51,9 +52,7 @@ const emit = defineEmits(['update:model-value']);
const linkInput = ref(null);
const { attribute, plugin } = toRefs(props);
const localValue = ref(attribute.value.value);
const options = ref(plugin.value.data.getComponentsByType(
attribute.value.definition.linkRef,
).map(({ id }) => id));
const options = ref([]);
const iconName = ref(plugin.value.data.definitions.components.find(
({ type }) => type === attribute.value.definition.linkRef,
).icon);
Expand All @@ -62,38 +61,6 @@ const defaultValues = ref(plugin.value.data.getComponentsByType(
).map(({ id }) => id));
const variables = ref(plugin.value.data.variables || []);
/**
* Initialize the options for the select.
*/
function initOptions() {
const categories = [...new Set(variables.value.map(({ category }) => category))];
const children = categories.map((category) => ({
type: 'category',
name: category,
children: variables.value
.filter((variable) => variable.category === category)
.map((variable) => ({
type: 'item',
name: variable.name,
value: variable.value !== null ? variable.value : variable.defaultValue,
formattedName: variable.formattedName,
})),
}));
options.value = [{
type: 'category',
name: 'default',
children: defaultValues.value.map((value) => ({
type: 'item',
value,
})),
}, {
type: 'category',
name: 'variable',
children,
}];
}
watch(() => props.plugin.data.components, () => {
defaultValues.value = props.plugin.data.getComponentsByType(
props.attribute.definition.linkRef,
Expand All @@ -119,6 +86,6 @@ watch(() => localValue.value, () => {
});
onMounted(() => {
initOptions();
options.value = initSelectOptions(variables.value, defaultValues.value);
});
</script>
40 changes: 3 additions & 37 deletions src/components/inputs/ReferenceInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
} from 'vue';
import { isRequired } from 'src/composables/QuasarFieldRule';
import ItemList from 'components/inputs/ItemList';
import { initSelectOptions } from 'src/composables/InputManager';
const props = defineProps({
attribute: {
Expand All @@ -49,10 +50,7 @@ const emit = defineEmits(['update:model-value']);
const referenceInput = ref(null);
const { attribute, plugin } = toRefs(props);
const localValue = ref(attribute.value.value);
const options = ref(plugin.value.data.getComponentsByType(
attribute.value.definition.containerRef,
plugin.value.data.components,
).map(({ id }) => id));
const options = ref([]);
const iconName = ref(plugin.value.data.definitions.components.find(
({ type }) => type === attribute.value.definition.containerRef,
).icon);
Expand All @@ -62,38 +60,6 @@ const defaultValues = ref(plugin.value.data.getComponentsByType(
).map(({ id }) => id));
const variables = ref(plugin.value.data.variables || []);
/**
* Initialize the options for the select.
*/
function initOptions() {
const categories = [...new Set(variables.value.map(({ category }) => category))];
const children = categories.map((category) => ({
type: 'category',
name: category,
children: variables.value
.filter((variable) => variable.category === category)
.map((variable) => ({
type: 'item',
name: variable.name,
value: variable.value !== null ? variable.value : variable.defaultValue,
formattedName: variable.formattedName,
})),
}));
options.value = [{
type: 'category',
name: 'default',
children: defaultValues.value.map((value) => ({
type: 'item',
value,
})),
}, {
type: 'category',
name: 'variable',
children,
}];
}
watch(() => props.plugin.data.components, () => {
defaultValues.value = props.plugin.data.getComponentsByType(
props.attribute.definition.containerRef,
Expand All @@ -119,6 +85,6 @@ watch(() => localValue.value, () => {
});
onMounted(() => {
initOptions();
options.value = initSelectOptions(variables.value, defaultValues.value);
});
</script>
35 changes: 2 additions & 33 deletions src/components/inputs/SelectInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from 'vue';
import { isRequired } from 'src/composables/QuasarFieldRule';
import ItemList from 'components/inputs/ItemList';
import { initSelectOptions } from 'src/composables/InputManager';
const props = defineProps({
attribute: {
Expand All @@ -47,38 +48,6 @@ const options = ref([]);
const defaultValues = ref(attribute.value.definition.rules.values);
const variables = ref(plugin.value.data.variables || []);
/**
* Initialize the options for the select.
*/
function initOptions() {
const categories = [...new Set(variables.value.map(({ category }) => category))];
const children = categories.map((category) => ({
type: 'category',
name: category,
children: variables.value
.filter((variable) => variable.category === category)
.map((variable) => ({
type: 'item',
name: variable.name,
value: variable.value !== null ? variable.value : variable.defaultValue,
formattedName: variable.formattedName,
})),
}));
options.value = [{
type: 'category',
name: 'default',
children: defaultValues.value.map((value) => ({
type: 'item',
value,
})),
}, {
type: 'category',
name: 'variable',
children,
}];
}
watch(() => props.plugin.data.components, () => {
defaultValues.value = props.plugin.data.getComponentsByType(
props.attribute.definition.linkRef,
Expand Down Expand Up @@ -106,6 +75,6 @@ watch(() => localValue.value, () => {
});
onMounted(() => {
initOptions();
options.value = initSelectOptions(variables.value, defaultValues.value);
});
</script>
34 changes: 34 additions & 0 deletions src/composables/InputManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Initialize the options for the select input by formatting the default and variable values array.
* @param {Array} variables - Array of plugin variables values.
* @param {Array} defaultValues - Array of attribute default values.
* @returns {Array} Array of objects representing the default and variable values.
*/
export function initSelectOptions(variables, defaultValues) {
const categories = [...new Set(variables.map(({ category }) => category))];
const children = categories.map((category) => ({
type: 'category',
name: category,
children: variables
.filter((variable) => variable.category === category)
.map((variable) => ({
type: 'item',
name: variable.name,
value: variable.value !== null ? variable.value : variable.defaultValue,
formattedName: variable.formattedName,
})),
}));

return [{
type: 'category',
name: 'plugin.component.attribute.selectInput.defaultValue',
children: defaultValues.map((value) => ({
type: 'item',
value,
})),
}, {
type: 'category',
name: 'plugin.component.attribute.selectInput.variables',
children,
}];
}
4 changes: 4 additions & 0 deletions src/i18n/en-US/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ export default {
hint: {
array: 'Press enter to validate',
},
selectInput: {
defaultValue: 'Default values',
variables: 'Variables',
},
},
},
},
Expand Down
16 changes: 8 additions & 8 deletions tests/unit/components/inputs/ArrayInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Test component: ArrayInput', () => {
let wrapper;

const options = [{
name: 'default',
name: 'plugin.component.attribute.selectInput.defaultValue',
type: 'category',
children: [{
type: 'item',
Expand All @@ -20,7 +20,7 @@ describe('Test component: ArrayInput', () => {
value: 'value2',
}],
}, {
name: 'variable',
name: 'plugin.component.attribute.selectInput.variables',
type: 'category',
children: [{
name: 'variable',
Expand Down Expand Up @@ -111,12 +111,6 @@ describe('Test component: ArrayInput', () => {
});
});

describe('Test function: initOptions', () => {
it('should init options', () => {
expect(wrapper.vm.options).toEqual(options);
});
});

describe('Test watcher: props.attributes', () => {
it('should update localValue when props.attribute.value is defined', async () => {
await wrapper.setProps({
Expand All @@ -129,4 +123,10 @@ describe('Test component: ArrayInput', () => {
expect(wrapper.vm.localValue).toEqual(['updatedValue']);
});
});

describe('Test hook function: onMounted', () => {
it('should init options', () => {
expect(wrapper.vm.options).toEqual(options);
});
});
});
16 changes: 8 additions & 8 deletions tests/unit/components/inputs/LinkInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ describe('Test component: LinkInput', () => {
let wrapper;

const options = [{
name: 'default',
name: 'plugin.component.attribute.selectInput.defaultValue',
type: 'category',
children: [{
type: 'item',
value: 'componentName',
}],
}, {
name: 'variable',
name: 'plugin.component.attribute.selectInput.variables',
type: 'category',
children: [{
name: 'variable',
Expand Down Expand Up @@ -118,12 +118,6 @@ describe('Test component: LinkInput', () => {
});
});

describe('Test function: initOptions', () => {
it('should init options', () => {
expect(wrapper.vm.options).toEqual(options);
});
});

describe('Test watcher: props.plugin.data.components', () => {
it('should update options', async () => {
await wrapper.setProps({
Expand Down Expand Up @@ -157,4 +151,10 @@ describe('Test component: LinkInput', () => {
expect(wrapper.vm.options).toEqual(options);
});
});

describe('Test hook function: onMounted', () => {
it('should init options', () => {
expect(wrapper.vm.options).toEqual(options);
});
});
});
Loading

0 comments on commit d51cce5

Please sign in to comment.