Skip to content

Commit

Permalink
Simplify individual components by moving hook to get required and dis…
Browse files Browse the repository at this point in the history
…playFlyover into ItemFieldWrapper
  • Loading branch information
fongsean committed Feb 20, 2024
1 parent 4ab1621 commit 7c1e04a
Show file tree
Hide file tree
Showing 26 changed files with 47 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import type {
PropsWithQrItemChangeHandler
} from '../../../interfaces/renderProps.interface';
import type { QuestionnaireItem, QuestionnaireResponseItem } from 'fhir/r4';
import useRenderingExtensions from '../../../hooks/useRenderingExtensions';
import { createEmptyQrItem } from '../../../utils/qrItem';
import { FullWidthFormComponentBox } from '../../Box.styles';
import ItemFieldGrid from '../ItemParts/ItemFieldGrid';
Expand All @@ -44,7 +43,6 @@ function BooleanItem(props: BooleanItemProps) {
const { qItem, qrItem, isRepeated, isTabled, parentIsReadOnly, onQrItemChange } = props;

const readOnly = useReadOnly(qItem, parentIsReadOnly);
const { required } = useRenderingExtensions(qItem);

// Init input value
let checked = false;
Expand Down Expand Up @@ -75,7 +73,7 @@ function BooleanItem(props: BooleanItemProps) {
}
return (
<FullWidthFormComponentBox data-test="q-item-boolean-box">
<ItemFieldGrid qItem={qItem} required={required} readOnly={readOnly}>
<ItemFieldGrid qItem={qItem} readOnly={readOnly}>
<BooleanField checked={checked} readOnly={readOnly} onCheckedChange={handleCheckedChange} />
</ItemFieldGrid>
</FullWidthFormComponentBox>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { createEmptyQrItem } from '../../../utils/qrItem';
import { FullWidthFormComponentBox } from '../../Box.styles';
import useDebounce from '../../../hooks/useDebounce';
import useTerminologyServerQuery from '../../../hooks/useTerminologyServerQuery';
import useRenderingExtensions from '../../../hooks/useRenderingExtensions';
import type {
PropsWithIsRepeatedAttribute,
PropsWithIsTabledAttribute,
Expand Down Expand Up @@ -54,7 +53,6 @@ function ChoiceAutocompleteItem(props: ChoiceAutocompleteItemProps) {
}

const readOnly = useReadOnly(qItem, parentIsReadOnly);
const { required } = useRenderingExtensions(qItem);

const maxList = 10;

Expand Down Expand Up @@ -104,7 +102,7 @@ function ChoiceAutocompleteItem(props: ChoiceAutocompleteItemProps) {

return (
<FullWidthFormComponentBox>
<ItemFieldGrid qItem={qItem} required={required} readOnly={readOnly}>
<ItemFieldGrid qItem={qItem} readOnly={readOnly}>
<ChoiceAutocompleteField
qItem={qItem}
options={options}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function ChoiceCheckboxAnswerOptionItem(props: ChoiceCheckboxAnswerOptionItemPro
const answers = qrChoiceCheckbox.answer ? qrChoiceCheckbox.answer : [];

const readOnly = useReadOnly(qItem, parentIsReadOnly);
const { displayInstructions, required } = useRenderingExtensions(qItem);
const { displayInstructions } = useRenderingExtensions(qItem);

// Event handlers
function handleCheckedChange(changedValue: string) {
Expand Down Expand Up @@ -96,7 +96,7 @@ function ChoiceCheckboxAnswerOptionItem(props: ChoiceCheckboxAnswerOptionItemPro

return (
<FullWidthFormComponentBox data-test="q-item-choice-checkbox-answer-option-box">
<ItemFieldGrid qItem={qItem} required={required} readOnly={readOnly}>
<ItemFieldGrid qItem={qItem} readOnly={readOnly}>
<ChoiceCheckboxAnswerValueSetFields
qItem={qItem}
answers={answers}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function ChoiceCheckboxAnswerValueSetItem(props: ChoiceCheckboxAnswerValueSetIte
const answers = qrChoiceCheckbox.answer ? qrChoiceCheckbox.answer : [];

const readOnly = useReadOnly(qItem, parentIsReadOnly);
const { displayInstructions, required } = useRenderingExtensions(qItem);
const { displayInstructions } = useRenderingExtensions(qItem);

// Get codings/options from valueSet
const { codings, serverError } = useValueSetCodings(qItem);
Expand Down Expand Up @@ -101,7 +101,7 @@ function ChoiceCheckboxAnswerValueSetItem(props: ChoiceCheckboxAnswerValueSetIte

return (
<FullWidthFormComponentBox data-test="q-item-choice-checkbox-answer-value-set-box">
<ItemFieldGrid qItem={qItem} required={required} readOnly={readOnly}>
<ItemFieldGrid qItem={qItem} readOnly={readOnly}>
<ChoiceCheckboxAnswerValueSetFields
codings={codings}
answers={answers}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import type { QuestionnaireItem, QuestionnaireResponseItem } from 'fhir/r4';
import { findInAnswerOptions, getQrChoiceValue } from '../../../utils/choice';
import { createEmptyQrItem } from '../../../utils/qrItem';
import { FullWidthFormComponentBox } from '../../Box.styles';
import useRenderingExtensions from '../../../hooks/useRenderingExtensions';
import type {
PropsWithIsRepeatedAttribute,
PropsWithParentIsReadOnlyAttribute,
Expand All @@ -48,7 +47,6 @@ function ChoiceRadioAnswerOptionItem(props: ChoiceRadioAnswerOptionItemProps) {
const valueRadio = getQrChoiceValue(qrChoiceRadio);

const readOnly = useReadOnly(qItem, parentIsReadOnly);
const { required } = useRenderingExtensions(qItem);

// Event handlers
function handleChange(newValue: string) {
Expand All @@ -74,7 +72,7 @@ function ChoiceRadioAnswerOptionItem(props: ChoiceRadioAnswerOptionItemProps) {

return (
<FullWidthFormComponentBox data-test="q-item-choice-radio-answer-option-box">
<ItemFieldGrid qItem={qItem} required={required} readOnly={readOnly}>
<ItemFieldGrid qItem={qItem} readOnly={readOnly}>
<ChoiceRadioAnswerOptionFields
qItem={qItem}
valueRadio={valueRadio}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { findInAnswerValueSetCodings } from '../../../utils/choice';
import { createEmptyQrItem } from '../../../utils/qrItem';
import { FullWidthFormComponentBox } from '../../Box.styles';
import useValueSetCodings from '../../../hooks/useValueSetCodings';
import useRenderingExtensions from '../../../hooks/useRenderingExtensions';
import type {
PropsWithIsRepeatedAttribute,
PropsWithParentIsReadOnlyAttribute,
Expand All @@ -45,7 +44,6 @@ function ChoiceRadioAnswerValueSetItem(props: ChoiceRadioAnswerValueSetItemProps
const { qItem, qrItem, orientation, isRepeated, parentIsReadOnly, onQrItemChange } = props;

const readOnly = useReadOnly(qItem, parentIsReadOnly);
const { required } = useRenderingExtensions(qItem);

// Init input value
const qrChoiceRadio = qrItem ?? createEmptyQrItem(qItem);
Expand Down Expand Up @@ -86,7 +84,7 @@ function ChoiceRadioAnswerValueSetItem(props: ChoiceRadioAnswerValueSetItemProps

return (
<FullWidthFormComponentBox data-test="q-item-choice-radio-answer-value-set-box">
<ItemFieldGrid qItem={qItem} required={required} readOnly={readOnly}>
<ItemFieldGrid qItem={qItem} readOnly={readOnly}>
<ChoiceRadioAnswerValueSetFields
qItem={qItem}
codings={codings}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import type { QuestionnaireItem, QuestionnaireResponseItem } from 'fhir/r4';
import { findInAnswerOptions, getQrChoiceValue } from '../../../utils/choice';
import { createEmptyQrItem } from '../../../utils/qrItem';
import { FullWidthFormComponentBox } from '../../Box.styles';
import useRenderingExtensions from '../../../hooks/useRenderingExtensions';
import type {
PropsWithIsRepeatedAttribute,
PropsWithIsTabledAttribute,
Expand All @@ -45,7 +44,6 @@ function ChoiceSelectAnswerOptionItem(props: ChoiceSelectAnswerOptionItemProps)
const { qItem, qrItem, isRepeated, isTabled, parentIsReadOnly, onQrItemChange } = props;

const readOnly = useReadOnly(qItem, parentIsReadOnly);
const { required } = useRenderingExtensions(qItem);

// Init input value
const qrChoiceSelect = qrItem ?? createEmptyQrItem(qItem);
Expand Down Expand Up @@ -80,7 +78,7 @@ function ChoiceSelectAnswerOptionItem(props: ChoiceSelectAnswerOptionItemProps)

return (
<FullWidthFormComponentBox data-test="q-item-choice-select-answer-option-box">
<ItemFieldGrid qItem={qItem} required={required} readOnly={readOnly}>
<ItemFieldGrid qItem={qItem} readOnly={readOnly}>
<ChoiceSelectAnswerOptionFields
qItem={qItem}
valueSelect={valueSelect}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import type { Coding, QuestionnaireItem, QuestionnaireResponseItem } from 'fhir/
import { createEmptyQrItem } from '../../../utils/qrItem';
import { FullWidthFormComponentBox } from '../../Box.styles';
import useValueSetCodings from '../../../hooks/useValueSetCodings';
import useRenderingExtensions from '../../../hooks/useRenderingExtensions';
import type {
PropsWithIsRepeatedAttribute,
PropsWithIsTabledAttribute,
Expand All @@ -45,7 +44,6 @@ function ChoiceSelectAnswerValueSetItem(props: ChoiceSelectAnswerValueSetItemPro
const { qItem, qrItem, isRepeated, isTabled, parentIsReadOnly, onQrItemChange } = props;

const readOnly = useReadOnly(qItem, parentIsReadOnly);
const { required } = useRenderingExtensions(qItem);

// Init input value
const qrChoiceSelect = qrItem ?? createEmptyQrItem(qItem);
Expand Down Expand Up @@ -106,7 +104,7 @@ function ChoiceSelectAnswerValueSetItem(props: ChoiceSelectAnswerValueSetItemPro

return (
<FullWidthFormComponentBox data-test="q-item-choice-dropdown-answer-value-set-box">
<ItemFieldGrid qItem={qItem} required={required} readOnly={readOnly}>
<ItemFieldGrid qItem={qItem} readOnly={readOnly}>
<ChoiceSelectAnswerValueSetFields
qItem={qItem}
codings={codings}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function CustomDateItem(props: CustomDateItemProps) {
const { qItem, qrItem, isRepeated, isTabled, parentIsReadOnly, onQrItemChange } = props;

const readOnly = useReadOnly(qItem, parentIsReadOnly);
const { displayPrompt, entryFormat, required } = useRenderingExtensions(qItem);
const { displayPrompt, entryFormat } = useRenderingExtensions(qItem);

const qrDate = qrItem ?? createEmptyQrItem(qItem);

Expand Down Expand Up @@ -117,7 +117,7 @@ function CustomDateItem(props: CustomDateItemProps) {

return (
<FullWidthFormComponentBox data-test="q-item-date-box">
<ItemFieldGrid qItem={qItem} required={required} readOnly={readOnly}>
<ItemFieldGrid qItem={qItem} readOnly={readOnly}>
<CustomDateField
linkId={qItem.linkId}
valueDate={displayDate}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function DateItem(props: DateItemProps) {
const { qItem, qrItem, isRepeated, isTabled, parentIsReadOnly, onQrItemChange } = props;

const readOnly = useReadOnly(qItem, parentIsReadOnly);
const { displayPrompt, entryFormat, required } = useRenderingExtensions(qItem);
const { displayPrompt, entryFormat } = useRenderingExtensions(qItem);

// Init input value
let dateString: string | null = null;
Expand Down Expand Up @@ -85,7 +85,7 @@ function DateItem(props: DateItemProps) {

return (
<FullWidthFormComponentBox data-test="q-item-date-box">
<ItemFieldGrid qItem={qItem} required={required} readOnly={readOnly}>
<ItemFieldGrid qItem={qItem} readOnly={readOnly}>
<DateField
value={dateDayJs}
displayPrompt={displayPrompt}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function DateTimeItem(props: DateTimeItemProps) {
const { qItem, qrItem, isRepeated, isTabled, parentIsReadOnly, onQrItemChange } = props;

const readOnly = useReadOnly(qItem, parentIsReadOnly);
const { displayPrompt, entryFormat, required } = useRenderingExtensions(qItem);
const { displayPrompt, entryFormat } = useRenderingExtensions(qItem);

// Init input value
let dateTimeString: string | null = null;
Expand Down Expand Up @@ -83,7 +83,7 @@ function DateTimeItem(props: DateTimeItemProps) {

return (
<FullWidthFormComponentBox data-test="q-item-date-time-box">
<ItemFieldGrid qItem={qItem} required={required} readOnly={readOnly}>
<ItemFieldGrid qItem={qItem} readOnly={readOnly}>
<DateTimeField
value={dateTimeDayJs}
displayPrompt={displayPrompt}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,8 @@ function DecimalItem(props: DecimalItemProps) {

const readOnly = useReadOnly(qItem, parentIsReadOnly);
const precision = getDecimalPrecision(qItem);
const {
displayUnit,
displayPrompt,
entryFormat,
required,
regexValidation,
minLength,
maxLength
} = useRenderingExtensions(qItem);
const { displayUnit, displayPrompt, entryFormat, regexValidation, minLength, maxLength } =
useRenderingExtensions(qItem);

// Init input value
let valueDecimal = 0.0;
Expand Down Expand Up @@ -134,7 +127,7 @@ function DecimalItem(props: DecimalItemProps) {

return (
<FullWidthFormComponentBox data-test="q-item-decimal-box">
<ItemFieldGrid qItem={qItem} required={required} readOnly={readOnly}>
<ItemFieldGrid qItem={qItem} readOnly={readOnly}>
<DecimalField
linkId={qItem.linkId}
input={input}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,8 @@ function IntegerItem(props: IntegerItemProps) {
const { qItem, qrItem, isRepeated, isTabled, parentIsReadOnly, onQrItemChange } = props;

const readOnly = useReadOnly(qItem, parentIsReadOnly);
const {
displayUnit,
displayPrompt,
entryFormat,
required,
regexValidation,
minLength,
maxLength
} = useRenderingExtensions(qItem);
const { displayUnit, displayPrompt, entryFormat, regexValidation, minLength, maxLength } =
useRenderingExtensions(qItem);

// Init input value
let valueInteger = 0;
Expand Down Expand Up @@ -122,7 +115,7 @@ function IntegerItem(props: IntegerItemProps) {

return (
<FullWidthFormComponentBox data-test="q-item-integer-box">
<ItemFieldGrid qItem={qItem} required={required} readOnly={readOnly}>
<ItemFieldGrid qItem={qItem} readOnly={readOnly}>
<IntegerField
linkId={qItem.linkId}
value={value}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,18 @@ import useRenderingExtensions from '../../../hooks/useRenderingExtensions';
interface ItemFieldGridProps {
children: ReactNode;
qItem: QuestionnaireItem;
required: boolean;
readOnly: boolean;
}

function ItemFieldGrid(props: ItemFieldGridProps) {
const { children, qItem, required, readOnly } = props;
const { children, qItem, readOnly } = props;

const { displayInstructions, displayFlyover } = useRenderingExtensions(qItem);
const { displayInstructions } = useRenderingExtensions(qItem);

return (
<Grid container columnSpacing={6}>
<Grid item xs={5}>
<LabelWrapper
qItem={qItem}
displayFlyover={displayFlyover}
required={required}
readOnly={readOnly}
/>
<LabelWrapper qItem={qItem} readOnly={readOnly} />
</Grid>
<Grid item xs={7}>
{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ import { getContextDisplays } from '../../../utils/tabs';
import ItemLabelText from './ItemLabelText';
import Tooltip from '@mui/material/Tooltip';
import Typography from '@mui/material/Typography';
import useRenderingExtensions from '../../../hooks/useRenderingExtensions';

interface LabelWrapperProps {
qItem: QuestionnaireItem;
required: boolean;
displayFlyover: string;
readOnly: boolean;
}

function ItemLabelWrapper(props: LabelWrapperProps) {
const { qItem, required, displayFlyover, readOnly } = props;
const { qItem, readOnly } = props;

const { required, displayFlyover } = useRenderingExtensions(qItem);
const contextDisplayItems = getContextDisplays(qItem);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { createEmptyQrItem } from '../../../utils/qrItem';
import { FullWidthFormComponentBox } from '../../Box.styles';
import useDebounce from '../../../hooks/useDebounce';
import useTerminologyServerQuery from '../../../hooks/useTerminologyServerQuery';
import useRenderingExtensions from '../../../hooks/useRenderingExtensions';
import type {
PropsWithIsRepeatedAttribute,
PropsWithIsTabledAttribute,
Expand All @@ -48,7 +47,6 @@ function OpenChoiceAutocompleteItem(props: OpenChoiceAutocompleteItemProps) {
const { qItem, qrItem, isRepeated, isTabled, parentIsReadOnly, onQrItemChange } = props;

const readOnly = useReadOnly(qItem, parentIsReadOnly);
const { required } = useRenderingExtensions(qItem);

const qrOpenChoice = qrItem ?? createEmptyQrItem(qItem);

Expand Down Expand Up @@ -133,7 +131,7 @@ function OpenChoiceAutocompleteItem(props: OpenChoiceAutocompleteItemProps) {

return (
<FullWidthFormComponentBox>
<ItemFieldGrid qItem={qItem} required={required} readOnly={readOnly}>
<ItemFieldGrid qItem={qItem} readOnly={readOnly}>
<OpenChoiceAutocompleteField
qItem={qItem}
options={options}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function OpenChoiceCheckboxAnswerOptionItem(props: OpenChoiceCheckboxAnswerOptio

const readOnly = useReadOnly(qItem, parentIsReadOnly);
const openLabelText = getOpenLabelText(qItem);
const { displayInstructions, required } = useRenderingExtensions(qItem);
const { displayInstructions } = useRenderingExtensions(qItem);

// Init answers
const qrOpenChoiceCheckbox = qrItem ?? createEmptyQrItem(qItem);
Expand Down Expand Up @@ -158,7 +158,7 @@ function OpenChoiceCheckboxAnswerOptionItem(props: OpenChoiceCheckboxAnswerOptio

return (
<FullWidthFormComponentBox data-test="q-item-open-choice-checkbox-answer-option-box">
<ItemFieldGrid qItem={qItem} required={required} readOnly={readOnly}>
<ItemFieldGrid qItem={qItem} readOnly={readOnly}>
<OpenChoiceCheckboxAnswerOptionFields
qItem={qItem}
answers={answers}
Expand Down
Loading

0 comments on commit 7c1e04a

Please sign in to comment.