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

add disable option for select and use it in RoomBedDropDown #584

Merged
merged 4 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions lib/components/user_input/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { ChevronDown, ChevronUp } from 'lucide-react'

type Option<T> = {
label: string,
value: T
value: T,
disabled?: boolean
}

type SelectProps<T> = {
Expand All @@ -14,6 +15,7 @@ type SelectProps<T> = {
onChange: (value: T) => void,
isHidingCurrentValue?: boolean,
hintText?: string,
showDisabledOptions?: boolean,
className?: string
};

Expand All @@ -22,9 +24,21 @@ type SelectProps<T> = {
*
* The State is managed by the parent
*/
export const Select = <T, >({ value, label, options, onChange, isHidingCurrentValue = true, hintText = '', className }: SelectProps<T>) => {
export const Select = <T, >({
value,
label,
options,
onChange,
isHidingCurrentValue = true,
hintText = '',
showDisabledOptions = true,
className
}: SelectProps<T>) => {
// Notice: for more complex types this check here might need an additional compare method
const filteredOptions = isHidingCurrentValue ? options.filter(option => option.value !== value) : options
let filteredOptions = isHidingCurrentValue ? options.filter(option => option.value !== value) : options
if (!showDisabledOptions) {
filteredOptions = filteredOptions.filter(value => !value.disabled)
}
return (
<div className={tx(className)}>
{label && (
Expand All @@ -47,12 +61,18 @@ export const Select = <T, >({ value, label, options, onChange, isHidingCurrentVa
<Menu.Item key={option.label}>
{
<div
className={tx('px-4 py-2 cursor-pointer overflow-hidden whitespace-nowrap text-ellipsis hover:bg-gray-100 border-2 border-t-0', {
'bg-gray-100 ': option.value === value,
className={tx('px-4 py-2 overflow-hidden whitespace-nowrap text-ellipsis border-2 border-t-0', {
'bg-gray-100': option.value === value,
'bg-gray-50': index % 2 === 1,
'text-gray-300 cursor-not-allowed': !!option.disabled,
DasProffi marked this conversation as resolved.
Show resolved Hide resolved
'hover:bg-gray-100 cursor-pointer': !option.disabled,
'border-b-0 rounded-b-lg': index === filteredOptions.length - 1,
})}
onClick={() => onChange(option.value)}
onClick={() => {
if (!option.disabled) {
onChange(option.value)
}
}}
>
{option.label}
</div>
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tasks/components/RoomBedDropDown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const RoomBedDropDown = ({
<Select
className={tw('min-w-[120px]')}
value={currentSelection.roomID}
options={data.map(room => ({ value: room.id, label: room.name }))}
options={data.map(room => ({ value: room.id, label: room.name, disabled: room.beds.length === 0 }))}
onChange={value => {
setCurrentSelection({
...currentSelection,
Expand All @@ -99,7 +99,7 @@ export const RoomBedDropDown = ({
<Select
className={tw('min-w-[150px]')}
value={currentSelection.bedID}
options={currentRoom.beds.map(value => ({ value: value.id, label: value.name }))}
options={currentRoom.beds.map(value => ({ value: value.id, label: value.name, disabled: !!value.patient }))}
onChange={value => {
setCurrentSelection({
...currentSelection,
Expand Down
2 changes: 1 addition & 1 deletion tasks/mutations/patient_mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export const useAssignBedMutation = (callback: (bed: BedWithPatientID) => void =
return bed
},
onSuccess: () => {
queryClient.refetchQueries([roomsQueryKey, roomOverviewsQueryKey]).then()
queryClient.refetchQueries([roomsQueryKey]).then()
queryClient.refetchQueries([patientsQueryKey]).then()
}
})
Expand Down