Skip to content
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

Ratings Tweaks #1531

Open
wants to merge 2 commits into
base: esds-3.0-vue3-primevue
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 70 additions & 51 deletions es-ds-components/components/es-rating.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,73 +44,77 @@ const props = defineProps({
},
});

const localRating = ref(props.rating);
const model = defineModel<number>()
model.value = props.rating;

const roundedRating = computed(() => {
if (!props.rounded) {
return localRating.value;
}
// Rounds to nearest .5
return Math.round(localRating.value * 2) / 2;
});
// Rounds to nearest .5
const round = (value: number) => value ? Math.round(value * 2) / 2 : 0;
const localRating = computed(() => props.rounded ? round(model.value as number) : model.value || 0);

const update = (value: number) => {
localRating.value = value;
};
const showFocus = ref(false);
</script>

<template>
<div
v-if="readOnly"
:aria-label="`${roundedRating} out of 5 stars`"
v-if="props.readOnly"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need props. and can use the props explicitly

:aria-label="`${localRating} out of 5 stars`"
class="bg-transparent rounded-0 text-orange rating">
<div
v-for="i in 5"
:key="i"
aria-hidden="true">
<span v-if="i <= roundedRating">
<span v-if="i <= localRating">
<icon-star-full
:width="width"
:height="height" />
:width="props.width"
:height="props.height" />
</span>
<span v-else-if="i - 0.5 === roundedRating">
<span v-else-if="i - 0.5 === localRating">
<icon-star-half
:width="width"
:height="height" />
:width="props.width"
:height="props.height" />
</span>
<span v-else>
<icon-star-empty
:width="width"
:height="height" />
:width="props.width"
:height="props.height" />
</span>
</div>
</div>
<rating
v-else
:model-value="roundedRating"
:cancel="false"
:readonly="readOnly"
v-bind="$attrs"
:pt="{
root: {
class: 'bg-transparent rounded-0 text-orange rating reactive',
},
item: {
class: 'reactiveStar',
},
}"
@update:model-value="update">
<template #officon>
<icon-star-empty
:width="width"
:height="height" />
</template>
<template #onicon>
<icon-star-full
:width="width"
:height="height" />
</template>
</rating>
<div v-else>
<rating
v-model="model"
:cancel="false"
:readonly="props.readOnly"
v-bind="$attrs"
:pt="{
root: {
class: 'bg-transparent rounded-0 text-orange rating reactive',
},
item: (options) => {
return {
class: [{
'reactiveStar': true,
'focused': options.context.focused,
'fade-focus': options.context.active && options.context.focused,
}]
}
},
}"
@focus="showFocus = true"
@blur="showFocus = false">
<template #officon>
<icon-star-empty
:width="props.width"
:height="props.height" />
</template>
<template #onicon>
<icon-star-full
:width="props.width"
:height="props.height" />
</template>
</rating>
showFocus: {{ showFocus }}
</div>
</template>

<style lang="scss">
Expand All @@ -130,9 +134,24 @@ const update = (value: number) => {
cursor: pointer !important;
}

// TODO: Star should go back to normal size after click
.reactiveStar:hover,
.reactiveStar[data-p-focused='true'] {
transform: scale(1.5);
.reactiveStar {
transition: all 0.15s ease-in-out;

&:hover {
transform: scale(1.5);

}
&.focused {
transform: scale(1.5);
}
}

.rating {
.reactiveStar.focused,
.reactiveStar:hover {
.reactiveStar {
transform: scale(1.5);
}
}
}
</style>
34 changes: 17 additions & 17 deletions es-ds-docs/pages/molecules/rating.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<script setup lang="ts">
// Event will trigger twice when using keyboard to focus due to PrimeVue bug
const changeEvent = ($event) => {
// eslint-disable-next-line no-alert
alert($event.value);
};

const propTableRows = [
['rating', 'Number', '0', 'Starting rating value 0-5, with .5 values available in read only mode'],
['rounded', 'Boolean', 'true', 'Round rating to nearest .5'],
Expand All @@ -26,6 +20,8 @@ onMounted(async () => {
$prism.highlight();
}
});

const rating1Val = ref(0);
</script>

<template>
Expand All @@ -42,24 +38,28 @@ onMounted(async () => {
<div class="my-500">
<h2>Form Input</h2>
<es-rating
:read-only="false"
@change="changeEvent" />
v-model="rating1Val"
:read-only="false" />
<h2 class="mt-500">Static Display</h2>
<div
v-for="i in 11"
:key="i">
<es-rating :rating="(i - 1) / 2" />
</div>
</div>
<div class="d-flex flex-column flex-md-row">
<div class="bg-gray-50 justify-content-center d-flex p-200 mb-200 mb-md-0 mr-md-200">
<es-rating :rating="4.5" />
</div>
<div class="bg-blue-50 justify-content-center d-flex p-200 mb-200 mb-md-0 mr-md-200">
<es-rating :rating="4.5" />
</div>
<div class="bg-blue-900 justify-content-center d-flex p-200 mb-200 mb-md-0 mr-md-200">
<es-rating :rating="4.5" />

<div class="my-500">
<h2>Background Variations</h2>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentence case - Background variations

<div class="d-flex flex-column flex-md-row">
<div class="bg-gray-50 justify-content-center d-flex p-200 mb-200 mb-md-0 mr-md-200">
<es-rating :rating="4.5" />
</div>
<div class="bg-blue-50 justify-content-center d-flex p-200 mb-200 mb-md-0 mr-md-200">
<es-rating :rating="4.5" />
</div>
<div class="bg-blue-900 justify-content-center d-flex p-200 mb-200 mb-md-0 mr-md-200">
<es-rating :rating="4.5" />
</div>
</div>
</div>

Expand Down
Loading