Skip to content

Commit

Permalink
Merge pull request openmrs#76 from METS-Programme/revert-58-fix/actio…
Browse files Browse the repository at this point in the history
…ns-pre-release

Revert "fix radio button bug"
  • Loading branch information
makombe authored Jan 18, 2024
2 parents 2e2fe02 + 16ba4a1 commit b771f97
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import React from "react";
import { RadioButtonGroupProps } from "@carbon/react/lib/components/RadioButtonGroup/RadioButtonGroup";
import { Control, Controller, FieldValues } from "react-hook-form";
import { RadioButtonGroup, RadioButton } from "@carbon/react";
import {
RadioOption,
radioOptions,
} from "../../../../stock-items/add-stock-item/stock-item-details/stock-item-details.resource";
import { RadioButtonGroup } from "@carbon/react";

interface ControlledRadioButtonGroupProps<T> extends RadioButtonGroupProps {
controllerName: string;
name: string;
control: Control<FieldValues, T>;
defaultValue?: string;
radioOptions: RadioOption[];
}

const ControlledRadioButtonGroup = <T,>(
Expand All @@ -22,22 +16,21 @@ const ControlledRadioButtonGroup = <T,>(
<Controller
name={props.controllerName}
control={props.control}
defaultValue={props.defaultValue}
render={({ field: { onChange, value, ref } }) => (
<RadioButtonGroup id={props.name}>
{radioOptions.map((option: RadioOption, index) => (
<RadioButton
{...props}
key={`${index}-${option.value}`}
id={`${name}-${option.value}`}
labelText={option.label}
value={option.value}
checked={value === option.value}
onChange={() => onChange(option.value)}
ref={ref}
/>
))}
</RadioButtonGroup>
<RadioButtonGroup
{...props}
onChange={(selection: unknown, name: string, event: unknown) => {
onChange(selection, name, event);

// Fire prop change
if (props.onChange) {
props.onChange(selection, name, event);
}
}}
id={props.name}
ref={ref}
value={value}
/>
)}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { SaveStockItem } from "../../types";
import ConceptsSelector from "../concepts-selector/concepts-selector.component";
import rootStyles from "../../../root.scss";
import { closeOverlay } from "../../../core/components/overlay/hook";
import { expirationOptions, radioOptions } from "./stock-item-details.resource";

interface StockItemDetailsProps {
model: StockItemDTO;
Expand Down Expand Up @@ -57,12 +56,12 @@ const StockItemDetails = forwardRef<never, StockItemDetailsProps>(
};

const [isSaving, setIsSaving] = useState(false);
const [isDrug, setIsDrug] = useState(false);
const [isDrug, setIsDrug] = useState<boolean | null>();
const [hasExpiration, setHasExpiration] = useState<boolean | null>();

useEffect(() => {
setIsDrug(model.isDrug ?? false);
setHasExpiration(model.hasExpiration ?? false);
setIsDrug(model.isDrug);
setHasExpiration(model.hasExpiration);
}, [model.hasExpiration, model.isDrug]);

return (
Expand All @@ -84,8 +83,10 @@ const StockItemDetails = forwardRef<never, StockItemDetailsProps>(
setIsDrug(selection);
}}
name="isDrug"
radioOptions={radioOptions}
/>
>
<RadioButton value={true} id="isDrug-true" labelText="Drug" />
<RadioButton value={false} id="isDrug-false" labelText="Other" />
</ControlledRadioButtonGroup>
</FormGroup>
{isDrug && (
<DrugSelector
Expand Down Expand Up @@ -155,8 +156,18 @@ const StockItemDetails = forwardRef<never, StockItemDetailsProps>(
onChange={(selection: boolean) => {
setHasExpiration(selection);
}}
radioOptions={expirationOptions}
/>
>
<RadioButton
value={true}
id="hasExpiration-true"
labelText={t("yes", "Yes")}
/>
<RadioButton
value={false}
id="hasExpiration-false"
labelText={t("no", "No")}
/>
</ControlledRadioButtonGroup>
</FormGroup>

{hasExpiration && (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +0,0 @@
export interface RadioOption {
label: string;
value: boolean | string;
}

export const radioOptions: RadioOption[] = [
{ label: "Drug", value: true },
{ label: "Other", value: false },
];

export const expirationOptions: RadioOption[] = [
{ label: "Yes", value: "yes" },
{ label: "No", value: "no" },
];
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const AddStockOperation: React.FC<AddStockOperationProps> = (props) => {
);
const [selectedIndex, setSelectedIndex] = useState(0);

const [requiresDispatchAcknowledgement, setRequiresDispatchAcknowledgement] =
useState(false);

if (isLoading) return <AccordionSkeleton />;
if (isError) {
closeOverlay();
Expand Down Expand Up @@ -193,18 +196,12 @@ const AddStockOperation: React.FC<AddStockOperationProps> = (props) => {
{!props.model?.permission?.canEdit &&
props.model?.permission?.canApprove && (
<>
{!(props.model
? props.model?.operationTypeName === "return" ||
props.model?.operationTypeName === "stockissue"
: false) && (
{!requiresDispatchAcknowledgement && (
<div style={{ margin: "2px" }}>
<StockOperationApprovalButton operation={props.model} />
</div>
)}
{(props.model
? props.model?.operationTypeName === "return" ||
props.model?.operationTypeName === "stockissue"
: false) && (
{requiresDispatchAcknowledgement && (
<div style={{ margin: "2px" }}>
<StockOperationApproveDispatchButton
operation={props.model}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const StockOperationApproveDispatchButton: React.FC<
const { t } = useTranslation();
const launchApproveDispatchModal = useCallback(() => {
const dispose = showModal("stock-operation-dialog", {
title: "Dispatch",
title: "Approve Dispatch",
operation: operation,
requireReason: true,
closeModal: () => dispose(),
Expand All @@ -28,7 +28,7 @@ const StockOperationApproveDispatchButton: React.FC<
onClick={launchApproveDispatchModal}
renderIcon={(props) => <Departure size={16} {...props} />}
>
{t("approve", "Dispatch")}
{t("approve", "Approve Dispatch ")}
</Button>
);
};
Expand Down

0 comments on commit b771f97

Please sign in to comment.