@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
546 lines (545 loc) • 17 kB
JavaScript
/*!
* All material copyright ESRI, All Rights Reserved, unless otherwise specified.
* See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
* v1.5.0-next.4
*/
import { h, Host } from "@stencil/core";
import { connectForm, disconnectForm, HiddenFormInputSlot } from "../../utils/form";
import { guid } from "../../utils/guid";
import { connectInteractive, disconnectInteractive, updateHostInteraction } from "../../utils/interactive";
import { connectLabel, disconnectLabel } from "../../utils/label";
import { componentLoaded, setComponentLoaded, setUpLoadableComponent } from "../../utils/loadable";
import { connectLocalized, disconnectLocalized } from "../../utils/locale";
import { connectMessages, disconnectMessages, setUpMessages, updateMessages } from "../../utils/t9n";
import { StarIcon } from "./function/star";
export class Rating {
constructor() {
this.handleRatingPointerOver = () => {
this.isKeyboardInteraction = false;
};
this.handleRatingPointerOut = () => {
this.isKeyboardInteraction = true;
this.hoverValue = null;
this.focusValue = null;
this.hasFocus = false;
};
this.handleRatingFocusIn = () => {
const selectedInput = this.value > 0 ? this.value - 1 : 0;
const focusInput = this.inputRefs[selectedInput];
const focusValue = Number(focusInput.value);
focusInput.select();
this.focusValue = focusValue;
this.hoverValue = focusValue;
this.hasFocus = true;
};
this.handleRatingFocusLeave = () => {
this.focusValue = null;
this.hoverValue = null;
this.hasFocus = false;
};
this.handleHostKeyDown = () => {
this.isKeyboardInteraction = true;
};
this.handleInputKeyDown = (event) => {
const target = event.currentTarget;
const inputVal = Number(target.value);
const key = event.key;
const numberKey = key == " " ? undefined : Number(key);
this.emit = true;
if (isNaN(numberKey)) {
switch (key) {
case "Enter":
case " ":
this.value = !this.required && this.value === inputVal ? 0 : inputVal;
break;
case "ArrowLeft":
this.value = inputVal - 1;
break;
case "ArrowRight":
this.value = inputVal + 1;
break;
case "Tab":
if (this.hasFocus) {
this.hasFocus = false;
this.focusValue = null;
this.hoverValue = null;
}
default:
break;
}
}
else {
if (!this.required && numberKey >= 0 && numberKey <= this.max) {
this.value = numberKey;
}
else if (this.required && numberKey > 0 && numberKey <= this.max) {
this.value = numberKey;
}
}
};
this.handleInputChange = (event) => {
if (this.isKeyboardInteraction === true) {
const inputVal = Number(event.target["value"]);
this.focusValue = inputVal;
this.hoverValue = inputVal;
this.value = inputVal;
}
};
this.handleLabelPointerOver = (event) => {
const target = event.currentTarget;
const newPointerValue = Number(target.firstChild["value"] || 0);
this.hoverValue = newPointerValue;
this.focusValue = null;
};
this.handleLabelPointerDown = (event) => {
const target = event.currentTarget;
const inputVal = Number(target.firstChild["value"] || 0);
this.focusValue = null;
this.hoverValue = null;
this.emit = true;
this.value = !this.required && this.value === inputVal ? 0 : inputVal;
};
this.emit = false;
this.guid = `calcite-ratings-${guid()}`;
this.isKeyboardInteraction = true;
this.max = 5;
this.average = undefined;
this.count = undefined;
this.disabled = false;
this.form = undefined;
this.messages = undefined;
this.messageOverrides = undefined;
this.name = undefined;
this.readOnly = false;
this.required = false;
this.scale = "m";
this.showChip = false;
this.value = 0;
this.effectiveLocale = "";
this.defaultMessages = undefined;
this.hoverValue = undefined;
this.focusValue = undefined;
this.hasFocus = undefined;
}
onMessagesChange() {
/* wired up by t9n util */
}
handleValueUpdate(newValue) {
this.hoverValue = newValue;
this.focusValue = newValue;
if (this.emit) {
this.calciteRatingChange.emit();
}
this.emit = false;
}
effectiveLocaleChange() {
updateMessages(this, this.effectiveLocale);
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback() {
connectInteractive(this);
connectLocalized(this);
connectMessages(this);
connectLabel(this);
connectForm(this);
}
async componentWillLoad() {
await setUpMessages(this);
setUpLoadableComponent(this);
this.inputRefs = Array(this.max);
}
componentWillRender() {
this.starsMap = Array.from({ length: this.max }, (_, i) => {
const value = i + 1;
const average = !this.focusValue &&
!this.hoverValue &&
this.average &&
!this.value &&
value <= this.average;
const checked = value === this.value;
const focused = this.isKeyboardInteraction && this.hasFocus && this.focusValue === value;
const fraction = this.average && this.average + 1 - value;
const hovered = value <= this.hoverValue;
const id = `${this.guid}-${value}`;
const partial = !this.focusValue &&
!this.hoverValue &&
!this.value &&
!hovered &&
fraction > 0 &&
fraction < 1;
const selected = this.value >= value;
return {
average,
checked,
focused,
fraction,
hovered,
id,
idx: i,
partial,
selected,
value
};
});
}
componentDidLoad() {
setComponentLoaded(this);
}
disconnectedCallback() {
disconnectInteractive(this);
disconnectLocalized(this);
disconnectMessages(this);
disconnectLabel(this);
disconnectForm(this);
}
componentDidRender() {
updateHostInteraction(this);
}
render() {
return (h(Host, { onBlur: this.handleRatingFocusLeave, onFocus: this.handleRatingFocusIn, onKeyDown: this.handleHostKeyDown, onPointerOut: this.handleRatingPointerOut, onPointerOver: this.handleRatingPointerOver }, h("span", { class: "wrapper" }, h("fieldset", { class: "fieldset", disabled: this.disabled }, h("legend", { class: "visually-hidden" }, this.messages.rating), this.starsMap.map(({ average, checked, focused, fraction, hovered, id, idx, partial, selected, value }) => {
return (h("label", { class: {
star: true,
focused,
selected,
hovered,
average,
partial
}, htmlFor: id, onPointerDown: this.handleLabelPointerDown, onPointerOver: this.handleLabelPointerOver }, h("input", { checked: checked, class: "visually-hidden", disabled: this.disabled || this.readOnly, id: id, name: this.guid, onChange: this.handleInputChange, onKeyDown: this.handleInputKeyDown, type: "radio", value: value,
// eslint-disable-next-line react/jsx-sort-props
ref: (el) => {
this.inputRefs[idx] = el;
return ((value === 1 || value === this.value) &&
(this.inputFocusRef = el));
} }), h(StarIcon, { full: selected || average, scale: this.scale }), partial && (h("div", { class: "fraction", style: { width: `${fraction * 100}%` } }, h(StarIcon, { full: true, partial: true, scale: this.scale }))), h("span", { class: "visually-hidden" }, this.messages.stars.replace("${num}", `${value}`))));
}), (this.count || this.average) && this.showChip ? (h("calcite-chip", { scale: this.scale, value: this.count?.toString() }, !!this.average && h("span", { class: "number--average" }, this.average.toString()), !!this.count && h("span", { class: "number--count" }, "(", this.count?.toString(), ")"))) : null), h(HiddenFormInputSlot, { component: this }))));
}
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
onLabelClick() {
this.setFocus();
}
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/** Sets focus on the component. */
async setFocus() {
await componentLoaded(this);
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 assetsDirs() { return ["assets"]; }
static get properties() {
return {
"average": {
"type": "number",
"mutable": false,
"complexType": {
"original": "number",
"resolved": "number",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Specifies a cumulative average from previous ratings to display."
},
"attribute": "average",
"reflect": true
},
"count": {
"type": "number",
"mutable": false,
"complexType": {
"original": "number",
"resolved": "number",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Specifies the number of previous ratings to display."
},
"attribute": "count",
"reflect": true
},
"disabled": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "When `true`, interaction is prevented and the component is displayed with lower opacity."
},
"attribute": "disabled",
"reflect": true,
"defaultValue": "false"
},
"form": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "The ID of the form that will be associated with the component.\n\nWhen not set, the component will be associated with its ancestor form element, if any."
},
"attribute": "form",
"reflect": true
},
"messages": {
"type": "unknown",
"mutable": true,
"complexType": {
"original": "RatingMessages",
"resolved": "{ rating: string; stars: string; }",
"references": {
"RatingMessages": {
"location": "import",
"path": "./assets/rating/t9n"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": "Made into a prop for testing purposes only"
}
},
"messageOverrides": {
"type": "unknown",
"mutable": true,
"complexType": {
"original": "Partial<RatingMessages>",
"resolved": "{ rating?: string; stars?: string; }",
"references": {
"Partial": {
"location": "global"
},
"RatingMessages": {
"location": "import",
"path": "./assets/rating/t9n"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Use this property to override individual strings used by the component."
}
},
"name": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Specifies the name of the component.\n\nRequired to pass the component's `value` on form submission."
},
"attribute": "name",
"reflect": true
},
"readOnly": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "When `true`, the component's value can be read, but cannot be modified."
},
"attribute": "read-only",
"reflect": true,
"defaultValue": "false"
},
"required": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": "When `true`, the component must have a value in order for the form to submit."
},
"attribute": "required",
"reflect": true,
"defaultValue": "false"
},
"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": "Specifies the size of the component."
},
"attribute": "scale",
"reflect": true,
"defaultValue": "\"m\""
},
"showChip": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "When `true`, and if available, displays the `average` and/or `count` data summary in a `calcite-chip`."
},
"attribute": "show-chip",
"reflect": true,
"defaultValue": "false"
},
"value": {
"type": "number",
"mutable": true,
"complexType": {
"original": "number",
"resolved": "number",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "The component's value."
},
"attribute": "value",
"reflect": true,
"defaultValue": "0"
}
};
}
static get states() {
return {
"effectiveLocale": {},
"defaultMessages": {},
"hoverValue": {},
"focusValue": {},
"hasFocus": {}
};
}
static get events() {
return [{
"method": "calciteRatingChange",
"name": "calciteRatingChange",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [],
"text": "Fires when the component's value changes."
},
"complexType": {
"original": "void",
"resolved": "void",
"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 watchers() {
return [{
"propName": "messageOverrides",
"methodName": "onMessagesChange"
}, {
"propName": "value",
"methodName": "handleValueUpdate"
}, {
"propName": "effectiveLocale",
"methodName": "effectiveLocaleChange"
}];
}
}