@useloops/design-system
Version:
The official React based Loops design system
2 lines (1 loc) • 2.12 kB
JavaScript
;var e=require("react/jsx-runtime"),l=require("@mui/material"),i=require("react"),t=require("../FormControlLabel/FormControlLabel.js"),n=require("../Checkbox/StyledCheckbox.js"),r=require("../TextField/TextField.js");module.exports=({sizing:o,options:s,other:a,internalChange:u,value:c,inputRef:d,onChange:h,maxSelections:x,noneOfTheseOption:b})=>{const f=a?.name||"other-option",m=b?.value||"none-of-these-option",p=`${f}-value-`,g=e=>`${p}${e}`,j=a?.enabled,v=c?.includes(f)&&j,C=c?.find(e=>e.includes(p)),k=i.useMemo(()=>c?c.filter(e=>!e.startsWith(p)).length:0,[c,p]),q=!!x&&k>=x;if(!c)return e.jsx("div",{});const F=(e,l)=>{const i=c.includes(e);if(!i&&q&&!l)return;let t;i?t=c.filter(l=>l!==e):l?t=[e]:(t=[...new Set(c),e],t=t.filter(e=>!s.find(l=>l?.exclusive&&e===l.value)),b&&(t=t.filter(e=>e!==m))),a?.enabled&&(l?t=t.filter(e=>e!==f&&!e.includes(p)):e===f&&i&&(t=t.filter(e=>!e.includes(p)))),h(t),u&&u()},M=(e,l)=>!l&&(q&&!c.includes(e));return e.jsxs(e.Fragment,{children:[e.jsxs(l.FormGroup,{sx:{display:"flex",gap:e=>e.custom.margin.sm,label:{"&:last-child":{mb:0},"&:not(.Mui-disabled):hover":{"& .MuiCheckbox-root":{backgroundColor:e=>e.custom.palette.interaction.hover,"&.Mui-checked":{backgroundColor:e=>e.custom.palette.purple[500]}}}}},children:[s?.map(l=>{const i=l.value,r=M(i,l.exclusive);return e.jsx(t,{sx:{"& .MuiButtonBase-root.MuiCheckbox-root":{alignSelf:"baseline"},"& .MuiTypography-root":{mt:"default"!==o?.5:0}},onChange:()=>F(i,l.exclusive),checked:c.includes(i),disabled:r,control:e.jsx(n,{sizing:o,disabled:r}),label:l.label},i)}),j&&e.jsx(t,{inputRef:d,onChange:()=>F(f),label:a?.label||"Other",checked:c.includes(f),disabled:M(f),control:e.jsx(n,{sizing:o})},f),b&&e.jsx(t,{onChange:()=>F(m,!0),checked:c.includes(m),control:e.jsx(n,{sizing:o}),label:b.label},m)]}),v&&e.jsx(l.Box,{sx:{mt:1},children:e.jsx(r,{fullWidth:!0,autoFocus:!0,defaultValue:C?C.replace(p,""):"",placeholder:a?.placeholder,onChange:e=>{const l=c.find(e=>e.includes(p)),i=(l?c.map(l=>l.includes(p)?g(e.target.value):l):[...c,g(e.target.value)]).filter(e=>e!==p);h(i),u&&u()},required:!0})})]})};