UNPKG

@styleless-ui/react

Version:

Completely unstyled, headless and accessible React UI components.

1 lines 2.81 kB
var e=this&&this.__assign||function(){return e=Object.assign||function(e){for(var r,l=1,t=arguments.length;l<t;l++)for(var a in r=arguments[l])Object.prototype.hasOwnProperty.call(r,a)&&(e[a]=r[a]);return e},e.apply(this,arguments)},r=this&&this.__rest||function(e,r){var l={};for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&r.indexOf(t)<0&&(l[t]=e[t]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var a=0;for(t=Object.getOwnPropertySymbols(e);a<t.length;a++)r.indexOf(t[a])<0&&Object.prototype.propertyIsEnumerable.call(e,t[a])&&(l[t[a]]=e[t[a]])}return l};import{jsx as l,jsxs as t}from"react/jsx-runtime";import*as a from"react";import{componentWithForwardedRef as i,useControlledProp as n,useDeterministicId as o,useForkedRefs as s}from"../utils";import u from"./context";import*as d from"./slots";var c=i((function(i,c){var b=i.label,f=i.children,v=i.id,p=i.classes,h=i.defaultValue,y=i.value,g=i.onChange,m=i.orientation,O=void 0===m?"vertical":m,j=r(i,["label","children","id","classes","defaultValue","value","onChange","orientation"]),x=a.useRef(null),L=s(c,x),_=o(v,"styleless-ui__radio-group"),R=_?"".concat(_,"__label"):void 0,w=function(e){var r={};if("string"==typeof e)r.visibleLabel=e;else if("screenReaderLabel"in e)r.srOnlyLabel=e.screenReaderLabel;else{if(!("labelledBy"in e))throw new Error(["[StylelessUI][RadioGroup]: Invalid `label` property.","The `label` property must be either a `string` or in shape of `{ screenReaderLabel: string; } | { labelledBy: string; }`"].join("\n"));r.labelledBy=e.labelledBy}return r}(b),A=n(y,h,""),B=A[0],P=A[1],E=[];return a.useEffect((function(){var e,r;if(x.current){E.forEach((function(e){var r=e[0],l=e[1];if(l.current){var t=Array.isArray(B)?B.includes(r):B===r,a=l.current.hasAttribute("disabled");l.current.tabIndex=a?-1:t?0:-1}}));var l=E.filter((function(e){var r;e[0];return"0"!==(null===(r=e[1].current)||void 0===r?void 0:r.getAttribute("tabindex"))}));if(l.length===E.length){var t=null!==(e=E.find((function(e){var r;e[0];return!(null===(r=e[1].current)||void 0===r?void 0:r.hasAttribute("disabled"))})))&&void 0!==e?e:null;if(t){t[0];null===(r=t[1].current)||void 0===r||r.setAttribute("tabindex","0")}}}})),l("div",e({},j,{id:_,ref:L,className:null==p?void 0:p.root,"data-slot":d.Root},{children:t(u.Provider,e({value:{value:B,onChange:function(e,r){e&&(P(r),null==g||g(r))},registerRadio:function(e,r){E.some((function(r){return r[0]===e}))||E.push([e,r])},radios:E}},{children:[w.visibleLabel&&l("span",e({id:R,"data-slot":d.Label,className:null==p?void 0:p.label},{children:w.visibleLabel})),l("div",e({role:"radiogroup","data-slot":d.Group,className:null==p?void 0:p.group,"aria-label":w.srOnlyLabel,"aria-orientation":O,"aria-labelledby":w.visibleLabel?R:w.labelledBy},{children:f}))]}))}))}));export default c;