UNPKG

@progress/kendo-react-buttons

Version:

All you need in React Button in one package: disabled/enabled states, built-in styles and more. KendoReact Buttons package

9 lines (8 loc) 8.18 kB
/** * @license *------------------------------------------------------------------------------------------- * Copyright © 2025 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the package root for more information *------------------------------------------------------------------------------------------- */ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const N=require("react"),i=require("prop-types"),M=require("../Button.js"),e=require("@progress/kendo-react-common"),R=require("./utils/navigation.js"),A=require("./DropDownButtonItem.js"),T=require("./ButtonItem.js"),q=require("@progress/kendo-react-popup"),m=require("./utils/popup.js"),f=require("../package-metadata.js");function L(d){const o=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(d){for(const t in d)if(t!=="default"){const s=Object.getOwnPropertyDescriptor(d,t);Object.defineProperty(o,t,s.get?s:{enumerable:!0,get:()=>d[t]})}}return o.default=d,Object.freeze(o)}const u=L(N),j=12e3,z=2e3,l=class l extends u.Component{constructor(o){super(o),this.state={opened:!1,focused:!1,focusedIndex:-1},this.mainButton=null,this.list=null,this.skipFocus=!1,this.showLicenseWatermark=!1,this.buttonsData=[],this.onKeyDown=t=>{const{focusedIndex:s}=this.state;if(t.altKey){!this.opened&&t.keyCode===e.Keys.down?(t.preventDefault(),this.setOpen(!0,t),this.setState({focusedIndex:0})):this.opened&&t.keyCode===e.Keys.up&&(t.preventDefault(),this.setState({focusedIndex:-1}),this.setOpen(!1,t));return}const n={...this.state};if(t.keyCode===e.Keys.enter||t.keyCode===e.Keys.space){t.preventDefault(),s>=0&&this.dispatchClickEvent(t,s);const a=!this.opened;this.setState({focused:!0,focusedIndex:a?0:-1}),this.setOpen(a,t);return}if(this.opened&&t.keyCode===e.Keys.esc){this.setState({focusedIndex:-1}),this.setOpen(!1,t);return}if(this.opened){const a=R.navigate(s,t.keyCode,t.altKey,this.buttonsData.length);n.focusedIndex=a;const p=t.keyCode===e.Keys.up||t.keyCode===e.Keys.down||t.keyCode===e.Keys.left||t.keyCode===e.Keys.right;!t.altKey&&(p||t.keyCode===e.Keys.home||t.keyCode===e.Keys.end)&&t.preventDefault()}this.setState(n)},this.switchFocus=t=>{this.skipFocus=!0,t(),window.setTimeout(()=>this.skipFocus=!1,0)},this.handleFocus=t=>{this.skipFocus||(this.setState({focused:!0,focusedIndex:this.opened?0:-1}),e.dispatchEvent(this.props.onFocus,t,this,void 0))},this.handleButtonBlur=t=>{this.opened||(this.setState({focused:!1}),e.dispatchEvent(this.props.onBlur,t,this,void 0))},this.handleMenuBlur=t=>{this.skipFocus||(this.setState({focused:!1,focusedIndex:-1}),e.dispatchEvent(this.props.onBlur,t,this,void 0),setTimeout(()=>{this.setOpen(!1,t)},0))},this.setOpen=(t,s)=>{this.opened!==t&&(this.openedDuringOnChange=t,this.setState({opened:t}),s?this.dispatchPopupEvent(s,t):this.openedDuringOnChange=void 0)},this.onItemClick=(t,s)=>{this.setState({focusedIndex:-1}),this.dispatchClickEvent(t,s),this.setOpen(!1,t)},this.onItemDown=t=>{e.getActiveElement(document)===this.list&&t.preventDefault()},this.mouseDown=t=>{t.preventDefault();const s=e.getActiveElement(document);this.element&&s!==this.element&&s!==this.list&&this.element.focus()},this.onPopupClose=t=>{var n;const s=e.getActiveElement(document);this.element&&this.element.removeAttribute("tabindex"),(s===this.list||(n=this.list)!=null&&n.contains(s))&&this.switchFocus(()=>{this.element&&this.element.focus({preventScroll:!0})}),this.props.popupSettings&&this.props.popupSettings.onClose&&this.props.popupSettings.onClose.call(void 0,t)},this.listRef=t=>{this.list=t,t&&this.state.focused&&this.switchFocus(()=>{t.focus({preventScroll:!0}),this.element&&(this.element.tabIndex=-1)})},this.onClickMainButton=t=>{if(e.dispatchEvent(this.props.onClick,t,this,void 0),!this.buttonsData.length)return;const s=!this.opened;this.setState({focused:!0,focusedIndex:s?0:-1}),this.setOpen(s,t)},this.dispatchPopupEvent=(t,s)=>{e.dispatchEvent(s?this.props.onOpen:this.props.onClose,t,this,void 0),this.openedDuringOnChange=void 0},this.showLicenseWatermark=!e.validatePackage(f.packageMetadata,{component:"DropDownButton"}),this.licenseMessage=e.getLicenseMessage(f.packageMetadata)}get guid(){return this.props.id+"-accessibility-id"}get opened(){return this.openedDuringOnChange!==void 0?this.openedDuringOnChange:this.props.opened===void 0?this.state.opened:this.props.opened}render(){const o=this.isRtl(),t=this.props.unstyled&&this.props.unstyled.uDropDownButton,s=o?"rtl":void 0,{id:n,style:a,tabIndex:p,disabled:r,size:C,rounded:y,fillMode:k,themeColor:w,ariaLabel:I,title:b,accessKey:B,icon:O,svgIcon:E,iconClass:v,buttonClass:S,className:x,imageUrl:P,startIcon:K,endIcon:F}=this.props;return this.buttonsData=this.props.items||u.Children.toArray(this.props.children).filter(c=>c&&c.type===A.DropDownButtonItem).map(c=>c.props),u.createElement(u.Fragment,null,u.createElement(M.Button,{id:n,size:C,style:a,rounded:y,fillMode:k,themeColor:w,onClick:this.onClickMainButton,onMouseDown:this.mouseDown,onKeyDown:this.onKeyDown,onFocus:this.handleFocus,onBlur:this.handleButtonBlur,"aria-disabled":r?"true":void 0,tabIndex:p,accessKey:B,icon:O,svgIcon:E,iconClass:v,className:e.classNames(S,x,e.uDropDownButton.wrapper({c:t,focused:this.state.focused,disabled:r})),imageUrl:P,dir:s,ref:c=>{this.mainButton=(c==null?void 0:c.element)||null},type:"button","aria-expanded":this.opened?"true":"false","aria-label":I,"aria-controls":this.opened?this.guid:void 0,title:b,startIcon:K,endIcon:F},this.props.text),this.showLicenseWatermark&&u.createElement(e.WatermarkOverlay,{message:this.licenseMessage}),this.renderPopup(o))}componentDidMount(){(this.props.dir===void 0&&this.isRtl()||this.opened)&&this.forceUpdate()}get element(){return this.mainButton}dispatchClickEvent(o,t){this.isItemDisabled(t)||e.dispatchEvent(this.props.onItemClick,o,this,{item:this.buttonsData[t],itemIndex:t})}renderPopup(o){const{popupSettings:t={},_zIndex:s,unstyled:n}=this.props,a=s?s+z:j,p=n&&n.uDropDownButton,{focusedIndex:r}=this.state;return u.createElement(e.ZIndexContext.Provider,{value:a},u.createElement(q.Popup,{anchor:this.mainButton,show:this.opened,animate:t.animate,popupClass:e.classNames(e.uDropDownButton.popup({c:p}),t.popupClass),anchorAlign:t.anchorAlign||m.getAnchorAlign(o),popupAlign:t.popupAlign||m.getPopupAlign(o),style:o?{direction:"rtl"}:void 0,onClose:this.onPopupClose},u.createElement("ul",{role:"list",id:this.guid,tabIndex:-1,"aria-activedescendant":r>=0?`${this.guid}-${r}`:void 0,ref:this.listRef,onKeyDown:this.onKeyDown,onBlur:this.handleMenuBlur,className:e.classNames(e.uDropDownButton.ul({c:p,size:this.props.size}))},this.renderChildItems())))}renderChildItems(){const{item:o,itemRender:t,textField:s,unstyled:n}=this.props,a=n&&n.uDropDownButton;return this.buttonsData.length>0?this.buttonsData.map((p,r)=>u.createElement(T.ButtonItem,{className:e.classNames(e.uDropDownButton.item({c:a}),p.className,{"k-first":r===0},{"k-last":r===this.buttonsData.length-1}),dataItem:p,textField:s,focused:this.state.focusedIndex===r,onClick:this.onItemClick,onDown:this.onItemDown,render:t,item:o,index:r,key:r,id:`${this.guid}-${r}`})):null}isItemDisabled(o){return this.buttonsData[o]?this.buttonsData[o].disabled:this.props.disabled}isRtl(){return this.props.dir!==void 0?this.props.dir==="rtl":!!this.mainButton&&getComputedStyle(this.mainButton).direction==="rtl"}};l.propTypes={accessKey:i.string,ariaLabel:i.string,title:i.string,onFocus:i.func,onBlur:i.func,onClick:i.func,onItemClick:i.func,onOpen:i.func,onClose:i.func,items:i.arrayOf(i.any),textField:i.string,tabIndex:i.number,disabled:i.bool,icon:i.string,svgIcon:e.svgIconPropType,iconClass:i.string,imageUrl:i.string,popupSettings:i.object,itemRender:i.func,item:i.func,className:i.string,buttonClass:i.string,dir:i.string},l.defaultProps={size:"medium",rounded:"medium",fillMode:"solid",themeColor:"base"};let h=l;const g=e.createPropsContext(),D=e.withIdHOC(e.withPropsContext(g,e.withUnstyledHOC(e.withZIndexContext(h))));D.displayName="KendoReactDropDownButton";exports.DropDownButton=D;exports.DropDownButtonPropsContext=g;exports.DropDownButtonWithoutContext=h;