11import { createElement , ReactElement , useState , useCallback , useEffect } from "react" ;
2- import { View } from "react-native" ;
2+ import { View , LayoutAnimation , Platform , UIManager } from "react-native" ;
33import { flattenStyles } from "@mendix/piw-native-utils-internal" ;
44import { executeAction } from "@mendix/piw-utils-internal" ;
55import { ValueStatus } from "mendix" ;
@@ -10,6 +10,10 @@ import { AccordionGroup } from "./components/AccordionGroup";
1010
1111export type Props = AccordionProps < AccordionStyle > ;
1212
13+ if ( Platform . OS === "android" && UIManager . setLayoutAnimationEnabledExperimental ) {
14+ UIManager . setLayoutAnimationEnabledExperimental ( true ) ;
15+ }
16+
1317export function Accordion ( props : Props ) : ReactElement | null {
1418 const styles = flattenStyles ( defaultAccordionStyle , props . style ) ;
1519 const [ expandedGroups , setExpandedGroups ] = useState < number [ ] > (
@@ -41,15 +45,20 @@ export function Accordion(props: Props): ReactElement | null {
4145
4246 const onPressGroupHeader = useCallback (
4347 ( group : GroupsType , index : number ) : void => {
48+ LayoutAnimation . easeInEaseOut ( ) ;
4449 const expanded = expandedGroups . includes ( index ) ;
50+ let newExpandedGroup : number [ ] = [ ] ; // use new expanded group, as we need to state before we call execute action.
4551 if ( expanded ) {
52+ newExpandedGroup = expandedGroups . filter ( i => i !== index ) ;
4653 collapseGroup ( index ) ;
4754 } else {
55+ newExpandedGroup = props . collapseBehavior === "singleExpanded" ? [ index ] : [ ...expandedGroups , index ] ;
4856 expandGroup ( index ) ;
4957 }
58+ props . groups . forEach ( ( g , i ) => g . groupCollapsedAttribute ?. setValue ( ! newExpandedGroup . includes ( i ) ) ) ;
5059 executeAction ( group . groupOnChange ) ;
5160 } ,
52- [ expandedGroups , expandGroup , collapseGroup ]
61+ [ expandedGroups , props . groups , props . collapseBehavior , collapseGroup , expandGroup ]
5362 ) ;
5463
5564 const checkPropertyValues = ( group : GroupsType , i : number ) : void => {
0 commit comments