UNPKG

@shopgate/engage

Version:
12 lines 5.12 kB
function _typeof(obj){if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){_typeof=function _typeof(obj){return typeof obj;};}else{_typeof=function _typeof(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj;};}return _typeof(obj);}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor;}function _callSuper(_this,derived,args){function isNativeReflectConstruct(){if(typeof Reflect==="undefined"||!Reflect.construct)return false;if(Reflect.construct.sham)return false;if(typeof Proxy==="function")return true;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}));}catch(e){return false;}}derived=_getPrototypeOf(derived);return _possibleConstructorReturn(_this,isNativeReflectConstruct()?Reflect.construct(derived,args||[],_getPrototypeOf(_this).constructor):derived.apply(_this,args));}function _possibleConstructorReturn(self,call){if(call&&(_typeof(call)==="object"||typeof call==="function")){return call;}return _assertThisInitialized(self);}function _assertThisInitialized(self){if(self===void 0){throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return self;}function _getPrototypeOf(o){_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function _getPrototypeOf(o){return o.__proto__||Object.getPrototypeOf(o);};return _getPrototypeOf(o);}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function");}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,writable:true,configurable:true}});if(superClass)_setPrototypeOf(subClass,superClass);}function _setPrototypeOf(o,p){_setPrototypeOf=Object.setPrototypeOf||function _setPrototypeOf(o,p){o.__proto__=p;return o;};return _setPrototypeOf(o,p);}function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true});}else{obj[key]=value;}return obj;}import React,{PureComponent,Fragment}from'react';import PropTypes from'prop-types';import classNames from'classnames';import Transition from'react-transition-group/Transition';import{VariantSwatch}from'@shopgate/engage/product/components';import styles from"./style";import transition from"../transition";/** * A single characteristic swatch type. */var Swatch=/*#__PURE__*/function(_PureComponent){function Swatch(){var _this2;_classCallCheck(this,Swatch);for(var _len=arguments.length,args=new Array(_len),_key=0;_key<_len;_key++){args[_key]=arguments[_key];}_this2=_callSuper(this,Swatch,[].concat(args));_defineProperty(_this2,"state",{highlight:false});/** * @param {string} charLabel The default button label. * @return {string} */_defineProperty(_this2,"getLabel",function(charLabel){if(!_this2.props.selected){return charLabel;}var value=_this2.props.values.find(function(val){return val.id===_this2.props.selected;});return"".concat(charLabel," - ").concat(value.label);});/** * @param {string} valueId The ID of the selected value. */_defineProperty(_this2,"handleItemSelection",function(valueId){_this2.props.select({id:_this2.props.id,value:valueId});});_defineProperty(_this2,"removeHighlight",function(){_this2.setState({highlight:false});});return _this2;}_inherits(Swatch,_PureComponent);return _createClass(Swatch,[{key:"UNSAFE_componentWillReceiveProps",value:/** * @param {Object} nextProps The next component props. */function UNSAFE_componentWillReceiveProps(nextProps){this.setState({highlight:nextProps.highlight});}},{key:"render",value:/** * @return {JSX} */function render(){var _this3=this;var _this$props=this.props,id=_this$props.id,disabled=_this$props.disabled,charRef=_this$props.charRef,label=_this$props.label,values=_this$props.values;var swatch={id:id,label:label,values:values};return React.createElement(Fragment,null,React.createElement(Transition,{"in":this.state.highlight,timeout:500,onEntered:this.removeHighlight},function(state){return React.createElement("div",{"aria-hidden":true,className:classNames(styles.label,_defineProperty({},styles.labelDisabled,disabled)),ref:charRef,style:transition[state],"data-test-id":label},_this3.getLabel(label));}),React.createElement("div",{className:styles.items},React.createElement(VariantSwatch,{swatch:swatch,onClick:this.handleItemSelection})));}}]);}(PureComponent);_defineProperty(Swatch,"defaultProps",{selected:null});export default Swatch;