feat(settings): Add action enabled callback

Signed-off-by: Christopher Ng <chrng8@gmail.com>
This commit is contained in:
Christopher Ng 2024-06-05 15:05:39 -07:00
parent 94bc020d07
commit 1de77edfdf
2 changed files with 14 additions and 4 deletions

View file

@ -15,7 +15,7 @@
<NcIconSvgWrapper :key="editSvg" :svg="editSvg" aria-hidden="true" />
</template>
</NcActionButton>
<NcActionButton v-for="({ action, icon, text }, index) in actions"
<NcActionButton v-for="({ action, icon, text }, index) in enabledActions"
:key="index"
:disabled="disabled"
:aria-label="text"
@ -38,8 +38,9 @@ import SvgPencil from '@mdi/svg/svg/pencil.svg?raw'
interface UserAction {
action: (event: MouseEvent, user: Record<string, unknown>) => void,
enabled?: (user: Record<string, unknown>) => boolean,
icon: string,
text: string
text: string,
}
export default defineComponent({
@ -87,9 +88,16 @@ export default defineComponent({
/**
* Current MDI logo to show for edit toggle
*/
editSvg() {
editSvg(): string {
return this.edit ? SvgCheck : SvgPencil
},
/**
* Enabled user row actions
*/
enabledActions(): UserAction[] {
return this.actions.filter(action => typeof action.enabled === 'function' ? action.enabled(this.user) : true)
},
},
methods: {

View file

@ -80,13 +80,15 @@ export default defineComponent({
* @param {string} icon the icon class
* @param {string} text the text to display
* @param {Function} action the function to run
* @param {(user: Record<string, unknown>) => boolean} enabled return true if the action is enabled for the user
* @return {Array}
*/
registerAction(icon, text, action) {
registerAction(icon, text, action, enabled) {
this.externalActions.push({
icon,
text,
action,
enabled,
})
return this.externalActions
},