Skip to content

Commit

Permalink
show disabled checkbox tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
tongo-angelov committed Sep 20, 2023
1 parent f4dc18c commit db63725
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
3 changes: 2 additions & 1 deletion public/locales/bg/person.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"organizerRelation": "Отношения с организатор",
"countryCode": "Държава",
"city": "Град",
"description": "Описание"
"description": "Описание",
"disabled-tooltip": "Ролята не може да бъде премахната, потребителя е {{role}} в {{campaigns}} кампании"
},
"cta": {
"create": "Създай",
Expand Down
3 changes: 2 additions & 1 deletion public/locales/en/person.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"organizerRelation": "Organizer relation",
"countryCode": "Country code",
"city": "City",
"description": "Description"
"description": "Description",
"disabled-tooltip": "Cannot remove role because the user is {{role}} in {{campaigns}} campaigns"
},
"cta": {
"create": "Create",
Expand Down
36 changes: 20 additions & 16 deletions src/components/common/form/CheckboxField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChangeEvent } from 'react'

import { useField } from 'formik'
import { useTranslation } from 'next-i18next'
import { Checkbox, FormControl, FormControlLabel, FormHelperText } from '@mui/material'
import { Checkbox, FormControl, FormControlLabel, FormHelperText, Tooltip } from '@mui/material'

import { TranslatableField, translateError } from 'common/form/validation'

Expand All @@ -11,34 +11,38 @@ export type CheckboxFieldProps = {
disabled?: boolean
onChange?: (e: ChangeEvent<HTMLInputElement>) => void
label: string | number | React.ReactElement
disabledTooltip?: string
}

export default function CheckboxField({
name,
disabled,
onChange: handleChange,
label,
disabledTooltip,
}: CheckboxFieldProps) {
const { t } = useTranslation()
const [field, meta] = useField(name)
const helperText = meta.touched ? translateError(meta.error as TranslatableField, t) : ''
return (
<FormControl required component="fieldset" error={Boolean(meta.error) && Boolean(meta.touched)}>
<FormControlLabel
label={typeof label === 'string' ? `${t(label)}` : label}
control={
<Checkbox
color="primary"
checked={Boolean(field.value)}
disabled={disabled}
{...field}
onChange={(e) => {
field.onChange(e)
if (handleChange) handleChange(e)
}}
/>
}
/>
<Tooltip title={disabled && disabledTooltip} arrow>
<FormControlLabel
label={typeof label === 'string' ? `${t(label)}` : label}
control={
<Checkbox
color="primary"
checked={Boolean(field.value)}
disabled={disabled}
{...field}
onChange={(e) => {
field.onChange(e)
if (handleChange) handleChange(e)
}}
/>
}
/>
</Tooltip>
{Boolean(meta.error) && <FormHelperText error>{helperText}</FormHelperText>}
</FormControl>
)
Expand Down
24 changes: 18 additions & 6 deletions src/components/common/person/grid/EditForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ export default function EditForm() {
phone: data?.phone ?? '',
isCoordinator: !!data?.coordinators,
coordinatorId: data?.coordinators?.id,
activeCoordinator: !!data?.coordinators?._count?.campaigns,
coordinatorCampaigns: data?.coordinators?._count?.campaigns,
isOrganizer: !!data?.organizer,
organizerId: data?.organizer?.id,
activeOrganizer: !!data?.organizer?._count?.campaigns,
organizerCampaigns: data?.organizer?._count?.campaigns,
isBeneficiary: !!data?.beneficiaries?.length,
beneficiaryId: beneficiary?.id,
activeBeneficiary: !!beneficiary?._count?.campaigns,
beneficiaryCampaigns: beneficiary?._count?.campaigns,
countryCode: beneficiary?.countryCode ?? 'BG',
cityId: beneficiary?.cityId ?? '',
description: beneficiary?.description ?? '',
Expand Down Expand Up @@ -219,21 +219,33 @@ export default function EditForm() {
<Grid item xs={4}>
<CheckboxField
name="isOrganizer"
disabled={initialValues.activeOrganizer}
disabled={!!initialValues.organizerCampaigns}
disabledTooltip={t('person:admin.fields.disabled-tooltip', {
role: t('person:admin.fields.organizer'),
campaigns: initialValues.organizerCampaigns,
})}
label="person:admin.fields.organizer"
/>
</Grid>
<Grid item xs={4}>
<CheckboxField
name="isCoordinator"
disabled={initialValues.activeCoordinator}
disabled={!!initialValues.coordinatorCampaigns}
disabledTooltip={t('person:admin.fields.disabled-tooltip', {
role: t('person:admin.fields.coordinator'),
campaigns: initialValues.coordinatorCampaigns,
})}
label="person:admin.fields.coordinator"
/>
</Grid>
<Grid item xs={4}>
<CheckboxField
name="isBeneficiary"
disabled={initialValues.activeBeneficiary}
disabled={!!initialValues.beneficiaryCampaigns}
disabledTooltip={t('person:admin.fields.disabled-tooltip', {
role: t('person:admin.fields.beneficiary'),
campaigns: initialValues.beneficiaryCampaigns,
})}
label="person:admin.fields.beneficiary"
onChange={(e) => {
setShowBenefactor(e.target.checked)
Expand Down

0 comments on commit db63725

Please sign in to comment.