@equinor/roma-ui 0.1.2 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './lib/select/select';
2
2
  export * from './lib/switch/Switch';
3
+ export * from './lib/NumberField/NumberField';
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./select.js"),t=require("./switch.js");exports.Option=e.Option;exports.Select=e.Select;exports.Styled=e.Styled;exports.Switch=t.Switch;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./select.js"),t=require("./switch.js"),i=require("./number.js");exports.Option=e.Option;exports.Select=e.Select;exports.Styled=e.Styled;exports.Switch=t.Switch;exports.NumberField=i.NumberField;
package/index.mjs CHANGED
@@ -1,8 +1,10 @@
1
- import { Option as o, Select as r, Styled as p } from "./select.mjs";
2
- import { Switch as c } from "./switch.mjs";
1
+ import { Option as r, Select as t, Styled as m } from "./select.mjs";
2
+ import { Switch as f } from "./switch.mjs";
3
+ import { NumberField as l } from "./number.mjs";
3
4
  export {
4
- o as Option,
5
- r as Select,
6
- p as Styled,
7
- c as Switch
5
+ l as NumberField,
6
+ r as Option,
7
+ t as Select,
8
+ m as Styled,
9
+ f as Switch
8
10
  };
@@ -0,0 +1,3 @@
1
+ import { InputProps } from '@equinor/eds-core-react';
2
+ import { AriaNumberFieldProps } from '@react-types/numberfield';
3
+ export declare const NumberField: import('react').ForwardRefExoticComponent<InputProps & AriaNumberFieldProps & import('react').RefAttributes<unknown>>;
@@ -26,7 +26,10 @@ declare const _default: {
26
26
  disabledKeys?: Iterable<import('react-aria-components').Key> | undefined;
27
27
  onFocusChange?: ((isFocused: boolean) => void) | undefined;
28
28
  selectionBehavior?: import('react-stately').SelectionBehavior | undefined;
29
+ shouldSelectOnPressUp?: boolean | undefined;
30
+ shouldFocusOnHover?: boolean | undefined;
29
31
  onAction?: ((key: import('react-aria-components').Key) => void) | undefined;
32
+ escapeKeyBehavior?: "clearSelection" | "none" | undefined;
30
33
  shouldFocusWrap?: boolean | undefined;
31
34
  selectionMode?: import('react-aria-components').SelectionMode | undefined;
32
35
  selectedKeys?: ("all" | Iterable<import('react-aria-components').Key>) | undefined;
package/number.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const me=require("react/jsx-runtime"),u=require("react"),de=require("react-aria"),fe=require("@equinor/eds-core-react"),pe=require("./useControlledState-D0L6S_5r.js");function L(r,e=-1/0,t=1/0){return Math.min(Math.max(r,e),t)}function A(r,e){let t=r,i=e.toString(),n=i.indexOf("."),a=n>=0?i.length-n:0;if(a>0){let l=Math.pow(10,a);t=Math.round(t*l)/l}return t}function M(r,e,t,i){e=Number(e),t=Number(t);let n=(r-(isNaN(e)?0:e))%i,a=A(Math.abs(n)*2>=i?r+Math.sign(n)*(i-Math.abs(n)):r-n,i);return isNaN(e)?!isNaN(t)&&a>t&&(a=Math.floor(A(t/i,i))*i):a<e?a=e:!isNaN(t)&&a>t&&(a=e+Math.floor(A((t-e)/i,i))*i),a=A(a,i),a}const ee={badInput:!1,customError:!1,patternMismatch:!1,rangeOverflow:!1,rangeUnderflow:!1,stepMismatch:!1,tooLong:!1,tooShort:!1,typeMismatch:!1,valueMissing:!1,valid:!0},te={...ee,customError:!0,valid:!1},O={isInvalid:!1,validationDetails:ee,validationErrors:[]},be=u.createContext({}),Y="__formValidationState"+Date.now();function he(r){if(r[Y]){let{realtimeValidation:e,displayValidation:t,updateValidation:i,resetValidation:n,commitValidation:a}=r[Y];return{realtimeValidation:e,displayValidation:t,updateValidation:i,resetValidation:n,commitValidation:a}}return ge(r)}function ge(r){let{isInvalid:e,validationState:t,name:i,value:n,builtinValidation:a,validate:l,validationBehavior:o="aria"}=r;t&&(e||(e=t==="invalid"));let m=e!==void 0?{isInvalid:e,validationErrors:[],validationDetails:te}:null,V=u.useMemo(()=>{if(!l||n==null)return null;let s=$e(l,n);return k(s)},[l,n]);a!=null&&a.validationDetails.valid&&(a=void 0);let h=u.useContext(be),f=u.useMemo(()=>i?Array.isArray(i)?i.flatMap(s=>G(h[s])):G(h[i]):[],[h,i]),[g,$]=u.useState(h),[p,b]=u.useState(!1);h!==g&&($(h),b(!1));let D=u.useMemo(()=>k(p?[]:f),[p,f]),w=u.useRef(O),[x,y]=u.useState(O),v=u.useRef(O),d=()=>{if(!P)return;S(!1);let s=V||a||w.current;q(s,v.current)||(v.current=s,y(s))},[P,S]=u.useState(!1);return u.useEffect(d),{realtimeValidation:m||D||V||a||O,displayValidation:o==="native"?m||D||x:m||D||V||a||x,updateValidation(s){o==="aria"&&!q(x,s)?y(s):w.current=s},resetValidation(){let s=O;q(s,v.current)||(v.current=s,y(s)),o==="native"&&S(!1),b(!0)},commitValidation(){o==="native"&&S(!0),b(!0)}}}function G(r){return r?Array.isArray(r)?r:[r]:[]}function $e(r,e){if(typeof r=="function"){let t=r(e);if(t&&typeof t!="boolean")return G(t)}return[]}function k(r){return r.length?{isInvalid:!0,validationErrors:r,validationDetails:te}:null}function q(r,e){return r===e?!0:!!r&&!!e&&r.isInvalid===e.isInvalid&&r.validationErrors.length===e.validationErrors.length&&r.validationErrors.every((t,i)=>t===e.validationErrors[i])&&Object.entries(r.validationDetails).every(([t,i])=>e.validationDetails[t]===i)}let z=new Map,U=!1;try{U=new Intl.NumberFormat("de-DE",{signDisplay:"exceptZero"}).resolvedOptions().signDisplay==="exceptZero"}catch{}let j=!1;try{j=new Intl.NumberFormat("de-DE",{style:"unit",unit:"degree"}).resolvedOptions().style==="unit"}catch{}const ie={degree:{narrow:{default:"°","ja-JP":" 度","zh-TW":"度","sl-SI":" °"}}};class Z{format(e){let t="";if(!U&&this.options.signDisplay!=null?t=ve(this.numberFormatter,this.options.signDisplay,e):t=this.numberFormatter.format(e),this.options.style==="unit"&&!j){var i;let{unit:n,unitDisplay:a="short",locale:l}=this.resolvedOptions();if(!n)return t;let o=(i=ie[n])===null||i===void 0?void 0:i[a];t+=o[l]||o.default}return t}formatToParts(e){return this.numberFormatter.formatToParts(e)}formatRange(e,t){if(typeof this.numberFormatter.formatRange=="function")return this.numberFormatter.formatRange(e,t);if(t<e)throw new RangeError("End date must be >= start date");return`${this.format(e)} – ${this.format(t)}`}formatRangeToParts(e,t){if(typeof this.numberFormatter.formatRangeToParts=="function")return this.numberFormatter.formatRangeToParts(e,t);if(t<e)throw new RangeError("End date must be >= start date");let i=this.numberFormatter.formatToParts(e),n=this.numberFormatter.formatToParts(t);return[...i.map(a=>({...a,source:"startRange"})),{type:"literal",value:" – ",source:"shared"},...n.map(a=>({...a,source:"endRange"}))]}resolvedOptions(){let e=this.numberFormatter.resolvedOptions();return!U&&this.options.signDisplay!=null&&(e={...e,signDisplay:this.options.signDisplay}),!j&&this.options.style==="unit"&&(e={...e,style:"unit",unit:this.options.unit,unitDisplay:this.options.unitDisplay}),e}constructor(e,t={}){this.numberFormatter=ye(e,t),this.options=t}}function ye(r,e={}){let{numberingSystem:t}=e;if(t&&r.includes("-nu-")&&(r.includes("-u-")||(r+="-u-"),r+=`-nu-${t}`),e.style==="unit"&&!j){var i;let{unit:l,unitDisplay:o="short"}=e;if(!l)throw new Error('unit option must be provided with style: "unit"');if(!(!((i=ie[l])===null||i===void 0)&&i[o]))throw new Error(`Unsupported unit ${l} with unitDisplay = ${o}`);e={...e,style:"decimal"}}let n=r+(e?Object.entries(e).sort((l,o)=>l[0]<o[0]?-1:1).join():"");if(z.has(n))return z.get(n);let a=new Intl.NumberFormat(r,e);return z.set(n,a),a}function ve(r,e,t){if(e==="auto")return r.format(t);if(e==="never")return r.format(Math.abs(t));{let i=!1;if(e==="always"?i=t>0||Object.is(t,0):e==="exceptZero"&&(Object.is(t,-0)||Object.is(t,0)?t=Math.abs(t):i=t>0),i){let n=r.format(-t),a=r.format(t),l=n.replace(a,"").replace(/\u200e|\u061C/,"");return[...l].length!==1&&console.warn("@react-aria/i18n polyfill for NumberFormat signDisplay: Unsupported case"),n.replace(a,"!!!").replace(l,"+").replace("!!!",a)}else return r.format(t)}}const Ne=new RegExp("^.*\\(.*\\).*$"),Ve=["latn","arab","hanidec","deva","beng"];class re{parse(e){return W(this.locale,this.options,e).parse(e)}isValidPartialNumber(e,t,i){return W(this.locale,this.options,e).isValidPartialNumber(e,t,i)}getNumberingSystem(e){return W(this.locale,this.options,e).options.numberingSystem}constructor(e,t={}){this.locale=e,this.options=t}}const Q=new Map;function W(r,e,t){let i=J(r,e);if(!r.includes("-nu-")&&!i.isValidPartialNumber(t)){for(let n of Ve)if(n!==i.options.numberingSystem){let a=J(r+(r.includes("-u-")?"-nu-":"-u-nu-")+n,e);if(a.isValidPartialNumber(t))return a}}return i}function J(r,e){let t=r+(e?Object.entries(e).sort((n,a)=>n[0]<a[0]?-1:1).join():""),i=Q.get(t);return i||(i=new Se(r,e),Q.set(t,i)),i}class Se{parse(e){let t=this.sanitize(e);if(this.symbols.group&&(t=T(t,this.symbols.group,"")),this.symbols.decimal&&(t=t.replace(this.symbols.decimal,".")),this.symbols.minusSign&&(t=t.replace(this.symbols.minusSign,"-")),t=t.replace(this.symbols.numeral,this.symbols.index),this.options.style==="percent"){let l=t.indexOf("-");t=t.replace("-",""),t=t.replace("+","");let o=t.indexOf(".");o===-1&&(o=t.length),t=t.replace(".",""),o-2===0?t=`0.${t}`:o-2===-1?t=`0.0${t}`:o-2===-2?t="0.00":t=`${t.slice(0,o-2)}.${t.slice(o-2)}`,l>-1&&(t=`-${t}`)}let i=t?+t:NaN;if(isNaN(i))return NaN;if(this.options.style==="percent"){var n,a;let l={...this.options,style:"decimal",minimumFractionDigits:Math.min(((n=this.options.minimumFractionDigits)!==null&&n!==void 0?n:0)+2,20),maximumFractionDigits:Math.min(((a=this.options.maximumFractionDigits)!==null&&a!==void 0?a:0)+2,20)};return new re(this.locale,l).parse(new Z(this.locale,l).format(i))}return this.options.currencySign==="accounting"&&Ne.test(e)&&(i=-1*i),i}sanitize(e){return e=e.replace(this.symbols.literals,""),this.symbols.minusSign&&(e=e.replace("-",this.symbols.minusSign)),this.options.numberingSystem==="arab"&&(this.symbols.decimal&&(e=e.replace(",",this.symbols.decimal),e=e.replace("،",this.symbols.decimal)),this.symbols.group&&(e=T(e,".",this.symbols.group))),this.options.locale==="fr-FR"&&this.symbols.group&&(e=T(e," ",this.symbols.group),e=T(e,/\u00A0/g,this.symbols.group)),e}isValidPartialNumber(e,t=-1/0,i=1/0){return e=this.sanitize(e),this.symbols.minusSign&&e.startsWith(this.symbols.minusSign)&&t<0?e=e.slice(this.symbols.minusSign.length):this.symbols.plusSign&&e.startsWith(this.symbols.plusSign)&&i>0&&(e=e.slice(this.symbols.plusSign.length)),this.symbols.group&&e.startsWith(this.symbols.group)||this.symbols.decimal&&e.indexOf(this.symbols.decimal)>-1&&this.options.maximumFractionDigits===0?!1:(this.symbols.group&&(e=T(e,this.symbols.group,"")),e=e.replace(this.symbols.numeral,""),this.symbols.decimal&&(e=e.replace(this.symbols.decimal,"")),e.length===0)}constructor(e,t={}){this.locale=e,t.roundingIncrement!==1&&t.roundingIncrement!=null&&(t.maximumFractionDigits==null&&t.minimumFractionDigits==null?(t.maximumFractionDigits=0,t.minimumFractionDigits=0):t.maximumFractionDigits==null?t.maximumFractionDigits=t.minimumFractionDigits:t.minimumFractionDigits==null&&(t.minimumFractionDigits=t.maximumFractionDigits)),this.formatter=new Intl.NumberFormat(e,t),this.options=this.formatter.resolvedOptions(),this.symbols=we(e,this.formatter,this.options,t);var i,n;this.options.style==="percent"&&(((i=this.options.minimumFractionDigits)!==null&&i!==void 0?i:0)>18||((n=this.options.maximumFractionDigits)!==null&&n!==void 0?n:0)>18)&&console.warn("NumberParser cannot handle percentages with greater than 18 decimal places, please reduce the number in your options.")}}const X=new Set(["decimal","fraction","integer","minusSign","plusSign","group"]),De=[0,4,2,1,11,20,3,7,100,21,.1,1.1];function we(r,e,t,i){var n,a,l,o;let m=new Intl.NumberFormat(r,{...t,minimumSignificantDigits:1,maximumSignificantDigits:21,roundingIncrement:1,roundingPriority:"auto",roundingMode:"halfExpand"}),V=m.formatToParts(-10000.111),h=m.formatToParts(10000.111),f=De.map(s=>m.formatToParts(s));var g;let $=(g=(n=V.find(s=>s.type==="minusSign"))===null||n===void 0?void 0:n.value)!==null&&g!==void 0?g:"-",p=(a=h.find(s=>s.type==="plusSign"))===null||a===void 0?void 0:a.value;!p&&((i==null?void 0:i.signDisplay)==="exceptZero"||(i==null?void 0:i.signDisplay)==="always")&&(p="+");let D=(l=new Intl.NumberFormat(r,{...t,minimumFractionDigits:2,maximumFractionDigits:2}).formatToParts(.001).find(s=>s.type==="decimal"))===null||l===void 0?void 0:l.value,w=(o=V.find(s=>s.type==="group"))===null||o===void 0?void 0:o.value,x=V.filter(s=>!X.has(s.type)).map(s=>H(s.value)),y=f.flatMap(s=>s.filter(E=>!X.has(E.type)).map(E=>H(E.value))),v=[...new Set([...x,...y])].sort((s,E)=>E.length-s.length),d=v.length===0?new RegExp("[\\p{White_Space}]","gu"):new RegExp(`${v.join("|")}|[\\p{White_Space}]`,"gu"),P=[...new Intl.NumberFormat(t.locale,{useGrouping:!1}).format(9876543210)].reverse(),S=new Map(P.map((s,E)=>[s,E])),F=new RegExp(`[${P.join("")}]`,"g");return{minusSign:$,plusSign:p,decimal:D,group:w,literals:d,numeral:F,index:s=>String(S.get(s))}}function T(r,e,t){return r.replaceAll?r.replaceAll(e,t):r.split(e).join(t)}function H(r){return r.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function xe(r){let{minValue:e,maxValue:t,step:i,formatOptions:n,value:a,defaultValue:l=NaN,onChange:o,locale:m,isDisabled:V,isReadOnly:h}=r;a===null&&(a=NaN),a!==void 0&&!isNaN(a)&&(i!==void 0&&!isNaN(i)?a=M(a,e,t,i):a=L(a,e,t)),isNaN(l)||(i!==void 0&&!isNaN(i)?l=M(l,e,t,i):l=L(l,e,t));let[f,g]=pe.$458b0a5536c1a7cf$export$40bfa8c7b0832715(a,isNaN(l)?NaN:l,o),[$,p]=u.useState(()=>isNaN(f)?"":new Z(m,n).format(f)),b=u.useMemo(()=>new re(m,n),[m,n]),D=u.useMemo(()=>b.getNumberingSystem($),[b,$]),w=u.useMemo(()=>new Z(m,{...n,numberingSystem:D}),[m,n,D]),x=u.useMemo(()=>w.resolvedOptions(),[w]),y=u.useCallback(c=>isNaN(c)||c===null?"":w.format(c),[w]),v=he({...r,value:f}),d=i!==void 0&&!isNaN(i)?i:1;x.style==="percent"&&(i===void 0||isNaN(i))&&(d=.01);let[P,S]=u.useState(f),[F,I]=u.useState(m),[s,E]=u.useState(n);(!Object.is(f,P)||m!==F||n!==s)&&(p(y(f)),S(f),I(m),E(n));let N=u.useMemo(()=>b.parse($),[b,$]),ae=()=>{if(!$.length){g(NaN),p(a===void 0?"":y(f));return}if(isNaN(N)){p(y(f));return}let c;i===void 0||isNaN(i)?c=L(N,e,t):c=M(N,e,t,i),c=b.parse(y(c)),g(c),p(y(a===void 0?c:f))},B=(c,K=0)=>{let R=N;if(isNaN(R)){let C=isNaN(K)?0:K;return M(C,e,t,d)}else{let C=M(R,e,t,d);return c==="+"&&C>R||c==="-"&&C<R?C:M(_(c,R,d),e,t,d)}},ne=()=>{let c=B("+",e);c===f&&p(y(c)),g(c),v.commitValidation()},se=()=>{let c=B("-",t);c===f&&p(y(c)),g(c),v.commitValidation()},le=()=>{t!=null&&(g(M(t,e,t,d)),v.commitValidation())},oe=()=>{e!=null&&(g(e),v.commitValidation())},ue=u.useMemo(()=>!V&&!h&&(isNaN(N)||t===void 0||isNaN(t)||M(N,e,t,d)>N||_("+",N,d)<=t),[V,h,e,t,d,N]),ce=u.useMemo(()=>!V&&!h&&(isNaN(N)||e===void 0||isNaN(e)||M(N,e,t,d)<N||_("-",N,d)>=e),[V,h,e,t,d,N]);return{...v,validate:c=>b.isValidPartialNumber(c,e,t),increment:ne,incrementToMax:le,decrement:se,decrementToMin:oe,canIncrement:ue,canDecrement:ce,minValue:e,maxValue:t,numberValue:N,setNumberValue:g,setInputValue:p,inputValue:$,commit:ae}}function _(r,e,t){let i=r==="+"?e+t:e-t;if(e%1!==0||t%1!==0){const n=e.toString().split("."),a=t.toString().split("."),l=n[1]&&n[1].length||0,o=a[1]&&a[1].length||0,m=Math.pow(10,Math.max(l,o));e=Math.round(e*m),t=Math.round(t*m),i=r==="+"?e+t:e-t,i/=m}return i}const Pe=u.forwardRef(({placeholder:r,variant:e,disabled:t,type:i,readOnly:n,leftAdornments:a,rightAdornments:l,leftAdornmentsProps:o,rightAdornmentsProps:m,leftAdornmentsWidth:V,rightAdornmentsWidth:h,className:f,style:g,label:$,value:p,...b},D)=>{const x=xe({...b,label:$,value:p,onChange:d=>{b.onChange&&b.onChange(d)},locale:"nb-NO"}),y=u.useRef(null),{inputProps:v}=de.useNumberField({...b,label:$,value:p},x,y);return me.jsx(fe.Input,{placeholder:r,variant:e,disabled:t,type:i,readOnly:n,leftAdornments:a,rightAdornments:l,leftAdornmentsProps:o,rightAdornmentsProps:m,leftAdornmentsWidth:V,rightAdornmentsWidth:h,className:f,style:g,"data-testid":`number-field-${$}`,...v,onKeyDown:d=>{var P;if(d.key==="."){d.preventDefault();const S=d.target;if((P=S.value)!=null&&P.includes(","))return;const F=S.selectionStart||0,I=S.selectionEnd||0;S.value=S.value.substring(0,F)+","+S.value.substring(I)}},ref:D,"aria-label":$})});exports.NumberField=Pe;
package/number.mjs ADDED
@@ -0,0 +1,564 @@
1
+ import { jsx as fe } from "react/jsx-runtime";
2
+ import { useMemo as w, useContext as pe, createContext as be, useState as M, useRef as Z, useEffect as he, useCallback as ge, forwardRef as $e } from "react";
3
+ import { useNumberField as ye } from "react-aria";
4
+ import { Input as ve } from "@equinor/eds-core-react";
5
+ import { $ as Ne } from "./useControlledState-u436Wbp1.mjs";
6
+ function W(r, e = -1 / 0, t = 1 / 0) {
7
+ return Math.min(Math.max(r, e), t);
8
+ }
9
+ function j(r, e) {
10
+ let t = r, i = e.toString(), n = i.indexOf("."), a = n >= 0 ? i.length - n : 0;
11
+ if (a > 0) {
12
+ let l = Math.pow(10, a);
13
+ t = Math.round(t * l) / l;
14
+ }
15
+ return t;
16
+ }
17
+ function F(r, e, t, i) {
18
+ e = Number(e), t = Number(t);
19
+ let n = (r - (isNaN(e) ? 0 : e)) % i, a = j(Math.abs(n) * 2 >= i ? r + Math.sign(n) * (i - Math.abs(n)) : r - n, i);
20
+ return isNaN(e) ? !isNaN(t) && a > t && (a = Math.floor(j(t / i, i)) * i) : a < e ? a = e : !isNaN(t) && a > t && (a = e + Math.floor(j((t - e) / i, i)) * i), a = j(a, i), a;
21
+ }
22
+ const ie = {
23
+ badInput: !1,
24
+ customError: !1,
25
+ patternMismatch: !1,
26
+ rangeOverflow: !1,
27
+ rangeUnderflow: !1,
28
+ stepMismatch: !1,
29
+ tooLong: !1,
30
+ tooShort: !1,
31
+ typeMismatch: !1,
32
+ valueMissing: !1,
33
+ valid: !0
34
+ }, re = {
35
+ ...ie,
36
+ customError: !0,
37
+ valid: !1
38
+ }, T = {
39
+ isInvalid: !1,
40
+ validationDetails: ie,
41
+ validationErrors: []
42
+ }, Ve = be({}), q = "__formValidationState" + Date.now();
43
+ function Se(r) {
44
+ if (r[q]) {
45
+ let { realtimeValidation: e, displayValidation: t, updateValidation: i, resetValidation: n, commitValidation: a } = r[q];
46
+ return {
47
+ realtimeValidation: e,
48
+ displayValidation: t,
49
+ updateValidation: i,
50
+ resetValidation: n,
51
+ commitValidation: a
52
+ };
53
+ }
54
+ return De(r);
55
+ }
56
+ function De(r) {
57
+ let { isInvalid: e, validationState: t, name: i, value: n, builtinValidation: a, validate: l, validationBehavior: o = "aria" } = r;
58
+ t && (e || (e = t === "invalid"));
59
+ let m = e !== void 0 ? {
60
+ isInvalid: e,
61
+ validationErrors: [],
62
+ validationDetails: re
63
+ } : null, N = w(() => {
64
+ if (!l || n == null) return null;
65
+ let s = we(l, n);
66
+ return J(s);
67
+ }, [
68
+ l,
69
+ n
70
+ ]);
71
+ a != null && a.validationDetails.valid && (a = void 0);
72
+ let b = pe(Ve), d = w(() => i ? Array.isArray(i) ? i.flatMap((s) => B(b[s])) : B(b[i]) : [], [
73
+ b,
74
+ i
75
+ ]), [h, g] = M(b), [f, p] = M(!1);
76
+ b !== h && (g(b), p(!1));
77
+ let S = w(() => J(f ? [] : d), [
78
+ f,
79
+ d
80
+ ]), D = Z(T), [x, $] = M(T), y = Z(T), c = () => {
81
+ if (!P) return;
82
+ V(!1);
83
+ let s = N || a || D.current;
84
+ _(s, y.current) || (y.current = s, $(s));
85
+ }, [P, V] = M(!1);
86
+ return he(c), {
87
+ realtimeValidation: m || S || N || a || T,
88
+ displayValidation: o === "native" ? m || S || x : m || S || N || a || x,
89
+ updateValidation(s) {
90
+ o === "aria" && !_(x, s) ? $(s) : D.current = s;
91
+ },
92
+ resetValidation() {
93
+ let s = T;
94
+ _(s, y.current) || (y.current = s, $(s)), o === "native" && V(!1), p(!0);
95
+ },
96
+ commitValidation() {
97
+ o === "native" && V(!0), p(!0);
98
+ }
99
+ };
100
+ }
101
+ function B(r) {
102
+ return r ? Array.isArray(r) ? r : [
103
+ r
104
+ ] : [];
105
+ }
106
+ function we(r, e) {
107
+ if (typeof r == "function") {
108
+ let t = r(e);
109
+ if (t && typeof t != "boolean") return B(t);
110
+ }
111
+ return [];
112
+ }
113
+ function J(r) {
114
+ return r.length ? {
115
+ isInvalid: !0,
116
+ validationErrors: r,
117
+ validationDetails: re
118
+ } : null;
119
+ }
120
+ function _(r, e) {
121
+ return r === e ? !0 : !!r && !!e && r.isInvalid === e.isInvalid && r.validationErrors.length === e.validationErrors.length && r.validationErrors.every((t, i) => t === e.validationErrors[i]) && Object.entries(r.validationDetails).every(([t, i]) => e.validationDetails[t] === i);
122
+ }
123
+ let z = /* @__PURE__ */ new Map(), K = !1;
124
+ try {
125
+ K = new Intl.NumberFormat("de-DE", {
126
+ signDisplay: "exceptZero"
127
+ }).resolvedOptions().signDisplay === "exceptZero";
128
+ } catch {
129
+ }
130
+ let L = !1;
131
+ try {
132
+ L = new Intl.NumberFormat("de-DE", {
133
+ style: "unit",
134
+ unit: "degree"
135
+ }).resolvedOptions().style === "unit";
136
+ } catch {
137
+ }
138
+ const ae = {
139
+ degree: {
140
+ narrow: {
141
+ default: "°",
142
+ "ja-JP": " 度",
143
+ "zh-TW": "度",
144
+ "sl-SI": " °"
145
+ }
146
+ }
147
+ };
148
+ class Y {
149
+ /** Formats a number value as a string, according to the locale and options provided to the constructor. */
150
+ format(e) {
151
+ let t = "";
152
+ if (!K && this.options.signDisplay != null ? t = Pe(this.numberFormatter, this.options.signDisplay, e) : t = this.numberFormatter.format(e), this.options.style === "unit" && !L) {
153
+ var i;
154
+ let { unit: n, unitDisplay: a = "short", locale: l } = this.resolvedOptions();
155
+ if (!n) return t;
156
+ let o = (i = ae[n]) === null || i === void 0 ? void 0 : i[a];
157
+ t += o[l] || o.default;
158
+ }
159
+ return t;
160
+ }
161
+ /** Formats a number to an array of parts such as separators, digits, punctuation, and more. */
162
+ formatToParts(e) {
163
+ return this.numberFormatter.formatToParts(e);
164
+ }
165
+ /** Formats a number range as a string. */
166
+ formatRange(e, t) {
167
+ if (typeof this.numberFormatter.formatRange == "function") return this.numberFormatter.formatRange(e, t);
168
+ if (t < e) throw new RangeError("End date must be >= start date");
169
+ return `${this.format(e)} – ${this.format(t)}`;
170
+ }
171
+ /** Formats a number range as an array of parts. */
172
+ formatRangeToParts(e, t) {
173
+ if (typeof this.numberFormatter.formatRangeToParts == "function") return this.numberFormatter.formatRangeToParts(e, t);
174
+ if (t < e) throw new RangeError("End date must be >= start date");
175
+ let i = this.numberFormatter.formatToParts(e), n = this.numberFormatter.formatToParts(t);
176
+ return [
177
+ ...i.map((a) => ({
178
+ ...a,
179
+ source: "startRange"
180
+ })),
181
+ {
182
+ type: "literal",
183
+ value: " – ",
184
+ source: "shared"
185
+ },
186
+ ...n.map((a) => ({
187
+ ...a,
188
+ source: "endRange"
189
+ }))
190
+ ];
191
+ }
192
+ /** Returns the resolved formatting options based on the values passed to the constructor. */
193
+ resolvedOptions() {
194
+ let e = this.numberFormatter.resolvedOptions();
195
+ return !K && this.options.signDisplay != null && (e = {
196
+ ...e,
197
+ signDisplay: this.options.signDisplay
198
+ }), !L && this.options.style === "unit" && (e = {
199
+ ...e,
200
+ style: "unit",
201
+ unit: this.options.unit,
202
+ unitDisplay: this.options.unitDisplay
203
+ }), e;
204
+ }
205
+ constructor(e, t = {}) {
206
+ this.numberFormatter = xe(e, t), this.options = t;
207
+ }
208
+ }
209
+ function xe(r, e = {}) {
210
+ let { numberingSystem: t } = e;
211
+ if (t && r.includes("-nu-") && (r.includes("-u-") || (r += "-u-"), r += `-nu-${t}`), e.style === "unit" && !L) {
212
+ var i;
213
+ let { unit: l, unitDisplay: o = "short" } = e;
214
+ if (!l) throw new Error('unit option must be provided with style: "unit"');
215
+ if (!(!((i = ae[l]) === null || i === void 0) && i[o])) throw new Error(`Unsupported unit ${l} with unitDisplay = ${o}`);
216
+ e = {
217
+ ...e,
218
+ style: "decimal"
219
+ };
220
+ }
221
+ let n = r + (e ? Object.entries(e).sort((l, o) => l[0] < o[0] ? -1 : 1).join() : "");
222
+ if (z.has(n)) return z.get(n);
223
+ let a = new Intl.NumberFormat(r, e);
224
+ return z.set(n, a), a;
225
+ }
226
+ function Pe(r, e, t) {
227
+ if (e === "auto") return r.format(t);
228
+ if (e === "never") return r.format(Math.abs(t));
229
+ {
230
+ let i = !1;
231
+ if (e === "always" ? i = t > 0 || Object.is(t, 0) : e === "exceptZero" && (Object.is(t, -0) || Object.is(t, 0) ? t = Math.abs(t) : i = t > 0), i) {
232
+ let n = r.format(-t), a = r.format(t), l = n.replace(a, "").replace(/\u200e|\u061C/, "");
233
+ return [
234
+ ...l
235
+ ].length !== 1 && console.warn("@react-aria/i18n polyfill for NumberFormat signDisplay: Unsupported case"), n.replace(a, "!!!").replace(l, "+").replace("!!!", a);
236
+ } else return r.format(t);
237
+ }
238
+ }
239
+ const Ee = new RegExp("^.*\\(.*\\).*$"), Fe = [
240
+ "latn",
241
+ "arab",
242
+ "hanidec",
243
+ "deva",
244
+ "beng"
245
+ ];
246
+ class ne {
247
+ /**
248
+ * Parses the given string to a number. Returns NaN if a valid number could not be parsed.
249
+ */
250
+ parse(e) {
251
+ return G(this.locale, this.options, e).parse(e);
252
+ }
253
+ /**
254
+ * Returns whether the given string could potentially be a valid number. This should be used to
255
+ * validate user input as the user types. If a `minValue` or `maxValue` is provided, the validity
256
+ * of the minus/plus sign characters can be checked.
257
+ */
258
+ isValidPartialNumber(e, t, i) {
259
+ return G(this.locale, this.options, e).isValidPartialNumber(e, t, i);
260
+ }
261
+ /**
262
+ * Returns a numbering system for which the given string is valid in the current locale.
263
+ * If no numbering system could be detected, the default numbering system for the current
264
+ * locale is returned.
265
+ */
266
+ getNumberingSystem(e) {
267
+ return G(this.locale, this.options, e).options.numberingSystem;
268
+ }
269
+ constructor(e, t = {}) {
270
+ this.locale = e, this.options = t;
271
+ }
272
+ }
273
+ const X = /* @__PURE__ */ new Map();
274
+ function G(r, e, t) {
275
+ let i = H(r, e);
276
+ if (!r.includes("-nu-") && !i.isValidPartialNumber(t)) {
277
+ for (let n of Fe) if (n !== i.options.numberingSystem) {
278
+ let a = H(r + (r.includes("-u-") ? "-nu-" : "-u-nu-") + n, e);
279
+ if (a.isValidPartialNumber(t)) return a;
280
+ }
281
+ }
282
+ return i;
283
+ }
284
+ function H(r, e) {
285
+ let t = r + (e ? Object.entries(e).sort((n, a) => n[0] < a[0] ? -1 : 1).join() : ""), i = X.get(t);
286
+ return i || (i = new Me(r, e), X.set(t, i)), i;
287
+ }
288
+ class Me {
289
+ parse(e) {
290
+ let t = this.sanitize(e);
291
+ if (this.symbols.group && (t = A(t, this.symbols.group, "")), this.symbols.decimal && (t = t.replace(this.symbols.decimal, ".")), this.symbols.minusSign && (t = t.replace(this.symbols.minusSign, "-")), t = t.replace(this.symbols.numeral, this.symbols.index), this.options.style === "percent") {
292
+ let l = t.indexOf("-");
293
+ t = t.replace("-", ""), t = t.replace("+", "");
294
+ let o = t.indexOf(".");
295
+ o === -1 && (o = t.length), t = t.replace(".", ""), o - 2 === 0 ? t = `0.${t}` : o - 2 === -1 ? t = `0.0${t}` : o - 2 === -2 ? t = "0.00" : t = `${t.slice(0, o - 2)}.${t.slice(o - 2)}`, l > -1 && (t = `-${t}`);
296
+ }
297
+ let i = t ? +t : NaN;
298
+ if (isNaN(i)) return NaN;
299
+ if (this.options.style === "percent") {
300
+ var n, a;
301
+ let l = {
302
+ ...this.options,
303
+ style: "decimal",
304
+ minimumFractionDigits: Math.min(((n = this.options.minimumFractionDigits) !== null && n !== void 0 ? n : 0) + 2, 20),
305
+ maximumFractionDigits: Math.min(((a = this.options.maximumFractionDigits) !== null && a !== void 0 ? a : 0) + 2, 20)
306
+ };
307
+ return new ne(this.locale, l).parse(new Y(this.locale, l).format(i));
308
+ }
309
+ return this.options.currencySign === "accounting" && Ee.test(e) && (i = -1 * i), i;
310
+ }
311
+ sanitize(e) {
312
+ return e = e.replace(this.symbols.literals, ""), this.symbols.minusSign && (e = e.replace("-", this.symbols.minusSign)), this.options.numberingSystem === "arab" && (this.symbols.decimal && (e = e.replace(",", this.symbols.decimal), e = e.replace("،", this.symbols.decimal)), this.symbols.group && (e = A(e, ".", this.symbols.group))), this.options.locale === "fr-FR" && this.symbols.group && (e = A(e, " ", this.symbols.group), e = A(e, /\u00A0/g, this.symbols.group)), e;
313
+ }
314
+ isValidPartialNumber(e, t = -1 / 0, i = 1 / 0) {
315
+ return e = this.sanitize(e), this.symbols.minusSign && e.startsWith(this.symbols.minusSign) && t < 0 ? e = e.slice(this.symbols.minusSign.length) : this.symbols.plusSign && e.startsWith(this.symbols.plusSign) && i > 0 && (e = e.slice(this.symbols.plusSign.length)), this.symbols.group && e.startsWith(this.symbols.group) || this.symbols.decimal && e.indexOf(this.symbols.decimal) > -1 && this.options.maximumFractionDigits === 0 ? !1 : (this.symbols.group && (e = A(e, this.symbols.group, "")), e = e.replace(this.symbols.numeral, ""), this.symbols.decimal && (e = e.replace(this.symbols.decimal, "")), e.length === 0);
316
+ }
317
+ constructor(e, t = {}) {
318
+ this.locale = e, t.roundingIncrement !== 1 && t.roundingIncrement != null && (t.maximumFractionDigits == null && t.minimumFractionDigits == null ? (t.maximumFractionDigits = 0, t.minimumFractionDigits = 0) : t.maximumFractionDigits == null ? t.maximumFractionDigits = t.minimumFractionDigits : t.minimumFractionDigits == null && (t.minimumFractionDigits = t.maximumFractionDigits)), this.formatter = new Intl.NumberFormat(e, t), this.options = this.formatter.resolvedOptions(), this.symbols = Re(e, this.formatter, this.options, t);
319
+ var i, n;
320
+ this.options.style === "percent" && (((i = this.options.minimumFractionDigits) !== null && i !== void 0 ? i : 0) > 18 || ((n = this.options.maximumFractionDigits) !== null && n !== void 0 ? n : 0) > 18) && console.warn("NumberParser cannot handle percentages with greater than 18 decimal places, please reduce the number in your options.");
321
+ }
322
+ }
323
+ const ee = /* @__PURE__ */ new Set([
324
+ "decimal",
325
+ "fraction",
326
+ "integer",
327
+ "minusSign",
328
+ "plusSign",
329
+ "group"
330
+ ]), Ie = [
331
+ 0,
332
+ 4,
333
+ 2,
334
+ 1,
335
+ 11,
336
+ 20,
337
+ 3,
338
+ 7,
339
+ 100,
340
+ 21,
341
+ 0.1,
342
+ 1.1
343
+ ];
344
+ function Re(r, e, t, i) {
345
+ var n, a, l, o;
346
+ let m = new Intl.NumberFormat(r, {
347
+ ...t,
348
+ // Resets so we get the full range of symbols
349
+ minimumSignificantDigits: 1,
350
+ maximumSignificantDigits: 21,
351
+ roundingIncrement: 1,
352
+ roundingPriority: "auto",
353
+ roundingMode: "halfExpand"
354
+ }), N = m.formatToParts(-10000.111), b = m.formatToParts(10000.111), d = Ie.map((s) => m.formatToParts(s));
355
+ var h;
356
+ let g = (h = (n = N.find((s) => s.type === "minusSign")) === null || n === void 0 ? void 0 : n.value) !== null && h !== void 0 ? h : "-", f = (a = b.find((s) => s.type === "plusSign")) === null || a === void 0 ? void 0 : a.value;
357
+ !f && ((i == null ? void 0 : i.signDisplay) === "exceptZero" || (i == null ? void 0 : i.signDisplay) === "always") && (f = "+");
358
+ let S = (l = new Intl.NumberFormat(r, {
359
+ ...t,
360
+ minimumFractionDigits: 2,
361
+ maximumFractionDigits: 2
362
+ }).formatToParts(1e-3).find((s) => s.type === "decimal")) === null || l === void 0 ? void 0 : l.value, D = (o = N.find((s) => s.type === "group")) === null || o === void 0 ? void 0 : o.value, x = N.filter((s) => !ee.has(s.type)).map((s) => te(s.value)), $ = d.flatMap((s) => s.filter((E) => !ee.has(E.type)).map((E) => te(E.value))), y = [
363
+ .../* @__PURE__ */ new Set([
364
+ ...x,
365
+ ...$
366
+ ])
367
+ ].sort((s, E) => E.length - s.length), c = y.length === 0 ? new RegExp("[\\p{White_Space}]", "gu") : new RegExp(`${y.join("|")}|[\\p{White_Space}]`, "gu"), P = [
368
+ ...new Intl.NumberFormat(t.locale, {
369
+ useGrouping: !1
370
+ }).format(9876543210)
371
+ ].reverse(), V = new Map(P.map((s, E) => [
372
+ s,
373
+ E
374
+ ])), I = new RegExp(`[${P.join("")}]`, "g");
375
+ return {
376
+ minusSign: g,
377
+ plusSign: f,
378
+ decimal: S,
379
+ group: D,
380
+ literals: c,
381
+ numeral: I,
382
+ index: (s) => String(V.get(s))
383
+ };
384
+ }
385
+ function A(r, e, t) {
386
+ return r.replaceAll ? r.replaceAll(e, t) : r.split(e).join(t);
387
+ }
388
+ function te(r) {
389
+ return r.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
390
+ }
391
+ function Ce(r) {
392
+ let { minValue: e, maxValue: t, step: i, formatOptions: n, value: a, defaultValue: l = NaN, onChange: o, locale: m, isDisabled: N, isReadOnly: b } = r;
393
+ a === null && (a = NaN), a !== void 0 && !isNaN(a) && (i !== void 0 && !isNaN(i) ? a = F(a, e, t, i) : a = W(a, e, t)), isNaN(l) || (i !== void 0 && !isNaN(i) ? l = F(l, e, t, i) : l = W(l, e, t));
394
+ let [d, h] = Ne(a, isNaN(l) ? NaN : l, o), [g, f] = M(() => isNaN(d) ? "" : new Y(m, n).format(d)), p = w(() => new ne(m, n), [
395
+ m,
396
+ n
397
+ ]), S = w(() => p.getNumberingSystem(g), [
398
+ p,
399
+ g
400
+ ]), D = w(() => new Y(m, {
401
+ ...n,
402
+ numberingSystem: S
403
+ }), [
404
+ m,
405
+ n,
406
+ S
407
+ ]), x = w(() => D.resolvedOptions(), [
408
+ D
409
+ ]), $ = ge((u) => isNaN(u) || u === null ? "" : D.format(u), [
410
+ D
411
+ ]), y = Se({
412
+ ...r,
413
+ value: d
414
+ }), c = i !== void 0 && !isNaN(i) ? i : 1;
415
+ x.style === "percent" && (i === void 0 || isNaN(i)) && (c = 0.01);
416
+ let [P, V] = M(d), [I, R] = M(m), [s, E] = M(n);
417
+ (!Object.is(d, P) || m !== I || n !== s) && (f($(d)), V(d), R(m), E(n));
418
+ let v = w(() => p.parse(g), [
419
+ p,
420
+ g
421
+ ]), se = () => {
422
+ if (!g.length) {
423
+ h(NaN), f(a === void 0 ? "" : $(d));
424
+ return;
425
+ }
426
+ if (isNaN(v)) {
427
+ f($(d));
428
+ return;
429
+ }
430
+ let u;
431
+ i === void 0 || isNaN(i) ? u = W(v, e, t) : u = F(v, e, t, i), u = p.parse($(u)), h(u), f($(a === void 0 ? u : d));
432
+ }, k = (u, Q = 0) => {
433
+ let C = v;
434
+ if (isNaN(C)) {
435
+ let O = isNaN(Q) ? 0 : Q;
436
+ return F(O, e, t, c);
437
+ } else {
438
+ let O = F(C, e, t, c);
439
+ return u === "+" && O > C || u === "-" && O < C ? O : F(U(u, C, c), e, t, c);
440
+ }
441
+ }, le = () => {
442
+ let u = k("+", e);
443
+ u === d && f($(u)), h(u), y.commitValidation();
444
+ }, oe = () => {
445
+ let u = k("-", t);
446
+ u === d && f($(u)), h(u), y.commitValidation();
447
+ }, ue = () => {
448
+ t != null && (h(F(t, e, t, c)), y.commitValidation());
449
+ }, me = () => {
450
+ e != null && (h(e), y.commitValidation());
451
+ }, ce = w(() => !N && !b && (isNaN(v) || t === void 0 || isNaN(t) || F(v, e, t, c) > v || U("+", v, c) <= t), [
452
+ N,
453
+ b,
454
+ e,
455
+ t,
456
+ c,
457
+ v
458
+ ]), de = w(() => !N && !b && (isNaN(v) || e === void 0 || isNaN(e) || F(v, e, t, c) < v || U("-", v, c) >= e), [
459
+ N,
460
+ b,
461
+ e,
462
+ t,
463
+ c,
464
+ v
465
+ ]);
466
+ return {
467
+ ...y,
468
+ validate: (u) => p.isValidPartialNumber(u, e, t),
469
+ increment: le,
470
+ incrementToMax: ue,
471
+ decrement: oe,
472
+ decrementToMin: me,
473
+ canIncrement: ce,
474
+ canDecrement: de,
475
+ minValue: e,
476
+ maxValue: t,
477
+ numberValue: v,
478
+ setNumberValue: h,
479
+ setInputValue: f,
480
+ inputValue: g,
481
+ commit: se
482
+ };
483
+ }
484
+ function U(r, e, t) {
485
+ let i = r === "+" ? e + t : e - t;
486
+ if (e % 1 !== 0 || t % 1 !== 0) {
487
+ const n = e.toString().split("."), a = t.toString().split("."), l = n[1] && n[1].length || 0, o = a[1] && a[1].length || 0, m = Math.pow(10, Math.max(l, o));
488
+ e = Math.round(e * m), t = Math.round(t * m), i = r === "+" ? e + t : e - t, i /= m;
489
+ }
490
+ return i;
491
+ }
492
+ const _e = $e(
493
+ ({
494
+ placeholder: r,
495
+ variant: e,
496
+ disabled: t,
497
+ type: i,
498
+ readOnly: n,
499
+ leftAdornments: a,
500
+ rightAdornments: l,
501
+ leftAdornmentsProps: o,
502
+ rightAdornmentsProps: m,
503
+ leftAdornmentsWidth: N,
504
+ rightAdornmentsWidth: b,
505
+ className: d,
506
+ style: h,
507
+ label: g,
508
+ value: f,
509
+ ...p
510
+ }, S) => {
511
+ const x = Ce({
512
+ ...p,
513
+ label: g,
514
+ value: f,
515
+ onChange: (c) => {
516
+ p.onChange && p.onChange(c);
517
+ },
518
+ locale: "nb-NO"
519
+ }), $ = Z(null), { inputProps: y } = ye(
520
+ {
521
+ ...p,
522
+ label: g,
523
+ value: f
524
+ },
525
+ x,
526
+ $
527
+ );
528
+ return /* @__PURE__ */ fe(
529
+ ve,
530
+ {
531
+ placeholder: r,
532
+ variant: e,
533
+ disabled: t,
534
+ type: i,
535
+ readOnly: n,
536
+ leftAdornments: a,
537
+ rightAdornments: l,
538
+ leftAdornmentsProps: o,
539
+ rightAdornmentsProps: m,
540
+ leftAdornmentsWidth: N,
541
+ rightAdornmentsWidth: b,
542
+ className: d,
543
+ style: h,
544
+ "data-testid": `number-field-${g}`,
545
+ ...y,
546
+ onKeyDown: (c) => {
547
+ var P;
548
+ if (c.key === ".") {
549
+ c.preventDefault();
550
+ const V = c.target;
551
+ if ((P = V.value) != null && P.includes(",")) return;
552
+ const I = V.selectionStart || 0, R = V.selectionEnd || 0;
553
+ V.value = V.value.substring(0, I) + "," + V.value.substring(R);
554
+ }
555
+ },
556
+ ref: S,
557
+ "aria-label": g
558
+ }
559
+ );
560
+ }
561
+ );
562
+ export {
563
+ _e as NumberField
564
+ };
package/package.json CHANGED
@@ -1,11 +1,16 @@
1
1
  {
2
2
  "name": "@equinor/roma-ui",
3
- "version": "0.1.2",
3
+ "version": "0.2.1",
4
4
  "exports": {
5
5
  ".": {
6
6
  "import": "./index.mjs",
7
7
  "require": "./index.js"
8
8
  },
9
+ "./number": {
10
+ "types": "./lib/NumberField/NumberField.d.ts",
11
+ "import": "./NumberField.mjs",
12
+ "require": "./NumberField.js"
13
+ },
9
14
  "./switch": {
10
15
  "types": "./lib/switch/Switch.d.ts",
11
16
  "import": "./switch.mjs",
package/switch.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("react/jsx-runtime"),S=require("react-aria"),b=require("react"),d=require("@equinor/eds-tokens"),C=require("@equinor/eds-core-react");function k(e,t,i){let[o,u]=b.useState(e||t),f=b.useRef(e!==void 0),s=e!==void 0;b.useEffect(()=>{let l=f.current;l!==s&&process.env.NODE_ENV!=="production"&&console.warn(`WARN: A component changed from ${l?"controlled":"uncontrolled"} to ${s?"controlled":"uncontrolled"}.`),f.current=s},[s]);let a=s?e:o,g=b.useCallback((l,...n)=>{let r=(h,...y)=>{i&&(Object.is(a,h)||i(h,...y)),s||(a=h)};typeof l=="function"?(process.env.NODE_ENV!=="production"&&console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320"),u((y,...p)=>{let x=l(s?a:y,...p);return r(x,...n),s?y:x})):(s||u(l),r(l,...n))},[s,a,i]);return[a,g]}function R(e={}){let{isReadOnly:t}=e,[i,o]=k(e.isSelected,e.defaultSelected||!1,e.onChange);function u(s){t||o(s)}function f(){t||o(!i)}return{isSelected:i,setSelected:u,toggle:f}}const _={close:"M8.21967 9.28033C8.51256 9.57322 8.98744 9.57322 9.28033 9.28033C9.57322 8.98744 9.57322 8.51256 9.28033 8.21967L6.06066 5L9.28033 1.78033C9.57322 1.48744 9.57322 1.01256 9.28033 0.71967C8.98744 0.426777 8.51256 0.426777 8.21967 0.71967L5 3.93934L1.78033 0.71967C1.48744 0.426777 1.01256 0.426777 0.71967 0.71967C0.426777 1.01256 0.426777 1.48744 0.71967 1.78033L3.93934 5L0.71967 8.21967C0.426777 8.51256 0.426777 8.98744 0.71967 9.28033C1.01256 9.57322 1.48744 9.57322 1.78033 9.28033L5 6.06066L8.21967 9.28033Z",check:"M13.0303 4.21967C13.3232 4.51256 13.3232 4.98744 13.0303 5.28033L6.78033 11.5303C6.48744 11.8232 6.01256 11.8232 5.71967 11.5303L2.96967 8.78033C2.67678 8.48744 2.67678 8.01256 2.96967 7.71967C3.26256 7.42678 3.73744 7.42678 4.03033 7.71967L6.25 9.93934L11.9697 4.21967C12.2626 3.92678 12.7374 3.92678 13.0303 4.21967Z"};function m(e){const t={...e,isDisabled:e.isDisabled??e.disabled,isReadOnly:e.isReadOnly??e.readOnly,isSelected:e.isSelected??e.checked,defaultSelected:e.defaultSelected??e.defaultChecked},i=R(t),o=b.useRef(null),{inputProps:u}=S.useSwitch(t,i,o),{isFocusVisible:f,focusProps:s}=S.useFocusRing(),{isHovered:a,hoverProps:g}=S.useHover({isDisabled:!1}),{density:l}=C.useEds(),n=l==="compact",r=b.useMemo(()=>({ripple:d.tokens.colors.interactive.primary__hover_alt.rgba,background:i.isSelected?d.tokens.colors.interactive.primary__resting.hex:"rgba(220, 220, 220, 1)",circle:i.isSelected?d.tokens.colors.interactive.primary__hover_alt.hex:"rgba(111, 111, 111, 1)",icon:i.isSelected?d.tokens.colors.interactive.primary__resting.hex:"white",focus:d.tokens.colors.interactive.primary__resting.hex}),[i.isSelected]),h=n?32:48,y=n?4:12,p=n?0:8;return c.jsxs(C.Typography,{as:"label",variant:"text",group:"input",style:{display:"flex",alignItems:"center",color:t.isReadOnly||t.isDisabled?d.tokens.colors.text.static_icons__tertiary.rgba:d.tokens.colors.text.static_icons__default.rgba,cursor:t.isReadOnly||t.isDisabled?"not-allowed":"pointer"},children:[c.jsx(S.VisuallyHidden,{children:c.jsx("input",{...u,...s,ref:o})}),c.jsx("div",{style:{display:"inline",height:n?32:48},...g,children:c.jsxs("svg",{width:60,height:h,"aria-hidden":"true",style:{marginRight:4,opacity:t.isDisabled||t.isReadOnly?.4:1},children:[a&&!t.isDisabled&&!t.isReadOnly&&c.jsx("circle",{cx:i.isSelected?42:20,cy:16+p,r:n?16:20,fill:r.ripple}),c.jsx("rect",{x:8,y,width:46,height:24,rx:12,fill:r.background}),c.jsx("circle",{cx:i.isSelected?42:20,cy:16+p,r:9.5,fill:r.circle}),c.jsx("path",{fillRule:"evenodd",clipRule:"evenodd",d:i.isSelected?_.check:_.close,transform:`translate(${i.isSelected?32:14},${(i.isSelected?6:10)+p}) scale(1.2)`,fill:r.icon,style:{pointerEvents:"none"}}),f&&c.jsx("rect",{x:6,y:p,width:50,height:32,rx:12,fill:"none",strokeDasharray:2,stroke:r.focus,strokeWidth:2})]})}),t.children??t.label]})}exports.Switch=m;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("react/jsx-runtime"),o=require("react-aria"),u=require("react"),c=require("@equinor/eds-tokens"),y=require("@equinor/eds-core-react"),C=require("./useControlledState-D0L6S_5r.js");function _(t={}){let{isReadOnly:e}=t,[i,l]=C.$458b0a5536c1a7cf$export$40bfa8c7b0832715(t.isSelected,t.defaultSelected||!1,t.onChange);function d(h){e||l(h)}function f(){e||l(!i)}return{isSelected:i,setSelected:d,toggle:f}}const g={close:"M8.21967 9.28033C8.51256 9.57322 8.98744 9.57322 9.28033 9.28033C9.57322 8.98744 9.57322 8.51256 9.28033 8.21967L6.06066 5L9.28033 1.78033C9.57322 1.48744 9.57322 1.01256 9.28033 0.71967C8.98744 0.426777 8.51256 0.426777 8.21967 0.71967L5 3.93934L1.78033 0.71967C1.48744 0.426777 1.01256 0.426777 0.71967 0.71967C0.426777 1.01256 0.426777 1.48744 0.71967 1.78033L3.93934 5L0.71967 8.21967C0.426777 8.51256 0.426777 8.98744 0.71967 9.28033C1.01256 9.57322 1.48744 9.57322 1.78033 9.28033L5 6.06066L8.21967 9.28033Z",check:"M13.0303 4.21967C13.3232 4.51256 13.3232 4.98744 13.0303 5.28033L6.78033 11.5303C6.48744 11.8232 6.01256 11.8232 5.71967 11.5303L2.96967 8.78033C2.67678 8.48744 2.67678 8.01256 2.96967 7.71967C3.26256 7.42678 3.73744 7.42678 4.03033 7.71967L6.25 9.93934L11.9697 4.21967C12.2626 3.92678 12.7374 3.92678 13.0303 4.21967Z"};function k(t){const e={...t,isDisabled:t.isDisabled??t.disabled,isReadOnly:t.isReadOnly??t.readOnly,isSelected:t.isSelected??t.checked,defaultSelected:t.defaultSelected??t.defaultChecked},i=_(e),l=u.useRef(null),{inputProps:d}=o.useSwitch(e,i,l),{isFocusVisible:f,focusProps:h}=o.useFocusRing(),{isHovered:x,hoverProps:S}=o.useHover({isDisabled:!1}),{density:b}=y.useEds(),n=b==="compact",r=u.useMemo(()=>({ripple:c.tokens.colors.interactive.primary__hover_alt.rgba,background:i.isSelected?c.tokens.colors.interactive.primary__resting.hex:"rgba(220, 220, 220, 1)",circle:i.isSelected?c.tokens.colors.interactive.primary__hover_alt.hex:"rgba(111, 111, 111, 1)",icon:i.isSelected?c.tokens.colors.interactive.primary__resting.hex:"white",focus:c.tokens.colors.interactive.primary__resting.hex}),[i.isSelected]),p=n?32:48,v=n?4:12,a=n?0:8;return s.jsxs(y.Typography,{as:"label",variant:"text",group:"input",style:{display:"flex",alignItems:"center",width:"fit-content",color:e.isReadOnly||e.isDisabled?c.tokens.colors.text.static_icons__tertiary.rgba:c.tokens.colors.text.static_icons__default.rgba,cursor:e.isReadOnly||e.isDisabled?"not-allowed":"pointer"},children:[s.jsx(o.VisuallyHidden,{children:s.jsx("input",{...d,...h,ref:l})}),s.jsx("div",{style:{display:"inline",height:n?32:48},...S,children:s.jsxs("svg",{width:60,height:p,"aria-hidden":"true",style:{marginRight:4,opacity:e.isDisabled||e.isReadOnly?.4:1},children:[x&&!e.isDisabled&&!e.isReadOnly&&s.jsx("circle",{cx:i.isSelected?42:20,cy:16+a,r:n?16:20,fill:r.ripple}),s.jsx("rect",{x:8,y:v,width:46,height:24,rx:12,fill:r.background}),s.jsx("circle",{cx:i.isSelected?42:20,cy:16+a,r:9.5,fill:r.circle}),s.jsx("path",{fillRule:"evenodd",clipRule:"evenodd",d:i.isSelected?g.check:g.close,transform:`translate(${i.isSelected?32:14},${(i.isSelected?6:10)+a}) scale(1.2)`,fill:r.icon,style:{pointerEvents:"none"}}),f&&s.jsx("rect",{x:6,y:a,width:50,height:32,rx:12,fill:"none",strokeDasharray:2,stroke:r.focus,strokeWidth:2})]})}),e.children??e.label]})}exports.Switch=k;
package/switch.mjs CHANGED
@@ -1,68 +1,43 @@
1
- import { jsxs as S, jsx as r } from "react/jsx-runtime";
2
- import { useSwitch as _, useFocusRing as x, useHover as R, VisuallyHidden as O } from "react-aria";
3
- import { useState as k, useRef as C, useEffect as L, useCallback as v, useMemo as w } from "react";
4
- import { tokens as a } from "@equinor/eds-tokens";
5
- import { useEds as D, Typography as $ } from "@equinor/eds-core-react";
6
- function V(e, t, i) {
7
- let [o, f] = k(e || t), u = C(e !== void 0), l = e !== void 0;
8
- L(() => {
9
- let c = u.current;
10
- c !== l && process.env.NODE_ENV !== "production" && console.warn(`WARN: A component changed from ${c ? "controlled" : "uncontrolled"} to ${l ? "controlled" : "uncontrolled"}.`), u.current = l;
11
- }, [
12
- l
13
- ]);
14
- let d = l ? e : o, b = v((c, ...s) => {
15
- let n = (h, ...p) => {
16
- i && (Object.is(d, h) || i(h, ...p)), l || (d = h);
17
- };
18
- typeof c == "function" ? (process.env.NODE_ENV !== "production" && console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320"), f((p, ...y) => {
19
- let g = c(l ? d : p, ...y);
20
- return n(g, ...s), l ? p : g;
21
- })) : (l || f(c), n(c, ...s));
22
- }, [
23
- l,
24
- d,
25
- i
26
- ]);
27
- return [
28
- d,
29
- b
30
- ];
31
- }
32
- function E(e = {}) {
33
- let { isReadOnly: t } = e, [i, o] = V(e.isSelected, e.defaultSelected || !1, e.onChange);
34
- function f(l) {
35
- t || o(l);
1
+ import { jsxs as h, jsx as l } from "react/jsx-runtime";
2
+ import { useSwitch as b, useFocusRing as x, useHover as _, VisuallyHidden as v } from "react-aria";
3
+ import { useRef as C, useMemo as R } from "react";
4
+ import { tokens as c } from "@equinor/eds-tokens";
5
+ import { useEds as L, Typography as k } from "@equinor/eds-core-react";
6
+ import { $ as O } from "./useControlledState-u436Wbp1.mjs";
7
+ function $(t = {}) {
8
+ let { isReadOnly: e } = t, [i, s] = O(t.isSelected, t.defaultSelected || !1, t.onChange);
9
+ function o(f) {
10
+ e || s(f);
36
11
  }
37
- function u() {
38
- t || o(!i);
12
+ function d() {
13
+ e || s(!i);
39
14
  }
40
15
  return {
41
16
  isSelected: i,
42
- setSelected: f,
43
- toggle: u
17
+ setSelected: o,
18
+ toggle: d
44
19
  };
45
20
  }
46
- const m = {
21
+ const y = {
47
22
  close: "M8.21967 9.28033C8.51256 9.57322 8.98744 9.57322 9.28033 9.28033C9.57322 8.98744 9.57322 8.51256 9.28033 8.21967L6.06066 5L9.28033 1.78033C9.57322 1.48744 9.57322 1.01256 9.28033 0.71967C8.98744 0.426777 8.51256 0.426777 8.21967 0.71967L5 3.93934L1.78033 0.71967C1.48744 0.426777 1.01256 0.426777 0.71967 0.71967C0.426777 1.01256 0.426777 1.48744 0.71967 1.78033L3.93934 5L0.71967 8.21967C0.426777 8.51256 0.426777 8.98744 0.71967 9.28033C1.01256 9.57322 1.48744 9.57322 1.78033 9.28033L5 6.06066L8.21967 9.28033Z",
48
23
  check: "M13.0303 4.21967C13.3232 4.51256 13.3232 4.98744 13.0303 5.28033L6.78033 11.5303C6.48744 11.8232 6.01256 11.8232 5.71967 11.5303L2.96967 8.78033C2.67678 8.48744 2.67678 8.01256 2.96967 7.71967C3.26256 7.42678 3.73744 7.42678 4.03033 7.71967L6.25 9.93934L11.9697 4.21967C12.2626 3.92678 12.7374 3.92678 13.0303 4.21967Z"
49
24
  };
50
- function W(e) {
51
- const t = {
52
- ...e,
53
- isDisabled: e.isDisabled ?? e.disabled,
54
- isReadOnly: e.isReadOnly ?? e.readOnly,
55
- isSelected: e.isSelected ?? e.checked,
56
- defaultSelected: e.defaultSelected ?? e.defaultChecked
57
- }, i = E(t), o = C(null), { inputProps: f } = _(t, i, o), { isFocusVisible: u, focusProps: l } = x(), { isHovered: d, hoverProps: b } = R({ isDisabled: !1 }), { density: c } = D(), s = c === "compact", n = w(() => ({
58
- ripple: a.colors.interactive.primary__hover_alt.rgba,
59
- background: i.isSelected ? a.colors.interactive.primary__resting.hex : "rgba(220, 220, 220, 1)",
60
- circle: i.isSelected ? a.colors.interactive.primary__hover_alt.hex : "rgba(111, 111, 111, 1)",
61
- icon: i.isSelected ? a.colors.interactive.primary__resting.hex : "white",
62
- focus: a.colors.interactive.primary__resting.hex
63
- }), [i.isSelected]), h = s ? 32 : 48, p = s ? 4 : 12, y = s ? 0 : 8;
64
- return /* @__PURE__ */ S(
65
- $,
25
+ function F(t) {
26
+ const e = {
27
+ ...t,
28
+ isDisabled: t.isDisabled ?? t.disabled,
29
+ isReadOnly: t.isReadOnly ?? t.readOnly,
30
+ isSelected: t.isSelected ?? t.checked,
31
+ defaultSelected: t.defaultSelected ?? t.defaultChecked
32
+ }, i = $(e), s = C(null), { inputProps: o } = b(e, i, s), { isFocusVisible: d, focusProps: f } = x(), { isHovered: u, hoverProps: g } = _({ isDisabled: !1 }), { density: p } = L(), r = p === "compact", n = R(() => ({
33
+ ripple: c.colors.interactive.primary__hover_alt.rgba,
34
+ background: i.isSelected ? c.colors.interactive.primary__resting.hex : "rgba(220, 220, 220, 1)",
35
+ circle: i.isSelected ? c.colors.interactive.primary__hover_alt.hex : "rgba(111, 111, 111, 1)",
36
+ icon: i.isSelected ? c.colors.interactive.primary__resting.hex : "white",
37
+ focus: c.colors.interactive.primary__resting.hex
38
+ }), [i.isSelected]), m = r ? 32 : 48, S = r ? 4 : 12, a = r ? 0 : 8;
39
+ return /* @__PURE__ */ h(
40
+ k,
66
41
  {
67
42
  as: "label",
68
43
  variant: "text",
@@ -70,72 +45,73 @@ function W(e) {
70
45
  style: {
71
46
  display: "flex",
72
47
  alignItems: "center",
73
- color: t.isReadOnly || t.isDisabled ? a.colors.text.static_icons__tertiary.rgba : a.colors.text.static_icons__default.rgba,
74
- cursor: t.isReadOnly || t.isDisabled ? "not-allowed" : "pointer"
48
+ width: "fit-content",
49
+ color: e.isReadOnly || e.isDisabled ? c.colors.text.static_icons__tertiary.rgba : c.colors.text.static_icons__default.rgba,
50
+ cursor: e.isReadOnly || e.isDisabled ? "not-allowed" : "pointer"
75
51
  },
76
52
  children: [
77
- /* @__PURE__ */ r(O, { children: /* @__PURE__ */ r("input", { ...f, ...l, ref: o }) }),
78
- /* @__PURE__ */ r(
53
+ /* @__PURE__ */ l(v, { children: /* @__PURE__ */ l("input", { ...o, ...f, ref: s }) }),
54
+ /* @__PURE__ */ l(
79
55
  "div",
80
56
  {
81
- style: { display: "inline", height: s ? 32 : 48 },
82
- ...b,
83
- children: /* @__PURE__ */ S(
57
+ style: { display: "inline", height: r ? 32 : 48 },
58
+ ...g,
59
+ children: /* @__PURE__ */ h(
84
60
  "svg",
85
61
  {
86
62
  width: 60,
87
- height: h,
63
+ height: m,
88
64
  "aria-hidden": "true",
89
65
  style: {
90
66
  marginRight: 4,
91
- opacity: t.isDisabled || t.isReadOnly ? 0.4 : 1
67
+ opacity: e.isDisabled || e.isReadOnly ? 0.4 : 1
92
68
  },
93
69
  children: [
94
- d && !t.isDisabled && !t.isReadOnly && /* @__PURE__ */ r(
70
+ u && !e.isDisabled && !e.isReadOnly && /* @__PURE__ */ l(
95
71
  "circle",
96
72
  {
97
73
  cx: i.isSelected ? 42 : 20,
98
- cy: 16 + y,
99
- r: s ? 16 : 20,
74
+ cy: 16 + a,
75
+ r: r ? 16 : 20,
100
76
  fill: n.ripple
101
77
  }
102
78
  ),
103
- /* @__PURE__ */ r(
79
+ /* @__PURE__ */ l(
104
80
  "rect",
105
81
  {
106
82
  x: 8,
107
- y: p,
83
+ y: S,
108
84
  width: 46,
109
85
  height: 24,
110
86
  rx: 12,
111
87
  fill: n.background
112
88
  }
113
89
  ),
114
- /* @__PURE__ */ r(
90
+ /* @__PURE__ */ l(
115
91
  "circle",
116
92
  {
117
93
  cx: i.isSelected ? 42 : 20,
118
- cy: 16 + y,
94
+ cy: 16 + a,
119
95
  r: 9.5,
120
96
  fill: n.circle
121
97
  }
122
98
  ),
123
- /* @__PURE__ */ r(
99
+ /* @__PURE__ */ l(
124
100
  "path",
125
101
  {
126
102
  fillRule: "evenodd",
127
103
  clipRule: "evenodd",
128
- d: i.isSelected ? m.check : m.close,
129
- transform: `translate(${i.isSelected ? 32 : 14},${(i.isSelected ? 6 : 10) + y}) scale(1.2)`,
104
+ d: i.isSelected ? y.check : y.close,
105
+ transform: `translate(${i.isSelected ? 32 : 14},${(i.isSelected ? 6 : 10) + a}) scale(1.2)`,
130
106
  fill: n.icon,
131
107
  style: { pointerEvents: "none" }
132
108
  }
133
109
  ),
134
- u && /* @__PURE__ */ r(
110
+ d && /* @__PURE__ */ l(
135
111
  "rect",
136
112
  {
137
113
  x: 6,
138
- y,
114
+ y: a,
139
115
  width: 50,
140
116
  height: 32,
141
117
  rx: 12,
@@ -150,11 +126,11 @@ function W(e) {
150
126
  )
151
127
  }
152
128
  ),
153
- t.children ?? t.label
129
+ e.children ?? e.label
154
130
  ]
155
131
  }
156
132
  );
157
133
  }
158
134
  export {
159
- W as Switch
135
+ F as Switch
160
136
  };
@@ -0,0 +1 @@
1
+ "use strict";const s=require("react");function E(o,p,l){let[b,i]=s.useState(o||p),u=s.useRef(o!==void 0),e=o!==void 0;s.useEffect(()=>{let t=u.current;t!==e&&process.env.NODE_ENV!=="production"&&console.warn(`WARN: A component changed from ${t?"controlled":"uncontrolled"} to ${e?"controlled":"uncontrolled"}.`),u.current=e},[e]);let n=e?o:b,$=s.useCallback((t,...f)=>{let a=(c,...r)=>{l&&(Object.is(n,c)||l(c,...r)),e||(n=c)};typeof t=="function"?(process.env.NODE_ENV!=="production"&&console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320"),i((r,...V)=>{let d=t(e?n:r,...V);return a(d,...f),e?r:d})):(e||i(t),a(t,...f))},[e,n,l]);return[n,$]}exports.$458b0a5536c1a7cf$export$40bfa8c7b0832715=E;
@@ -0,0 +1,30 @@
1
+ import { useState as V, useRef as $, useEffect as E, useCallback as N } from "react";
2
+ function h(o, a, l) {
3
+ let [p, s] = V(o || a), i = $(o !== void 0), e = o !== void 0;
4
+ E(() => {
5
+ let t = i.current;
6
+ t !== e && process.env.NODE_ENV !== "production" && console.warn(`WARN: A component changed from ${t ? "controlled" : "uncontrolled"} to ${e ? "controlled" : "uncontrolled"}.`), i.current = e;
7
+ }, [
8
+ e
9
+ ]);
10
+ let n = e ? o : p, b = N((t, ...u) => {
11
+ let f = (r, ...c) => {
12
+ l && (Object.is(n, r) || l(r, ...c)), e || (n = r);
13
+ };
14
+ typeof t == "function" ? (process.env.NODE_ENV !== "production" && console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320"), s((c, ...m) => {
15
+ let d = t(e ? n : c, ...m);
16
+ return f(d, ...u), e ? c : d;
17
+ })) : (e || s(t), f(t, ...u));
18
+ }, [
19
+ e,
20
+ n,
21
+ l
22
+ ]);
23
+ return [
24
+ n,
25
+ b
26
+ ];
27
+ }
28
+ export {
29
+ h as $
30
+ };