calcite-rating.entry.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.82
  5. */
  6. import { r as registerInstance, c as createEvent, h, F as Fragment, g as getElement } from './index-8ece2564.js';
  7. import { g as guid } from './guid-b4461004.js';
  8. import { c as connectLabel, d as disconnectLabel } from './label-50132b90.js';
  9. import { c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form-bca481e1.js';
  10. import { u as updateHostInteraction } from './interactive-cb5bf285.js';
  11. import './dom-da697a3f.js';
  12. const TEXT = {
  13. rating: "Rating",
  14. stars: "Stars: ${num}"
  15. };
  16. const ratingCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{position:relative;display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center;width:-moz-fit-content;width:-webkit-fit-content;width:fit-content}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host([scale=s]){height:1.5rem;--calcite-rating-spacing-unit:0.25rem}:host([scale=m]){height:2rem;--calcite-rating-spacing-unit:0.5rem}:host([scale=l]){height:2.75rem;--calcite-rating-spacing-unit:0.75rem}:host([read-only]){pointer-events:none}.fieldset{margin:0px;display:-ms-flexbox;display:flex;border-width:0px;padding:0px}.wrapper{display:inline-block;-webkit-margin-end:var(--calcite-rating-spacing-unit);margin-inline-end:var(--calcite-rating-spacing-unit)}.star{position:relative;display:-ms-flexbox;display:flex;cursor:pointer;outline-offset:0;outline-color:transparent;-webkit-transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;color:var(--calcite-ui-border-input);-webkit-transition:var(--calcite-animation-timing);transition:var(--calcite-animation-timing);-webkit-transform:scale(1);transform:scale(1)}.star:active{-webkit-transform:scale(1.1);transform:scale(1.1)}.focused{outline:2px solid var(--calcite-ui-brand);outline-offset:2px}.average,.fraction{color:var(--calcite-ui-warning)}.hovered,.selected,:host([read-only]) .average,:host([read-only]) .fraction{color:var(--calcite-ui-brand)}.hovered:not(.selected){-webkit-transform:scale(0.9);transform:scale(0.9)}:host .fraction{pointer-events:none;position:absolute;top:0px;overflow:hidden;-webkit-transition:var(--calcite-animation-timing);transition:var(--calcite-animation-timing);inset-inline-start:0}calcite-chip{pointer-events:none;cursor:default}.number--average{font-weight:var(--calcite-font-weight-bold)}.number--count{color:var(--calcite-ui-text-2);font-style:italic}.number--count:not(:first-child){-webkit-margin-start:var(--calcite-rating-spacing-unit);margin-inline-start:var(--calcite-rating-spacing-unit)}.visually-hidden{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;border-width:0}::slotted(input[slot=hidden-form-input]){bottom:0 !important;left:0 !important;margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;right:0 !important;top:0 !important;-webkit-transform:none !important;transform:none !important;-webkit-appearance:none !important;z-index:-1 !important}";
  17. const Rating = class {
  18. constructor(hostRef) {
  19. registerInstance(this, hostRef);
  20. this.calciteRatingChange = createEvent(this, "calciteRatingChange", 7);
  21. // --------------------------------------------------------------------------
  22. //
  23. // Properties
  24. //
  25. // --------------------------------------------------------------------------
  26. /** specify the scale of the component, defaults to m */
  27. this.scale = "m";
  28. /** the value of the rating component */
  29. this.value = 0;
  30. /** is the rating component in a selectable mode */
  31. this.readOnly = false;
  32. /** is the rating component in a selectable mode */
  33. this.disabled = false;
  34. /** Show average and count data summary chip (if available) */
  35. this.showChip = false;
  36. /** Localized string for "Rating" (used for aria label)
  37. * @default "Rating"
  38. */
  39. this.intlRating = TEXT.rating;
  40. /** Localized string for labelling each star, `${num}` in the string will be replaced by the number
  41. * @default "Stars: ${num}"
  42. */
  43. this.intlStars = TEXT.stars;
  44. /**
  45. * When true, makes the component required for form-submission.
  46. *
  47. * @internal
  48. */
  49. this.required = false;
  50. this.guid = `calcite-ratings-${guid()}`;
  51. }
  52. //--------------------------------------------------------------------------
  53. //
  54. // Lifecycle
  55. //
  56. //--------------------------------------------------------------------------
  57. connectedCallback() {
  58. connectLabel(this);
  59. connectForm(this);
  60. }
  61. disconnectedCallback() {
  62. disconnectLabel(this);
  63. disconnectForm(this);
  64. }
  65. componentDidRender() {
  66. updateHostInteraction(this);
  67. }
  68. //--------------------------------------------------------------------------
  69. //
  70. // Event Listeners
  71. //
  72. //--------------------------------------------------------------------------
  73. blurHandler() {
  74. this.hasFocus = false;
  75. }
  76. // --------------------------------------------------------------------------
  77. //
  78. // Render Methods
  79. //
  80. // --------------------------------------------------------------------------
  81. renderStars() {
  82. return [1, 2, 3, 4, 5].map((i) => {
  83. const selected = this.value >= i;
  84. const average = this.average && !this.value && i <= this.average;
  85. const hovered = i <= this.hoverValue;
  86. const fraction = this.average && this.average + 1 - i;
  87. const partial = !this.value && !hovered && fraction > 0 && fraction < 1;
  88. const focused = this.hasFocus && this.focusValue === i;
  89. return (h("span", { class: { wrapper: true } }, h("label", { class: { star: true, focused, selected, average, hovered, partial }, htmlFor: `${this.guid}-${i}`, onMouseOver: () => {
  90. this.hoverValue = i;
  91. } }, h("calcite-icon", { "aria-hidden": "true", class: "icon", icon: selected || average || this.readOnly ? "star-f" : "star", scale: this.scale }), partial && (h("div", { class: "fraction", style: { width: `${fraction * 100}%` } }, h("calcite-icon", { icon: "star-f", scale: this.scale }))), h("span", { class: "visually-hidden" }, this.intlStars.replace("${num}", `${i}`))), h("input", { checked: i === this.value, class: "visually-hidden", disabled: this.disabled || this.readOnly, id: `${this.guid}-${i}`, name: this.guid, onChange: () => this.updateValue(i), onClick: (event) =>
  92. // click is fired from the the component's label, so we treat this as an internal event
  93. event.stopPropagation(), onFocus: () => {
  94. this.hasFocus = true;
  95. this.focusValue = i;
  96. }, ref: (el) => (i === 1 || i === this.value) && (this.inputFocusRef = el), type: "radio", value: i })));
  97. });
  98. }
  99. render() {
  100. const { disabled, intlRating, showChip, scale, count, average } = this;
  101. return (h(Fragment, null, h("fieldset", { class: "fieldset", disabled: disabled, onBlur: () => (this.hoverValue = null), onMouseLeave: () => (this.hoverValue = null), onTouchEnd: () => (this.hoverValue = null) }, h("legend", { class: "visually-hidden" }, intlRating), this.renderStars()), (count || average) && showChip ? (h("calcite-chip", { scale: scale, value: count === null || count === void 0 ? void 0 : count.toString() }, !!average && h("span", { class: "number--average" }, average.toString()), !!count && h("span", { class: "number--count" }, "(", count === null || count === void 0 ? void 0 :
  102. count.toString(), ")"))) : null, h(HiddenFormInputSlot, { component: this })));
  103. }
  104. //--------------------------------------------------------------------------
  105. //
  106. // Private Methods
  107. //
  108. //--------------------------------------------------------------------------
  109. onLabelClick() {
  110. this.setFocus();
  111. }
  112. updateValue(value) {
  113. this.value = value;
  114. this.calciteRatingChange.emit({ value });
  115. }
  116. //--------------------------------------------------------------------------
  117. //
  118. // Public Methods
  119. //
  120. //--------------------------------------------------------------------------
  121. /** Sets focus on the component. */
  122. async setFocus() {
  123. this.inputFocusRef.focus();
  124. }
  125. get el() { return getElement(this); }
  126. };
  127. Rating.style = ratingCss;
  128. export { Rating as calcite_rating };