@@ -30,10 +30,10 @@ export interface SelectCascaderProps {
3030 cols ?: number ;
3131 pickerItemStyle ?: StyleProp < TextStyle > ;
3232 headerStyle ?: StyleProp < ViewStyle > ;
33+ maskClosable ?: boolean ;
3334}
3435
3536export interface Istate {
36- modalVisible : boolean ;
3737 value : SelectCascaderValue ;
3838}
3939
@@ -44,29 +44,35 @@ export default class SelectCascader extends Component<SelectCascaderProps, Istat
4444 title : '请选择' ,
4545 disabled : false ,
4646 cols : 3 ,
47+ maskClosable : true ,
4748 } ;
4849 state = {
4950 value : this . getValue ( this . props . data , this . props . defaultValue || this . props . value ) ,
50- modalVisible : this . props . visible ,
5151 } ;
5252
53- outerCtrl = ( ) => {
54- this . setState ( {
55- modalVisible : ! this . state . modalVisible ,
56- } ) ;
57- } ;
58-
59- componentWillReceiveProps ( nextProps : SelectCascaderProps ) {
60- if ( 'value' in nextProps ) {
61- this . setState ( {
62- value : this . getValue ( nextProps . data , nextProps . value ) ,
63- } ) ;
64- }
65- if ( 'visible' in nextProps ) {
66- this . setState ( {
67- modalVisible : nextProps . visible ,
68- } ) ;
53+ static getDerivedStateFromProps ( props : SelectCascaderProps , state : Istate ) {
54+ if ( JSON . stringify ( props . value ) === JSON . stringify ( state . value ) ) {
55+ return null ;
6956 }
57+ const getValue = ( d : ICascaderDataItem [ ] , val : SelectCascaderValue | undefined ) => {
58+ let data = d || props . data ;
59+ let value = val || props . value || props . defaultValue ;
60+ if ( ! value || ! value . length || value . indexOf ( undefined ) > - 1 ) {
61+ value = [ ] ;
62+ for ( let i = 0 ; i < props . cols ! ; i ++ ) {
63+ if ( data && data . length ) {
64+ value [ i ] = data [ 0 ] . value ;
65+ if ( data [ 0 ] . children ) {
66+ data = data [ 0 ] . children ;
67+ }
68+ }
69+ }
70+ }
71+ return value ;
72+ } ;
73+ return {
74+ value : getValue ( props . data , props . value ) ,
75+ } ;
7076 }
7177
7278 getSel ( value : SelectCascaderValue ) {
@@ -164,13 +170,13 @@ export default class SelectCascader extends Component<SelectCascaderProps, Istat
164170 } ;
165171
166172 render ( ) {
167- const { title, dismissText, okText, onDismiss, headerStyle } = this . props ;
173+ const { title, dismissText, okText, onDismiss, headerStyle, visible , maskClosable } = this . props ;
168174 const cols = this . getCols ( ) ;
169175 return (
170176 < Modal
171- visible = { this . state . modalVisible }
177+ visible = { visible }
172178 onClosed = { ( ) => {
173- this . setState ( { modalVisible : false } ) ;
179+ maskClosable && this . props . onDismiss ?. ( ) ;
174180 } }
175181 >
176182 < >
0 commit comments