Skip to content

Commit

Permalink
chore(OnyxForm): improve storybook docs (#1958)
Browse files Browse the repository at this point in the history
Relates to #1375 

- improve storybook documentation for `OnyxForm`
- add defaults and specific control for `showError`
- rename `ShowErrorMode` type and add `ShowErrorModes` constant for type
inference
  • Loading branch information
JoCa96 authored Oct 15, 2024
1 parent f8ce7fc commit d7d07f1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/sit-onyx/src/components/OnyxForm/OnyxForm.core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { computed, inject, provide, toRef, type InjectionKey, type Reactive, type Ref } from "vue";
import type { ShowErrorModes } from "../../composables/useErrorClass";
import type { ShowErrorMode } from "../../composables/useErrorClass";

const FORM_INJECTION_KEY = Symbol() as InjectionKey<ReturnType<typeof createFormInjectionContext>>;

Expand All @@ -24,7 +24,7 @@ export type FormProps = {
* The default is `"touched"`, which only shows an error *after* a user has significantly interacted with the input.
* See [:user-invalid](https://drafts.csswg.org/selectors/#user-invalid-pseudo).
*/
showError?: ShowErrorModes;
showError?: ShowErrorMode;
};

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/sit-onyx/src/components/OnyxForm/OnyxForm.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
import type { Meta, StoryObj } from "@storybook/vue3";
import { h } from "vue";
import { OnyxInput, OnyxToast } from "../..";
import { ShowErrorModes } from "../../composables/useErrorClass";
import FormExample from "../examples/FormExample/FormExample.vue";
import FormExampleSourceCode from "../examples/FormExample/FormExample.vue?raw";
import OnyxButton from "../OnyxButton/OnyxButton.vue";
Expand All @@ -15,6 +16,12 @@ import OnyxForm from "./OnyxForm.vue";
const meta: Meta<typeof OnyxForm> = {
title: "Form Elements/Form",
component: OnyxForm,
argTypes: {
showError: {
control: "select",
options: ShowErrorModes,
},
},
};

export default meta;
Expand Down
5 changes: 4 additions & 1 deletion packages/sit-onyx/src/components/OnyxForm/OnyxForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { useDensity } from "../../composables/density";
import { provideFormContext } from "./OnyxForm.core";
import type { OnyxFormProps } from "./types";
const props = defineProps<OnyxFormProps>();
const props = withDefaults(defineProps<OnyxFormProps>(), {
disabled: false,
showError: "touched",
});
defineSlots<{
/**
Expand Down
4 changes: 2 additions & 2 deletions packages/sit-onyx/src/composables/useErrorClass.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, expect, test } from "vitest";
import { ref } from "vue";
import { useErrorClass, type ShowErrorModes } from "./useErrorClass";
import { useErrorClass, type ShowErrorMode } from "./useErrorClass";

describe("useErrorClass", () => {
const showErrorMode = ref<ShowErrorModes>(true);
const showErrorMode = ref<ShowErrorMode>(true);
const errorClass = useErrorClass(showErrorMode);

test.each([
Expand Down
9 changes: 7 additions & 2 deletions packages/sit-onyx/src/composables/useErrorClass.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { computed, type Ref } from "vue";

/**
* All possible `showError` values.
*/
export const ShowErrorModes = [true, false, "touched"] as const;

/**
* Configures if and when errors are shown.
* When `true`, errors will be shown initially.
* When `false`, errors will never be shown.
* `"touched"` only shows an error *after* a user has significantly interacted with the input.
* See [:user-invalid](https://drafts.csswg.org/selectors/#user-invalid-pseudo).
*/
export type ShowErrorModes = boolean | "touched";
export type ShowErrorMode = (typeof ShowErrorModes)[number];

export const useErrorClass = (showError: Readonly<Ref<ShowErrorModes>>) =>
export const useErrorClass = (showError: Readonly<Ref<ShowErrorMode>>) =>
computed(() => {
if (showError.value === true) {
return "onyx-form-element--immediate-invalid";
Expand Down

0 comments on commit d7d07f1

Please sign in to comment.