Skip to content

Commit

Permalink
improvements to permission picker component
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodes committed Aug 17, 2024
1 parent d4dadfb commit 237072a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
9 changes: 8 additions & 1 deletion addon/components/permission-picker.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
</div>
{{/if}}
</div>
<div>
<a href="javascript:;" class="ml-2 text-xs text-blue-500 hover:opacity-50" {{on "click" this.toggleSelected}}>Toggle Selected</a>
</div>
</div>
<div class="text-sm dark:text-gray-100">
{{t "iam.components.permission-picker.selected"}}
Expand All @@ -43,7 +46,11 @@
</div>
{{/unless}}
{{#each this.permissions as |permission|}}
<div class="grid grid-cols-12 text-xs py-2 px-3 text-gray-800 border-b border-gray-200 dark:border-gray-900 dark:text-gray-100 {{if @disabled 'opacity-50'}}">
<div
class="grid grid-cols-12 text-xs py-2 px-3 text-gray-800 border-b border-gray-200 dark:border-gray-900 dark:text-gray-100
{{if @disabled 'opacity-50'}}
{{if (and this.showSelectedOnly (not permission.selected)) 'hidden'}}"
>
<div class="flex items-center">
<Checkbox @value={{permission.selected}} @onToggle={{fn this.selectPermission permission}} @disabled={{@disabled}} />
</div>
Expand Down
41 changes: 38 additions & 3 deletions addon/components/permission-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ export default class PermissionPickerComponent extends Component {
*/
@tracked isPermissionsLoaded = false;

/**
* Only show selected permissions.
*
* @memberof PermissionPickerComponent
*/
@tracked showSelectedOnly = false;

/**
* The ID of each permission loaded.
*
Expand All @@ -86,14 +93,33 @@ export default class PermissionPickerComponent extends Component {
* Creates an instance of PermissionPickerComponent.
* @memberof PermissionPickerComponent
*/
constructor() {
constructor(owner, { selected = [], guard = 'sanctum' }) {
super(...arguments);

this.selected = isArray(this.args.selected) ? this.args.selected : [];
this.guard = this.args.guard ?? 'sanctum';
this.selected = this.getDefaultSelected(selected);
this.guard = guard ?? 'sanctum';
this.queryPermissions();
}

/**
* Get the default selected.
*
* @param {Array} [selected=[]]
* @return {Array}
* @memberof PermissionPickerComponent
*/
getDefaultSelected(selected = []) {
if (typeof selected.toArray === 'function') {
return selected.toArray();
}

if (isArray(selected)) {
return selected;
}

return [];
}

/**
* Selects all permissions.
*
Expand Down Expand Up @@ -183,6 +209,15 @@ export default class PermissionPickerComponent extends Component {
});
}

/**
* Toggles to only show selected or all.
*
* @memberof PermissionPickerComponent
*/
@action toggleSelected() {
this.showSelectedOnly = !this.showSelectedOnly;
}

/**
* Task initiates the search for permissions
*
Expand Down

0 comments on commit 237072a

Please sign in to comment.