2016-03-09 19 views
5

tôi sử dụng để viết các thành phần với nhiều cấu hình Ví dụ:Làm thế nào để khai báo Phản ứng PropTypes XOR

ResponsiveTable.PropTypes = { 
    width: React.PropTypes.number, //used if widthOffset and minWidth are undefined 
    widthOffset: React.PropTypes.number, //used if width is undefined 
    minWidth: React.PropTypes.number, //used if width is undefined 
}; 

Làm thế nào tôi có thể tuyên bố đạo cụ có thể được sử dụng chỉ khi có i snot đã đạo cụ khác thiết lập?

Tùy chọn XOR sẽ hữu ích. Tôi đọc số https://facebook.github.io/react/docs/reusable-components.html nhưng không hiệu quả.

Bất kỳ ý tưởng nào?

Trả lời

2

Solution:

React.PropTypes.oneOfType([ 
    React.PropTypes.shape({ 
     width: React.PropTypes.number.isRequired 
    }), 
    React.PropTypes.shape({ 
     widthOffset: React.PropTypes.number, 
     minWidth: React.PropTypes.number.isRequired 
    }), 
]) 
+0

Nó có vẻ như điều này sẽ chỉ làm việc như một xác nhận XOR của các thuộc tính của, chẳng hạn, một đối tượng được truyền cho thành phần của bạn, như trái ngược để xác nhận XOR các đạo cụ được chuyển đến thành phần của bạn. Tôi có hiểu nhầm điều này và/hoặc bạn có biết thêm chi tiết về cách bạn sử dụng giải pháp này để giải quyết câu hỏi ban đầu của mình không? – aarontam

+0

Vâng, giải pháp đơn giản này đòi hỏi phải kiểm tra một prop cụ thể có chứa etiher 'width' hoặc' minWidth' và tùy chọn 'widthOffset'. Nó buộc bạn phải bọc các đặc tính để kiểm tra trong một giá đỡ cụ thể. –

2

Tôi đã thử các customProp. Tôi đã nhận được một cái gì đó như thế:

/** 
* Configure a React type to be usable only if including ot exclufing other props from component 
* @param {React.PropTypes} propType  current prop type 
* @param {Array} excludedProps names of the props to exclude 
* @param {Array} includedProps name of the props to include 
*/ 
function propTypeXOR(propType,excludedProps,includedProps){ 
    return(props, propName, componentName) =>{ 
    if(props[propName]){ 
     if(typeof props[propName] !== propType){ 
     return new Error("Failed propType: Invalid prop `"+propName+"` of type `"+propType+"` supplied to `"+componentName+"`, expected `number`"); 
     }else{ 
     excludedProps.map((excludedPropName) =>{ 
      if(props[excludedPropName]){ 
      return new Error("forbidden prop `"+excludedPropName+"` was specified in `"+componentName+"` when using the prop `"+propName+"`"); 
      } 
     }) 
     if(includedProps){ 
      includedProps.map((includedPropName) =>{ 
      if(props[includedPropName]){ 
       return new Error("required prop `"+includedPropName+"` was not specified in `"+componentName+"` when using the prop `"+propName+"`"); 
      } 
      }) 
     } 
     } 
    }else{ 
     if(excludedProps){ 
     var error = ""; 
     excludedProps.map((excludedPropName) =>{ 
      if(!props[excludedPropName]){ 
      error+="`"+excludedPropName+"`,"; 
      } 
     }) 
     if(error!=""){ 
      return new Error("required prop `"+propName+"` was not specified in `"+componentName+"`.It is required when props ["+error+"] are not defined."); 
     } 

     } 
    } 
    } 
} 

ResponsiveTable.propTypes = { 
    width: propTypeXOR("number",["widthOffset","minWidth"]), 
    widthOffset: propTypeXOR("number",["width"],["minWidth"]), 
    minWidth: propTypeXOR("number",["width"],["widthOffset"]) 
}; 

Nó đang hoạt động: người dùng phải khai báo bằng hoặc chiều rộngBài đặt và minWidth. Nhưng tôi nghĩ rằng một giải pháp nhúng hơn sẽ dễ dàng tuyên bố và sẽ cải thiện lỗi được nêu ra.

tôi đăng các nhu cầu trên react github

Các vấn đề liên quan