-
Notifications
You must be signed in to change notification settings - Fork 126
feat: rating question type #3637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
272 changes: 272 additions & 0 deletions
src/components/Questions/QuestionRating.vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,272 @@ | ||
| <!-- | ||
| - SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| - SPDX-License-Identifier: AGPL-3.0-or-later | ||
| --> | ||
|
|
||
| <template> | ||
| <Question | ||
| v-bind="questionProps" | ||
| :titlePlaceholder="answerType.titlePlaceholder" | ||
| :warningInvalid="answerType.warningInvalid" | ||
| :errorMessage="errorMessage" | ||
| v-on="commonListeners"> | ||
| <template #actions> | ||
| <NcActionInput | ||
| :modelValue="optionsHighest" | ||
| type="multiselect" | ||
| :clearable="false" | ||
| :label="t('forms', 'Number of icons')" | ||
| labelOutside | ||
| :options="[2, 3, 4, 5, 6, 7, 8, 9, 10]" | ||
| required | ||
| @update:modelValue="onOptionsHighestChange"> | ||
| <template #icon> | ||
| <NcIconSvgWrapper :svg="outlineIcon" /> | ||
| </template> | ||
| </NcActionInput> | ||
| <NcActionRadio | ||
| v-for="icon in iconChoices" | ||
| :key="icon.id" | ||
| :modelValue="ratingIcon" | ||
| :name="`ratingIcon_${id}`" | ||
| :value="icon.id" | ||
| @update:modelValue="onRatingIconChange(icon.id)"> | ||
| {{ icon.label }} | ||
| </NcActionRadio> | ||
| </template> | ||
|
|
||
| <fieldset class="rating" :disabled="!readOnly"> | ||
| <legend class="hidden-visually"> | ||
| {{ text || t('forms', 'Rating') }} | ||
| </legend> | ||
| <label | ||
| v-for="value in optionsHighest" | ||
| :key="value" | ||
| class="rating__icon" | ||
| :class="{ 'rating__icon--on': value <= currentValue }"> | ||
| <input | ||
| class="hidden-visually" | ||
| type="radio" | ||
| :name="`rating_${id}`" | ||
| :aria-label=" | ||
| n('forms', '%n of {max}', '%n of {max}', value, { | ||
| max: optionsHighest, | ||
| }) | ||
| " | ||
| :value="value" | ||
| :checked="value === currentValue" | ||
| :required="isRequired && !currentValue" | ||
| @change="onPick(value)" /> | ||
| <NcIconSvgWrapper | ||
| :svg="value <= currentValue ? filledIcon : outlineIcon" /> | ||
| </label> | ||
| <NcButton | ||
| v-if="readOnly && currentValue" | ||
| variant="tertiary" | ||
| @click="onPick(0)"> | ||
| {{ t('forms', 'Clear') }} | ||
| </NcButton> | ||
| </fieldset> | ||
| </Question> | ||
| </template> | ||
|
|
||
| <script lang="ts"> | ||
| import IconHeartFilled from '@material-symbols/svg-400/outlined/favorite-fill.svg?raw' | ||
| import IconHeart from '@material-symbols/svg-400/outlined/favorite.svg?raw' | ||
| import IconStarFilled from '@material-symbols/svg-400/outlined/star-fill.svg?raw' | ||
| import IconStar from '@material-symbols/svg-400/outlined/star.svg?raw' | ||
| import IconThumbFilled from '@material-symbols/svg-400/outlined/thumb_up-fill.svg?raw' | ||
| import IconThumb from '@material-symbols/svg-400/outlined/thumb_up.svg?raw' | ||
| import { n, t } from '@nextcloud/l10n' | ||
| import { computed, defineComponent } from 'vue' | ||
| import NcActionInput from '@nextcloud/vue/components/NcActionInput' | ||
| import NcActionRadio from '@nextcloud/vue/components/NcActionRadio' | ||
| import NcButton from '@nextcloud/vue/components/NcButton' | ||
| import NcIconSvgWrapper from '@nextcloud/vue/components/NcIconSvgWrapper' | ||
| import Question from './Question.vue' | ||
| import { | ||
| QUESTION_EMITS, | ||
| QUESTION_PROPS, | ||
| useQuestion, | ||
| } from '../../composables/useQuestion.ts' | ||
|
|
||
| /** Matches the linear scale default the server assumes when optionsHighest is unset. */ | ||
| const DEFAULT_OPTIONS_HIGHEST = 5 | ||
|
|
||
| export default defineComponent({ | ||
| name: 'QuestionRating', | ||
|
|
||
| components: { | ||
| NcActionInput, | ||
| NcActionRadio, | ||
| NcButton, | ||
| NcIconSvgWrapper, | ||
| Question, | ||
| }, | ||
|
|
||
| props: QUESTION_PROPS, | ||
| emits: [...QUESTION_EMITS, 'update:values'], | ||
|
|
||
| setup(props, { emit }) { | ||
| const question = useQuestion(props, { emit }) | ||
|
|
||
| const extraSettings = computed( | ||
| () => (props.extraSettings as Record<string, unknown> | undefined) ?? {}, | ||
| ) | ||
|
|
||
| const optionsHighest = computed<number>(() => { | ||
| const configured = extraSettings.value.optionsHighest | ||
| return typeof configured === 'number' | ||
| && configured >= 2 | ||
| && configured <= 10 | ||
| ? configured | ||
| : DEFAULT_OPTIONS_HIGHEST | ||
| }) | ||
|
|
||
| const ratingIcon = computed<string>(() => { | ||
| const icon = extraSettings.value.ratingIcon | ||
| return typeof icon === 'string' | ||
| && ['star', 'heart', 'thumb'].includes(icon) | ||
| ? icon | ||
| : 'star' | ||
| }) | ||
|
|
||
| const iconChoices = computed(() => [ | ||
| { id: 'star', label: t('forms', 'Stars') }, | ||
| { id: 'heart', label: t('forms', 'Hearts') }, | ||
| { id: 'thumb', label: t('forms', 'Thumbs up') }, | ||
| ]) | ||
|
|
||
| const outlineIcon = computed( | ||
| () => | ||
| ({ star: IconStar, heart: IconHeart, thumb: IconThumb })[ | ||
| ratingIcon.value | ||
| ], | ||
| ) | ||
|
|
||
| const filledIcon = computed( | ||
| () => | ||
| ({ | ||
| star: IconStarFilled, | ||
| heart: IconHeartFilled, | ||
| thumb: IconThumbFilled, | ||
| })[ratingIcon.value], | ||
| ) | ||
|
|
||
| const currentValue = computed<number>( | ||
| () => parseInt((props.values as string[])?.[0]) || 0, | ||
| ) | ||
|
|
||
| /** | ||
| * @param value the chosen count, or 0 to clear the answer | ||
| */ | ||
| function onPick(value: number): void { | ||
| emit('update:values', value ? [String(value)] : []) | ||
| question.errorMessage.value = null | ||
| } | ||
|
|
||
| /** | ||
| * @param value how many icons to offer | ||
| */ | ||
| function onOptionsHighestChange(value: number): void { | ||
| question.onExtraSettingsChange({ | ||
| optionsHighest: value === DEFAULT_OPTIONS_HIGHEST ? null : value, | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * @param icon the chosen icon set | ||
| */ | ||
| function onRatingIconChange(icon: string): void { | ||
| question.onExtraSettingsChange({ | ||
| ratingIcon: icon === 'star' ? null : icon, | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * A rating cannot be partly filled in, so the only failure is a required | ||
| * question left unanswered. | ||
| */ | ||
| async function validate(): Promise<boolean> { | ||
| if (props.isRequired && !currentValue.value) { | ||
| question.errorMessage.value = t( | ||
| 'forms', | ||
| 'You must answer this question', | ||
| ) | ||
| return false | ||
| } | ||
| question.errorMessage.value = null | ||
| return true | ||
| } | ||
|
|
||
| return { | ||
| ...question, | ||
| currentValue, | ||
| filledIcon, | ||
| iconChoices, | ||
| optionsHighest, | ||
| n, | ||
| onOptionsHighestChange, | ||
| onPick, | ||
| onRatingIconChange, | ||
| outlineIcon, | ||
| ratingIcon, | ||
| t, | ||
| validate, | ||
| } | ||
| }, | ||
| }) | ||
| </script> | ||
|
|
||
| <style lang="scss" scoped> | ||
| .rating { | ||
| align-items: center; | ||
| border: none; | ||
| display: flex; | ||
| // Ten icons plus a Clear button do not fit one line on a narrow screen. | ||
| flex-wrap: wrap; | ||
| gap: 2px; | ||
| margin: 0; | ||
| padding: 0; | ||
|
|
||
| &__icon { | ||
| align-items: center; | ||
| border-radius: var(--border-radius); | ||
| color: var(--color-text-maxcontrast); | ||
| cursor: pointer; | ||
| display: inline-flex; | ||
| justify-content: center; | ||
| // A comfortable pointer target; the icon itself stays small. | ||
| min-height: var(--default-clickable-area); | ||
| min-width: var(--default-clickable-area); | ||
| transition: | ||
| color 0.1s ease-in-out, | ||
| transform 0.1s ease-in-out; | ||
|
|
||
| &--on { | ||
| color: var(--color-favorite, var(--color-warning)); | ||
| } | ||
|
|
||
| &:hover { | ||
| transform: scale(1.1); | ||
| } | ||
|
|
||
| &:focus-within { | ||
| outline: 2px solid var(--color-primary-element); | ||
| outline-offset: -2px; | ||
| } | ||
|
|
||
| @media (prefers-reduced-motion: reduce) { | ||
| transition: none; | ||
|
|
||
| &:hover { | ||
| transform: none; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| &:disabled &__icon { | ||
| cursor: default; | ||
| } | ||
| } | ||
| </style> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.