UNPKG

@progress/kendo-react-inputs

Version:

React Inputs offer a customizable interface for users to enter and pick different information. KendoReact Input package

317 lines (316 loc) 10.7 kB
/** * @license *------------------------------------------------------------------------------------------- * Copyright © 2025 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the package root for more information *------------------------------------------------------------------------------------------- */ import * as a from "react"; import r from "prop-types"; import { createPropsContext as te, usePropsContext as ae, useDir as ne, Keys as R, IconWrap as y, toIconName as B, getTabIndex as le, classNames as oe, svgIconPropType as S } from "@progress/kendo-react-common"; import { starOutlineIcon as $, starIcon as K } from "@progress/kendo-svg-icons"; import { RatingItem as se } from "./RatingItem.mjs"; import { RATING_ACTION as c, ratingReducer as ie } from "./rating-reducer.mjs"; import { calcIsFirstHalf as L, toRound as D, getRemainder as re, isHalf as ce, isSelected as F, isCorrectValue as A } from "./utils/index.mjs"; import { ratingAriaLabel as X, messages as ue } from "../messages/index.mjs"; import { useLocalization as me } from "@progress/kendo-react-intl"; const q = (n, g, v) => { const [e, N] = a.useState(n); return [e, (h) => { const s = ie(g.state || e, { ...h, ...g }); v && v(s, h.event), N(s); }]; }, de = te(), G = a.forwardRef((n, g) => { const v = ae(de, n), e = { dir: o.dir, min: o.min, max: o.max, step: o.step, item: o.item, defaultValue: o.defaultValue, readonly: o.readonly, disabled: o.disabled, selection: o.selection, precision: o.precision, ...v }, N = me(), m = a.useRef(null), h = a.useRef(null), s = ne(h, e.dir); a.useImperativeHandle(m, () => ({ element: h.current, props: e })), a.useImperativeHandle(g, () => m.current); const W = a.useCallback( (t, l) => { e.onChange && m.current && e.onChange.call(void 0, { value: t, target: m.current, syntheticEvent: l }); }, [e.onChange] ), I = a.useMemo(() => e.min !== void 0 ? e.min : o.min, [e.min]), x = a.useMemo(() => e.max !== void 0 ? e.max : o.max, [e.max]), d = a.useMemo(() => e.precision || o.precision, [e.precision]), i = a.useMemo(() => e.step !== void 0 ? e.step : o.step, [e.step]), V = a.useMemo(() => e.readonly || o.readonly, [e.readonly]), k = a.useMemo(() => e.disabled || o.disabled, [e.disabled]), _ = a.useMemo(() => e.item || o.item, [e.item]), [H, p] = q( e.defaultValue !== void 0 ? e.defaultValue : o.defaultValue, { state: e.value, min: I, max: x, step: d === "half" ? i / 2 : i }, W ), [T, O] = q(null, { state: e.value, min: I, max: x, step: d === "half" ? i / 2 : i, precision: d }), u = a.useMemo( () => e.value !== void 0 ? e.value : H, [e.value, H] ), C = a.useMemo(() => T, [T]), z = a.useMemo( () => e.selection || o.selection, [e.selection] ), E = a.useMemo(() => i / (d === "half" ? 2 : 1), [i, d]), j = a.useCallback( (t) => { if (!(!t.target || !t.target.element || V || k)) { if (d === "half") { const l = t.target.element.getBoundingClientRect(), M = L(s || "ltr", l, t.syntheticEvent.clientX) ? D(t.target.value - i / 2, E) : t.target.value; p({ type: c.select, payload: M, event: t.syntheticEvent }); } else p({ type: c.select, payload: t.target.value, event: t.syntheticEvent }); if (e.onClick) { const l = { value: u, target: m.current, syntheticEvent: t.syntheticEvent }; e.onClick.call(void 0, l); } } }, [e.onClick, p, u] ), J = a.useCallback( (t) => { if (!(V || k)) { switch (t.keyCode) { case R.right: t.preventDefault(), p({ type: s === "rtl" ? c.decrease : c.increase, event: t }); break; case R.left: t.preventDefault(), p({ type: s === "rtl" ? c.increase : c.decrease, event: t }); break; case R.home: t.preventDefault(), p({ type: s === "rtl" ? c.min : c.max, event: t }); break; case R.end: t.preventDefault(), p({ type: s === "rtl" ? c.max : c.min, event: t }); break; case R.esc: t.preventDefault(), p({ type: c.deselect, event: t }); break; } e.onKeyDown && e.onKeyDown.call(void 0, { value: u, target: m.current, syntheticEvent: t }); } }, [e.onKeyDown, p, u] ), Q = a.useCallback( (t) => { if (e.onFocus) { const l = { target: m.current, syntheticEvent: t }; e.onFocus.call(void 0, l); } }, [e.onFocus] ), U = a.useCallback( (t) => { if (e.onBlur) { const l = { target: m.current, syntheticEvent: t }; e.onBlur.call(void 0, l); } }, [e.onBlur] ), Y = a.useCallback( (t) => { if (!(!t.target || !t.target.element)) if (d === "half") { const l = t.target.element.getBoundingClientRect(), M = L(s || "ltr", l, t.syntheticEvent.clientX) ? t.target.value - i / 2 : t.target.value; O({ type: c.select, payload: M, event: t.syntheticEvent }); } else O({ type: c.select, payload: t.target.value, event: t.syntheticEvent }); }, [d, i, s] ), Z = a.useCallback((t) => { O({ type: c.reset, event: t.syntheticEvent }); }, []), P = [], ee = re(D(x - I, E), i); for (let t = I; t <= x; t = D(t + i, E)) { const l = D(t + ee, E), f = d === "half" ? ce(l, C !== null ? C : u !== null ? u : 0, i) : !1, M = F(l, u, i, z), w = F(l, C !== null ? C : u, i, z), b = F(l, C, i, z); P.push( /* @__PURE__ */ a.createElement( _, { key: l, value: l, dir: s, title: String(f ? D(l - i / 2, E) : l), icon: e.icon, svgIcon: e.svgIcon, half: f, selected: M || w, hovered: b, onClick: j, onMouseMove: Y, onMouseLeave: Z }, f && /* @__PURE__ */ a.createElement("span", { className: "k-rating-precision-complement" }, !(e.icon || e.svgIconOutline) && /* @__PURE__ */ a.createElement( y, { name: "star-outline", icon: $, style: { clipPath: `${s === "rtl" ? "inset(0 50% 0 0)" : "inset(0 0 0 50%)"}` }, size: "xlarge" } ), (e.icon || e.svgIconOutline) && /* @__PURE__ */ a.createElement( y, { name: B(`${e.icon}-outline`), icon: e.svgIconOutline, size: "xlarge" } )), f && /* @__PURE__ */ a.createElement("span", { className: "k-rating-precision-part" }, !(e.icon || e.svgIcon) && /* @__PURE__ */ a.createElement( y, { name: "star", icon: K, style: { clipPath: `${s === "rtl" ? "inset(0 0 0 50%)" : "inset(0 50% 0 0)"}` }, size: "xlarge" } ), (e.icon || e.svgIcon) && /* @__PURE__ */ a.createElement( y, { name: e.icon ? B(e.icon) : void 0, icon: e.svgIcon, size: "xlarge" } )), f && /* @__PURE__ */ a.createElement("span", { style: { width: "24px", height: "24px", display: "block" } }), !f && !(e.icon || e.svgIcon) && (b || w && !b) && /* @__PURE__ */ a.createElement(y, { name: "star", icon: K, size: "xlarge" }) || !(e.icon || e.svgIcon) && !b && !w && /* @__PURE__ */ a.createElement(y, { name: "star-outline", icon: $, size: "xlarge" }) || (e.icon || e.svgIcon) && (b || w && !b) && /* @__PURE__ */ a.createElement(y, { name: e.icon, icon: e.svgIcon, size: "xlarge" }) || (e.icon || e.svgIcon) && !b && /* @__PURE__ */ a.createElement( y, { name: B(`${e.icon}-outline`), icon: e.svgIconOutline, size: "xlarge" } ) ) ); } return /* @__PURE__ */ a.createElement( "span", { id: e.id, style: e.style, ref: h, role: "slider", dir: s, tabIndex: le(e.tabIndex, e.disabled, void 0), className: oe( "k-rating", { "k-rtl": s === "rtl", "k-readonly": V, "k-disabled": k }, e.className ), onKeyDown: J, onFocus: Q, onBlur: U, "aria-valuemin": I, "aria-valuemax": x, "aria-valuenow": u !== null ? u : void 0, "aria-disabled": k ? "true" : void 0, "aria-label": N.toLanguageString(X, ue[X]), "aria-labelledby": e.ariaLabelledBy, "aria-describedby": e.ariaDescribedBy }, /* @__PURE__ */ a.createElement("input", { id: "rating", className: "k-hidden", readOnly: V, disabled: k }), /* @__PURE__ */ a.createElement("span", { className: "k-rating-container" }, P), e.label && /* @__PURE__ */ a.createElement("span", { className: "k-rating-label" }, e.label) ); }), pe = { id: r.string, dir: r.oneOf(["ltr", "rtl"]), selection: r.oneOf(["continues", "single"]), precision: r.oneOf(["item", "half"]), value: (n, g, v) => n.value && n.min && n.max && n.step && !(n.precision === "half" ? A(n.min, n.max, n.step / 2, n.value) : A(n.min, n.max, n.step, n.value)) && n.precision === "half" ? new Error( `Invalid prop + ${g} supplied to ${v}. The value of the { value } property is not correct, please check your values. ` ) : null, defaultValue: r.number, min: r.number, max: r.number, step: (n, g, v) => n.step !== void 0 && n.step <= 0 ? new Error( `Invalid prop + ${g} supplied to ${v}. The value of the { step } property is cannot be equal or less than "0", please check your values. ` ) : null, hovered: r.number, label: r.node, readonly: r.bool, disabled: r.bool, half: r.bool, icon: r.string, svgIcon: S, svgIconOutline: S, ariaDescribedBy: r.string }, o = { dir: "ltr", min: 1, max: 5, step: 1, item: se, defaultValue: null, readonly: !1, disabled: !1, selection: "continues", precision: "item" }; G.displayName = "KendoReactRating"; G.propTypes = pe; export { G as Rating, de as RatingPropsContext };