-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement OnyxForm with disabled state injection (#1902)
Relates to #757 - Implement `OnyxForm` component: Wrapper around Form, which allows setting the `disabled` state for all child form elements - Add E2E and unit tests - Add Storybook Documentation - Update all form elements to use injected `disabled` state --------- Co-authored-by: Lars Rickert <[email protected]>
- Loading branch information
1 parent
27fae53
commit 5d8349c
Showing
34 changed files
with
531 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"sit-onyx": minor | ||
--- | ||
|
||
Implement OnyxForm which allows setting of disabled state for all child form elements |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@sit-onyx/storybook-utils": minor | ||
--- | ||
|
||
New createSymbolArgTypeEnhancer which adds description text to symbols used as default props |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { createSymbolArgTypeEnhancer } from "@sit-onyx/storybook-utils"; | ||
|
||
export const enhanceFormInjectedSymbol = createSymbolArgTypeEnhancer( | ||
"FORM_INJECTED_SYMBOL", | ||
"If no value (or `undefined`) is provided, `FORM_INJECTED_SYMBOL` is the internal default value for this prop.\n" + | ||
"In that case the props value will be derived from it's parent form (if it exists).\n", | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,9 @@ | ||
import { walkTree } from "@sit-onyx/storybook-utils"; | ||
import type { | ||
ArgTypesEnhancer, | ||
InputType, | ||
SBType, | ||
StrictInputType, | ||
} from "storybook/internal/types"; | ||
|
||
const SB_TYPE_CONTROL_MAP: Partial<Record<SBType["name"], InputType["control"]>> = { | ||
boolean: { type: "boolean" }, | ||
string: { type: "text" }, | ||
number: { type: "number" }, | ||
}; | ||
|
||
const getManagedParent = (inputType?: StrictInputType) => { | ||
if (!inputType?.type || inputType.table?.defaultValue?.summary !== "MANAGED_SYMBOL") { | ||
return undefined; | ||
} | ||
|
||
return walkTree(inputType.type, (elem, parent) => | ||
elem.name === "symbol" || (elem.name === "other" && elem.value === "unique symbol") | ||
? parent | ||
: undefined, | ||
); | ||
}; | ||
|
||
export const enhanceManagedSymbol: ArgTypesEnhancer = (context) => { | ||
Object.values(context.argTypes) | ||
.map((argType) => { | ||
const parent = getManagedParent(argType); | ||
return { argType, parent }; | ||
}) | ||
.filter(({ parent }) => parent) | ||
.forEach(({ argType, parent }) => { | ||
const firstAvailableControl = walkTree( | ||
parent || argType.type!, | ||
(sb) => SB_TYPE_CONTROL_MAP[sb.name], | ||
); | ||
|
||
if (firstAvailableControl && argType.table?.defaultValue) { | ||
argType.control = firstAvailableControl; | ||
argType.table.defaultValue.detail = | ||
"If no value (or `undefined`) is passed, `MANAGED_SYMBOL` is the internal default value for this prop.\n" + | ||
"It signals the component that the prop is managed and it's state tracked internally.\n" + | ||
"So in that case no prop binding or `v-model` is necessary.\n" + | ||
"Updates for the prop will still be emitted.\n"; | ||
} | ||
}); | ||
|
||
return context.argTypes; | ||
}; | ||
import { createSymbolArgTypeEnhancer } from "@sit-onyx/storybook-utils"; | ||
|
||
export const enhanceManagedSymbol = createSymbolArgTypeEnhancer( | ||
"MANAGED_SYMBOL", | ||
"If no value (or `undefined`) is passed, `MANAGED_SYMBOL` is the internal default value for this prop.\n" + | ||
"It signals the component that the prop is managed and it's state tracked internally.\n" + | ||
"So in that case no prop binding or `v-model` is necessary.\n" + | ||
"Updates for the prop will still be emitted.\n", | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
packages/sit-onyx/src/components/OnyxForm/OnyxForm.core.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { expect, it, vi } from "vitest"; | ||
import { reactive, toValue } from "vue"; | ||
import { | ||
FORM_INJECTED_SYMBOL, | ||
provideFormContext, | ||
useFormContext, | ||
type FORM_INJECTED, | ||
type FormInjected, | ||
type FormInjectedProps, | ||
} from "./OnyxForm.core"; | ||
|
||
let injected: (args: unknown[]) => void; | ||
|
||
vi.mock("vue", async (importOriginal) => { | ||
const mod = await importOriginal<typeof import("vue")>(); | ||
return { | ||
...mod, | ||
inject: () => injected, | ||
provide: (_: symbol, ctx: (...args: unknown[]) => void) => (injected = ctx), | ||
}; | ||
}); | ||
|
||
it.for([ | ||
{ | ||
formProps: { disabled: true }, | ||
localProps: { disabled: true }, | ||
expected: { disabled: true }, | ||
}, | ||
{ | ||
formProps: { disabled: false }, | ||
localProps: { disabled: true }, | ||
expected: { disabled: true }, | ||
}, | ||
{ | ||
formProps: { disabled: true }, | ||
localProps: { disabled: false }, | ||
expected: { disabled: false }, | ||
}, | ||
{ | ||
formProps: { disabled: false }, | ||
localProps: { disabled: false }, | ||
expected: { disabled: false }, | ||
}, | ||
{ | ||
formProps: { disabled: true }, | ||
localProps: { disabled: FORM_INJECTED_SYMBOL as FORM_INJECTED }, | ||
expected: { disabled: true }, | ||
}, | ||
{ | ||
formProps: { disabled: false }, | ||
localProps: { disabled: FORM_INJECTED_SYMBOL as FORM_INJECTED }, | ||
expected: { disabled: false }, | ||
}, | ||
{ | ||
formProps: undefined, | ||
localProps: { disabled: FORM_INJECTED_SYMBOL as FORM_INJECTED }, | ||
expected: { disabled: false }, | ||
}, | ||
{ | ||
formProps: undefined, | ||
localProps: { disabled: true }, | ||
expected: { disabled: true }, | ||
}, | ||
{ | ||
formProps: undefined, | ||
localProps: { disabled: false }, | ||
expected: { disabled: false }, | ||
}, | ||
])("it should derive expected state when correctly", ({ formProps, localProps, expected }) => { | ||
provideFormContext(formProps); | ||
const result = useFormContext(localProps); | ||
Object.entries(expected).forEach(([key, value]) => { | ||
const resultValue = toValue(result[key as keyof FormInjectedProps]); | ||
|
||
expect( | ||
resultValue, | ||
`Expected "${value}", got "${resultValue}" for formProps "${formProps}" and localProps "${localProps}"`, | ||
).toBe(value); | ||
}); | ||
}); | ||
|
||
it("should update when changed", async () => { | ||
const formProps = reactive({ disabled: false }); | ||
provideFormContext(formProps); | ||
|
||
const localProps = reactive({ disabled: FORM_INJECTED_SYMBOL as FormInjected<boolean> }); | ||
const { disabled } = useFormContext(localProps); | ||
expect(disabled.value).toBe(false); | ||
|
||
formProps.disabled = true; | ||
localProps.disabled = true; | ||
expect(disabled.value).toBe(true); | ||
localProps.disabled = false; | ||
expect(disabled.value).toBe(false); | ||
}); |
Oops, something went wrong.