UserGroupDropdown.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import type { FC } from 'react';
  2. import React, { useCallback } from 'react';
  3. import type { IUserGroupHasId } from '@growi/core';
  4. import { useTranslation } from 'next-i18next';
  5. type Props = {
  6. selectableUserGroups?: IUserGroupHasId[]
  7. onClickAddExistingUserGroupButton?(userGroup: IUserGroupHasId | null): void
  8. onClickCreateUserGroupButton?(): void
  9. };
  10. export const UserGroupDropdown: FC<Props> = (props: Props) => {
  11. const { t } = useTranslation();
  12. const { selectableUserGroups, onClickAddExistingUserGroupButton, onClickCreateUserGroupButton } = props;
  13. const onClickAddExistingUserGroupButtonHandler = useCallback((userGroup: IUserGroupHasId) => {
  14. if (onClickAddExistingUserGroupButton != null) {
  15. onClickAddExistingUserGroupButton(userGroup);
  16. }
  17. }, [onClickAddExistingUserGroupButton]);
  18. const onClickCreateUserGroupButtonHandler = useCallback(() => {
  19. if (onClickCreateUserGroupButton != null) {
  20. onClickCreateUserGroupButton();
  21. }
  22. }, [onClickCreateUserGroupButton]);
  23. return (
  24. <>
  25. <div className="dropdown">
  26. <button className="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown">
  27. {t('admin:user_group_management.add_child_group')}
  28. </button>
  29. <div className="dropdown-menu" aria-labelledby="dropdownMenuButton">
  30. {
  31. (selectableUserGroups != null && selectableUserGroups.length > 0) && (
  32. <>
  33. {
  34. selectableUserGroups.map(userGroup => (
  35. <button
  36. key={userGroup._id}
  37. type="button"
  38. className="dropdown-item"
  39. onClick={() => onClickAddExistingUserGroupButtonHandler(userGroup)}
  40. >
  41. {userGroup.name}
  42. </button>
  43. ))
  44. }
  45. <div className="dropdown-divider"></div>
  46. </>
  47. )
  48. }
  49. <button
  50. className="dropdown-item"
  51. type="button"
  52. onClick={() => onClickCreateUserGroupButtonHandler()}
  53. >{t('admin:user_group_management.create_group')}
  54. </button>
  55. </div>
  56. </div>
  57. </>
  58. );
  59. };