Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Accordion): emit open event with index #1559

Merged
merged 1 commit into from
Mar 25, 2024
Merged

Conversation

noook
Copy link
Collaborator

@noook noook commented Mar 25, 2024

πŸ”— Linked issue

NA

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This PR introduces a new event for UAccordion so that a user can listen to when an accordion item was opened.

This could allow the user to lazy load an accordion item's content when opened, for example.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

const { ui, attrs } = useUI('accordion', toRef(props, 'ui'), config, toRef(props, 'class'))

const uiButton = computed<typeof configButton>(() => configButton)

const buttonRefs = ref<{ open: boolean, close: (e: EventTarget) => {} }[]>([])

const openedStates = computed(() => buttonRefs.value.map(({ open }) => open))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't read directly the button refs in the watcher, because old and new value were identical, I don't know the reason. I had to use this intermediary state to effectively detect the changes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you use a function in the watcher? πŸ€”

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just passed the refs like this and the values were always equal

watch(buttonRefs, ...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an idea but what if you use watch(() => buttonRefs.value, ...)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tried, the exact same thing is happening. I wonder if this is not because the refs are still references to the dom and updated before the watcher's callback is ran, whereas the computed is only updated on the next tick

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might be the case yes. I don't mind having a computed anyway.

@noook noook changed the title feat: emit open event when opening accordion item feat(Accordion): emit open event when opening accordion item Mar 25, 2024
@benjamincanac benjamincanac changed the title feat(Accordion): emit open event when opening accordion item feat(Accordion): emit open event with index Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants