Skip to content

Commit

Permalink
Implement support for 'itemComponent' property in Vue2 (#7385)
Browse files Browse the repository at this point in the history
* Refactor vue3

* Work for #7188: implement custom item for radiogroup/checkbox questions in Vue2
  • Loading branch information
dk981234 committed Nov 20, 2023
1 parent 7360c48 commit 9315017
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 76 deletions.
6 changes: 1 addition & 5 deletions packages/survey-vue3-ui/src/Checkbox.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<template>
<SelectBase
:question="question"
:input-type="'checkbox'"
:show-legend="true"
></SelectBase>
<SelectBase :question="question" :show-legend="true"></SelectBase>
</template>
<script lang="ts" setup>
import type { QuestionCheckboxModel } from "survey-core";
Expand Down
2 changes: 1 addition & 1 deletion packages/survey-vue3-ui/src/Radiogroup.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<SelectBase :question="question" :input-type="'radio'"></SelectBase>
<SelectBase :question="question"></SelectBase>
</template>

<script lang="ts" setup>
Expand Down
19 changes: 2 additions & 17 deletions packages/survey-vue3-ui/src/SelectBase.vue
Original file line number Diff line number Diff line change
Expand Up @@ -98,36 +98,21 @@ defineOptions({
});
const props = defineProps<{
question: QuestionRadiogroupModel | QuestionCheckboxModel;
inputType: "radio" | "checkbox";
showLegend?: boolean;
}>();
const root = ref(null);
useQuestion(props, root);
const getDefaultItemComponentName = () =>
props.inputType == "radio"
? "survey-radiogroup-item"
: "survey-checkbox-item";
const getItemValueComponentName = (item: ItemValue) => {
return (
props.question.getItemValueWrapperComponentName(item) ||
getDefaultItemComponentName()
props.question.itemComponent
);
};
const getItemValueComponentData = (item: ItemValue) => {
const itemComponentProperty =
props.question.getPropertyByName("itemComponent");
const isDefaultItemComponent = itemComponentProperty.isDefaultValue(
props.question.itemComponent
);
const itemComponent = isDefaultItemComponent
? getDefaultItemComponentName()
: props.question.itemComponent;
return {
componentName: itemComponent,
componentName: props.question.itemComponent,
componentData: {
question: props.question,
item,
Expand Down
41 changes: 18 additions & 23 deletions src/vue/checkbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,31 @@
:aria-describedby="question.a11y_input_ariaDescribedBy"
>
<legend class="sv-hidden">{{question.locTitle.renderedHtml}}</legend>
<survey-checkbox-item
v-for="(item, index) in question.headItems"
<component
v-for="item in question.headItems"
v-if="question.hasHeadItems"
:key="item.value"
:class="question.getItemClass(item)"
:is="question.itemComponent"
:question="question"
:item="item"
:index="'' + index"
></survey-checkbox-item>
<survey-checkbox-item
></component>
<component
v-if="!question.hasColumns && !question.blockedRow"
v-for="(item, index) in question.bodyItems"
v-for="(item) in question.bodyItems"
:key="item.value"
:class="question.getItemClass(item)"
:is="question.itemComponent"
:question="question"
:item="item"
:index="index"
></survey-checkbox-item>
></component>
<div :class="question.cssClasses.rootRow" v-if="question.blockedRow">
<survey-checkbox-item
<component
v-if="!question.hasColumns && question.blockedRow"
v-for="(item, index) in question.dataChoices"
:key="item.value"
:class="question.getItemClass(item)"
:is="question.itemComponent"
:question="question"
:item="item"
:index="index"
></survey-checkbox-item>
></component>
</div>
<div
v-if="question.hasColumns"
Expand All @@ -47,25 +44,23 @@
:class="question.getColumnClass()"
role="presentation"
>
<survey-checkbox-item
v-for="(item, index) in column"
<component
v-for="item in column"
:key="item.value"
:class="question.getItemClass(item)"
:is="question.itemComponent"
:question="question"
:item="item"
:index="'' + colIndex + index"
></survey-checkbox-item>
></component>
</div>
</div>
<survey-checkbox-item
<component
v-for="(item, index) in question.footItems"
v-if="question.hasFootItems"
:key="item.value"
:class="question.getItemClass(item)"
:is="question.itemComponent"
:question="question"
:item="item"
:index="'' + index"
></survey-checkbox-item>
></component>
<survey-other-choice
v-if="
question.renderedValue && question.isOtherSelected
Expand Down
3 changes: 1 addition & 2 deletions src/vue/checkboxitem.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div role="presentation">
<div role="presentation" :class="question.getItemClass(item)">
<label :class="question.getLabelClass(item)">
<input v-if="item == question.selectAllItem" type="checkbox" role="option" :name="question.name+item.value"
:value="isAllSelected" v-model="isAllSelected" :id="question.getItemId(item)"
Expand Down Expand Up @@ -29,7 +29,6 @@ import { BaseVue } from "./base";
export class CheckboxItem extends BaseVue {
@Prop() question: any;
@Prop() item: ItemValue;
@Prop() index: any;
@Prop() hideLabel: boolean;
protected getModel(): Base {
return this.item;
Expand Down
4 changes: 0 additions & 4 deletions src/vue/matrixcell.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,15 @@
<survey-radiogroup-item
v-if="cell.isRadio"
:key="cell.item.value"
:class="cell.question.getItemClass(cell.item)"
:question="cell.question"
:item="cell.item"
:index="getCellIndex()"
:hideLabel="true"
></survey-radiogroup-item>
<survey-checkbox-item
v-if="cell.isCheckbox"
:key="cell.item.value"
:class="cell.question.getItemClass(cell.item)"
:question="cell.question"
:item="cell.item"
:index="getCellIndex()"
:hideLabel="true"
></survey-checkbox-item>
<survey-other-choice
Expand Down
37 changes: 15 additions & 22 deletions src/vue/radiogroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,23 @@
:aria-invalid="question.a11y_input_ariaInvalid"
:aria-describedby="question.a11y_input_ariaDescribedBy"
>
<survey-radiogroup-item
<component
v-if="!question.hasColumns && !question.blockedRow"
v-for="(item, index) in question.bodyItems"
v-for="(item) in question.bodyItems"
:key="item.value"
:class="getItemClass(item)"
:is="question.itemComponent"
:question="question"
:item="item"
:index="index"
></survey-radiogroup-item>
></component>
<div :class="question.cssClasses.rootRow" v-if="question.blockedRow">
<survey-radiogroup-item
<component
v-if="!question.hasColumns && question.blockedRow"
v-for="(item, index) in question.dataChoices"
v-for="(item) in question.dataChoices"
:key="item.value"
:class="question.getItemClass(item)"
:is="question.itemComponent"
:question="question"
:item="item"
:index="index"
></survey-radiogroup-item>
></component>
</div>
<div
v-if="question.hasColumns"
Expand All @@ -36,25 +34,23 @@
:class="question.getColumnClass()"
role="presentation"
>
<survey-radiogroup-item
v-for="(item, index) in column"
<component
v-for="item in column"
:key="item.value"
:class="getItemClass(item)"
:is="question.itemComponent"
:question="question"
:item="item"
:index="'' + colIndex + index"
></survey-radiogroup-item>
></component>
</div>
</div>
<survey-radiogroup-item
<component
v-for="(item, index) in question.footItems"
v-if="question.hasFootItems"
:key="item.value"
:class="question.getItemClass(item)"
:is="question.itemComponent"
:question="question"
:item="item"
:index="'' + index"
></survey-radiogroup-item>
></component>
<survey-other-choice
v-if="
question.renderedValue && question.isOtherSelected
Expand Down Expand Up @@ -85,9 +81,6 @@ export class Radiogroup extends QuestionVue<QuestionRadiogroupModel> {
get choicesCount() {
return this.question.visibleChoices.length - 1;
}
getItemClass(item: any) {
return this.question.getItemClass(item);
}
}
Vue.component("survey-radiogroup", Radiogroup);
export default Radiogroup;
Expand Down
3 changes: 1 addition & 2 deletions src/vue/radiogroupitem.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div role="presentation">
<div role="presentation" :class="question.getItemClass(item)">
<label @mousedown="question.onMouseDown()" :class="getLabelClass(item)">
<input
type="radio"
Expand Down Expand Up @@ -40,7 +40,6 @@ import { BaseVue } from "./base";
export class RadiogroupItem extends BaseVue {
@Prop() question: any;
@Prop() item: ItemValue;
@Prop() index: any;
@Prop() hideLabel: boolean;
protected getModel(): Base {
return this.item;
Expand Down

0 comments on commit 9315017

Please sign in to comment.