1- import React , { PureComponent } from 'react' ;
1+ import React , { Component } from 'react' ;
22import PropTypes from 'prop-types' ;
33import { translate } from 'react-i18next' ;
44import classnames from 'classnames' ;
55
6+ import { find } from 'lodash' ;
7+
68import styles from './index.scss' ;
79
810@translate ( )
9- export default class GroupCard extends PureComponent {
11+ export default class GroupCard extends Component {
1012 static propTypes = {
1113 groups : PropTypes . array ,
1214 selectValue : PropTypes . string ,
@@ -15,20 +17,52 @@ export default class GroupCard extends PureComponent {
1517
1618 static defaultProps = {
1719 groups : [ ] ,
18- selectValue : ''
20+ selectValue : '' // default select
1921 } ;
2022
23+ constructor ( props ) {
24+ super ( props ) ;
25+
26+ this . state = {
27+ curVal : props . selectValue || props . groups [ 0 ] . value
28+ } ;
29+ }
30+
31+ componentDidMount ( ) {
32+ const { groups } = this . props ;
33+ const { curVal } = this . state ;
34+
35+ this . props . selectCard ( find ( groups , { value : curVal } ) ) ;
36+ }
37+
38+ shouldComponentUpdate ( nextProps , nextState ) {
39+ return nextState . curVal !== this . state . curVal ;
40+ }
41+
42+ handleChange = ( { value } ) => {
43+ this . setState ( { curVal : value } ) ;
44+ } ;
45+
46+ componentDidUpdate ( ) {
47+ const { groups } = this . props ;
48+ const { curVal } = this . state ;
49+
50+ this . props . selectCard ( find ( groups , { value : curVal } ) ) ;
51+ }
52+
2153 render ( ) {
22- const { groups, selectCard, selectValue, t } = this . props ;
54+ const { groups, t } = this . props ;
55+ const { curVal } = this . state ;
56+
2357 return (
2458 < ul className = { styles . groupCard } >
2559 { groups . map ( ( data , index ) => (
2660 < li
2761 key = { data . id || index }
2862 onClick = { ( ) => {
29- selectCard ( data ) ;
63+ this . handleChange ( data ) ;
3064 } }
31- className = { classnames ( { [ styles . current ] : data . value === selectValue } ) }
65+ className = { classnames ( { [ styles . current ] : data . value === curVal } ) }
3266 >
3367 < div className = { styles . name } > { t ( data . name ) } </ div >
3468 { data . id && < div className = { styles . id } > id:{ data . id } </ div > }
0 commit comments