123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- /*!
- * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
- * v1.0.0-beta.82
- */
- import { Component, Element, Event, Fragment, h, Listen, Method, Prop, State } from "@stencil/core";
- import { guid } from "../../utils/guid";
- import { connectLabel, disconnectLabel } from "../../utils/label";
- import { connectForm, disconnectForm, HiddenFormInputSlot } from "../../utils/form";
- import { TEXT } from "./resources";
- import { updateHostInteraction } from "../../utils/interactive";
- export class Rating {
- constructor() {
- // --------------------------------------------------------------------------
- //
- // Properties
- //
- // --------------------------------------------------------------------------
- /** specify the scale of the component, defaults to m */
- this.scale = "m";
- /** the value of the rating component */
- this.value = 0;
- /** is the rating component in a selectable mode */
- this.readOnly = false;
- /** is the rating component in a selectable mode */
- this.disabled = false;
- /** Show average and count data summary chip (if available) */
- this.showChip = false;
- /** Localized string for "Rating" (used for aria label)
- * @default "Rating"
- */
- this.intlRating = TEXT.rating;
- /** Localized string for labelling each star, `${num}` in the string will be replaced by the number
- * @default "Stars: ${num}"
- */
- this.intlStars = TEXT.stars;
- /**
- * When true, makes the component required for form-submission.
- *
- * @internal
- */
- this.required = false;
- this.guid = `calcite-ratings-${guid()}`;
- }
- //--------------------------------------------------------------------------
- //
- // Lifecycle
- //
- //--------------------------------------------------------------------------
- connectedCallback() {
- connectLabel(this);
- connectForm(this);
- }
- disconnectedCallback() {
- disconnectLabel(this);
- disconnectForm(this);
- }
- componentDidRender() {
- updateHostInteraction(this);
- }
- //--------------------------------------------------------------------------
- //
- // Event Listeners
- //
- //--------------------------------------------------------------------------
- blurHandler() {
- this.hasFocus = false;
- }
- // --------------------------------------------------------------------------
- //
- // Render Methods
- //
- // --------------------------------------------------------------------------
- renderStars() {
- return [1, 2, 3, 4, 5].map((i) => {
- const selected = this.value >= i;
- const average = this.average && !this.value && i <= this.average;
- const hovered = i <= this.hoverValue;
- const fraction = this.average && this.average + 1 - i;
- const partial = !this.value && !hovered && fraction > 0 && fraction < 1;
- const focused = this.hasFocus && this.focusValue === i;
- return (h("span", { class: { wrapper: true } },
- h("label", { class: { star: true, focused, selected, average, hovered, partial }, htmlFor: `${this.guid}-${i}`, onMouseOver: () => {
- this.hoverValue = i;
- } },
- 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) =>
- // click is fired from the the component's label, so we treat this as an internal event
- event.stopPropagation(), onFocus: () => {
- this.hasFocus = true;
- this.focusValue = i;
- }, ref: (el) => (i === 1 || i === this.value) && (this.inputFocusRef = el), type: "radio", value: i })));
- });
- }
- render() {
- const { disabled, intlRating, showChip, scale, count, average } = this;
- 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 :
- count.toString(),
- ")"))) : null,
- h(HiddenFormInputSlot, { component: this })));
- }
- //--------------------------------------------------------------------------
- //
- // Private Methods
- //
- //--------------------------------------------------------------------------
- onLabelClick() {
- this.setFocus();
- }
- updateValue(value) {
- this.value = value;
- this.calciteRatingChange.emit({ value });
- }
- //--------------------------------------------------------------------------
- //
- // Public Methods
- //
- //--------------------------------------------------------------------------
- /** Sets focus on the component. */
- async setFocus() {
- this.inputFocusRef.focus();
- }
- static get is() { return "calcite-rating"; }
- static get encapsulation() { return "shadow"; }
- static get originalStyleUrls() { return {
- "$": ["rating.scss"]
- }; }
- static get styleUrls() { return {
- "$": ["rating.css"]
- }; }
- static get properties() { return {
- "scale": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "Scale",
- "resolved": "\"l\" | \"m\" | \"s\"",
- "references": {
- "Scale": {
- "location": "import",
- "path": "../interfaces"
- }
- }
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "specify the scale of the component, defaults to m"
- },
- "attribute": "scale",
- "reflect": true,
- "defaultValue": "\"m\""
- },
- "value": {
- "type": "number",
- "mutable": true,
- "complexType": {
- "original": "number",
- "resolved": "number",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "the value of the rating component"
- },
- "attribute": "value",
- "reflect": true,
- "defaultValue": "0"
- },
- "readOnly": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "is the rating component in a selectable mode"
- },
- "attribute": "read-only",
- "reflect": true,
- "defaultValue": "false"
- },
- "disabled": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "is the rating component in a selectable mode"
- },
- "attribute": "disabled",
- "reflect": true,
- "defaultValue": "false"
- },
- "showChip": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "Show average and count data summary chip (if available)"
- },
- "attribute": "show-chip",
- "reflect": true,
- "defaultValue": "false"
- },
- "count": {
- "type": "number",
- "mutable": false,
- "complexType": {
- "original": "number",
- "resolved": "number",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "optionally pass a number of previous ratings to display"
- },
- "attribute": "count",
- "reflect": true
- },
- "average": {
- "type": "number",
- "mutable": false,
- "complexType": {
- "original": "number",
- "resolved": "number",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "optionally pass a cumulative average rating to display"
- },
- "attribute": "average",
- "reflect": true
- },
- "name": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "The name of the rating"
- },
- "attribute": "name",
- "reflect": true
- },
- "intlRating": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [{
- "name": "default",
- "text": "\"Rating\""
- }],
- "text": "Localized string for \"Rating\" (used for aria label)"
- },
- "attribute": "intl-rating",
- "reflect": false,
- "defaultValue": "TEXT.rating"
- },
- "intlStars": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [{
- "name": "default",
- "text": "\"Stars: ${num}\""
- }],
- "text": "Localized string for labelling each star, `${num}` in the string will be replaced by the number"
- },
- "attribute": "intl-stars",
- "reflect": false,
- "defaultValue": "TEXT.stars"
- },
- "required": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": undefined
- }],
- "text": "When true, makes the component required for form-submission."
- },
- "attribute": "required",
- "reflect": true,
- "defaultValue": "false"
- }
- }; }
- static get states() { return {
- "hoverValue": {},
- "focusValue": {},
- "hasFocus": {}
- }; }
- static get events() { return [{
- "method": "calciteRatingChange",
- "name": "calciteRatingChange",
- "bubbles": true,
- "cancelable": true,
- "composed": true,
- "docs": {
- "tags": [],
- "text": "Fires when the rating value has changed."
- },
- "complexType": {
- "original": "{ value: number }",
- "resolved": "{ value: number; }",
- "references": {}
- }
- }]; }
- static get methods() { return {
- "setFocus": {
- "complexType": {
- "signature": "() => Promise<void>",
- "parameters": [],
- "references": {
- "Promise": {
- "location": "global"
- }
- },
- "return": "Promise<void>"
- },
- "docs": {
- "text": "Sets focus on the component.",
- "tags": []
- }
- }
- }; }
- static get elementRef() { return "el"; }
- static get listeners() { return [{
- "name": "blur",
- "method": "blurHandler",
- "target": undefined,
- "capture": false,
- "passive": false
- }]; }
- }
|