Closed jm06-zz closed 3 years ago
Thanks for your interest! What you’re trying to do should be very possible with the PreExpanded property of the react-accessible-accordion library. But I’m not able to get it to work either right now and feel like I'm missing something simple as well.
The AccordionItem props uuid values are indeed going to be zero-based, so a value of 0 is what you’d want to provide for the PreExpanded property to have the first AccordionItem be expanded on load while all the others are initially collapsed. So, you’re certainly on the right track there.
I’ll keep playing around with it and see if I can find some code modifications that would provide that behavior.
Hi @jm06 - I’ve come up with a way to pre-expand the first AccordionItem the way you describe.
You’ll have to modify 3 lines of code in this file: \src\webparts\reactAccordion\components\ReactAccordion.tsx
Within the REST API call to the selected SharePoint List, in addition to the values of the Title & Content columns we also need to pull in the values of the ID column. An ID column (usually hidden) is included with every List created in SharePoint. The first item you create in any SharePoint List is assigned an ID value of 1, the second one is assigned a value of 2 and so on.
To update the REST API call to include ID, change line 33 to this:
sp.web.lists.getById(this.props.listId).items.select("Title","Content","ID").get()
And now that our API call is also selecting the values for the ID column in the List we can set the preExpanded JSX attribute to 1 since the first item you add to your SharePoint List will automatically have an ID value of 1.
Change line 67 to:
<Accordion allowZeroExpanded preExpanded={[1]}>
And then we just need to set the uuid for each AccordionItem to the corresponding ID value for the equivalent item in the SharePoint List so that we pre-expand the first item in the accordion but not the others.
So, change line 70 to:
<AccordionItem uuid={item.ID}>
Caveat: Make sure not to delete the first item you add to your selected List or you’ll no longer have an item with ID=1 and as a result none of your AccordionItems will end up being expanded.
Hope that helps and thanks again for the great question!
Hi EJ
It is all very helpful though still not working 100% for me but it is great progress. Thank you for helping me.
This is what your code modified looks like for me in the ReactAccordion.tsx line 40
sp.web.lists.getById(this.props.listId).items.select("FAQ","Response", "ID").orderBy("Order1").get()
.then((results: Array
I tried to capture the ID from the first item in the Results array
I have tried what you suggested with no avail
Hi @jm06 -
I believe we're approaching the challenge from different angles. And I think the way you're looking at it is ultimately better since it would leverage the existing uuids from the react-accessible-accordion library, but I still can't get it to work that way myself either.
With my workaround of adding the SharePoint List ID to the REST call and keying off of that ID value, it does require the first List item that's added in SharePoint to the target List not to ever be deleted, but other than that that approach should work by just replacing out the 3 lines I mention in my response (but not including any of your changes). Just wanted to make sure that when you (or anyone else following this thread) who's trying my suggestion is only making those 3 code changes and not any other ones.
I'm going to close the issue since it's also more of a question of a new variation of the code and in a lot of ways related to the react-accessible-accordion library itself, but I'm definitely going to keep thinking about it & if I come up with additional ideas, I'll definitely add them to the thread. And, likewise, please do the same if you have any breakthroughs.
I've heard GitHub is adding a Discussion feature and I think this would be a perfect topic on this Repo once that's available since it would likely get more visibility from other developers who might have some additional ideas on this.
Best, Erik
This is a great piece of code. I am a newbie in that language. I am trying to get the First item to be selected/preExpanded at loading. Not really getting anywhere with below
preExpanded: string[] [optional, default: []] Accepts an array of strings and any AccordionItem whose uuid prop matches any one of these strings will be expanded on mount.
nor how to implement that piece of code I found while searching for an answer.
I also found the AccordionItem class with the instanceUuid property
export default class AccordionItem extends React.Component {
static defaultProps: typeof defaultProps;
static displayName: DisplayName.AccordionItem;
instanceUuid: UUID;
renderChildren: (itemContext: ItemContext) => JSX.Element;
render(): JSX.Element;
}
As items/results is a zero based collection, would have thought that items(0).selected=true or items(0).preExpanded=True.
Could anyone point me into the right direction or provides the line of code? it is going to be so simple that I feel embarrassed to ask. Thanking you in advance