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

Possible workaround react-hook-form v5 limitation #845

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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: 29 additions & 3 deletions src/pages/PrepareCondition/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ export const PrepareCondition = () => {
mode: 'onChange',
defaultValues: defaultValuesOmen,
})
const { dirty: isDirtyOmenCondition, isValid: isValidOmenCondition } = formStateOmenCondition
const {
dirty: isDirtyOmenCondition,
dirtyFields: dirtyFieldsOmenCondition,
isValid: isValidOmenCondition,
} = formStateOmenCondition

const category = watchOmenCondition('category')
const questionTitle = watchOmenCondition('questionTitle')
Expand All @@ -163,6 +167,7 @@ export const PrepareCondition = () => {
const [outcomes, setOutcomes] = useState<Array<string>>([])
const [outcome, setOutcome] = useState<string>('')
const [outcomesBeingEdited, setOutcomesBeingEdited] = useState<boolean[]>([])
const [categoryManuallyChanged, setCategoryChangedManually] = useState(false)

const addOutcome = useCallback(() => {
const sanitizedOutcome = outcome.trim()
Expand Down Expand Up @@ -564,6 +569,21 @@ export const PrepareCondition = () => {
[setErrorOmenCondition]
)

const isActuallyDirtyOmenCondition = React.useMemo(() => {
// If category actually changed by user interaction
if (categoryManuallyChanged) return isDirtyOmenCondition

// If is dirty, is possible that something else besides the initial dirty field also changed
if (
isDirtyOmenCondition &&
dirtyFieldsOmenCondition.size === 1 &&
dirtyFieldsOmenCondition.has('category')
)
return false

return isDirtyOmenCondition
}, [categoryManuallyChanged, dirtyFieldsOmenCondition, isDirtyOmenCondition])

const customConditionFormHasErrors = Object.keys(errorsCustomCondition).length > 0
const omenConditionFormHasErrors =
isQuestionAlreadyExist ||
Expand Down Expand Up @@ -831,7 +851,12 @@ export const PrepareCondition = () => {
as={CategoriesDropdown}
control={omenControl}
name="category"
onClick={(value: string) => setValueOmenCondition('category', value, true)}
onClick={(value: string) => {
if (category && !categoryManuallyChanged) {
setCategoryChangedManually(true)
}
setValueOmenCondition('category', value, true)
}}
rules={{ required: true }}
value={category}
/>
Expand Down Expand Up @@ -938,7 +963,8 @@ export const PrepareCondition = () => {
when={
conditionType === ConditionType.custom
? isDirtyCustomCondition && prepareConditionStatus.isNotAsked()
: (isDirtyOmenCondition || outcomes.length > 0) && prepareConditionStatus.isNotAsked()
: (isActuallyDirtyOmenCondition || outcomes.length > 0) &&
prepareConditionStatus.isNotAsked()
}
/>
</CenteredCard>
Expand Down