fix(dashboard): rules form operator change (#13324)

**What**
- fix condition rules form sometimes rendering multi and single selects unrelated to the attribute operator selected
- reset rule value when operator is changed
- disable selecting rule values untill operator is selected
- translate labels
This commit is contained in:
Frane Polić authored and GitHub committed 2025-08-29 15:53:00 +00:00
1 parent c603540562
commit f53f027ce6
4 files changed
+43 -4

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/dashboard": patch
---
fix(dashboard): rules form operator change
@@ -10818,6 +10818,12 @@
}, },
"loading": { "loading": {
"type": "string" "type": "string"
},
"selectValue": {
"type": "string"
},
"selectValues": {
"type": "string"
} }
}, },
"required": [ "required": [
@@ -10830,7 +10836,9 @@
"from", "from",
"to", "to",
"beaware", "beaware",
"loading" "loading",
"selectValue",
"selectValues"
], ],
"additionalProperties": false "additionalProperties": false
}, },
@@ -2913,7 +2913,9 @@
"from": "From", "from": "From",
"to": "To", "to": "To",
"beaware": "Be aware", "beaware": "Be aware",
"loading": "Loading" "loading": "Loading",
"selectValue": "Select Value",
"selectValues": "Select Values"
}, },
"fields": { "fields": {
"amount": "Amount", "amount": "Amount",
@@ -5,6 +5,9 @@ import {
} from "@medusajs/types" } from "@medusajs/types"
import { Input } from "@medusajs/ui" import { Input } from "@medusajs/ui"
import { useWatch } from "react-hook-form" import { useWatch } from "react-hook-form"
import { useTranslation } from "react-i18next"
import { useEffect } from "react"
import { Form } from "../../../../../../components/common/form" import { Form } from "../../../../../../components/common/form"
import { Combobox } from "../../../../../../components/inputs/combobox" import { Combobox } from "../../../../../../components/inputs/combobox"
import { useStore } from "../../../../../../hooks/api" import { useStore } from "../../../../../../hooks/api"
@@ -51,6 +54,8 @@ export const RuleValueFormField = ({
ruleType, ruleType,
applicationMethodTargetType, applicationMethodTargetType,
}: RuleValueFormFieldType) => { }: RuleValueFormFieldType) => {
const { t } = useTranslation()
const attribute = attributes?.find( const attribute = attributes?.find(
(attr) => attr.value === fieldRule.attribute (attr) => attr.value === fieldRule.attribute
) )
@@ -82,6 +87,23 @@ export const RuleValueFormField = ({
name: operator, name: operator,
}) })
useEffect(() => {
const hasDirtyRules = Object.keys(form.formState.dirtyFields).length > 0
/**
* Don't reset values if fileds didn't change - this is to prevent reset of form on initial render when editing an existing rule
*/
if (!hasDirtyRules) {
return
}
if (watchOperator === "eq") {
form.setValue(name, "")
} else {
form.setValue(name, [])
}
}, [watchOperator])
return ( return (
<Form.Field <Form.Field
key={`${identifier}.${scope}.${name}-${fieldRule.attribute}`} key={`${identifier}.${scope}.${name}-${fieldRule.attribute}`}
@@ -126,11 +148,13 @@ export const RuleValueFormField = ({
<Combobox <Combobox
{...field} {...field}
{...comboboxData} {...comboboxData}
multiple={watchOperator !== "eq"}
ref={ref} ref={ref}
placeholder={ placeholder={
watchOperator === "eq" ? "Select Value" : "Select Values" watchOperator === "eq"
? t("labels.selectValue")
: t("labels.selectValues")
} }
disabled={!watchOperator}
onChange={onChange} onChange={onChange}
/> />
</Form.Control> </Form.Control>