@ostack.tech/ui 0.13.2 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunks/{en-DtnihkTA.js → en-BiTLo8i1.js} +30 -1
- package/dist/chunks/en-BiTLo8i1.js.map +1 -0
- package/dist/locales/en-GB.js +1 -1
- package/dist/locales/en-US.js +1 -1
- package/dist/locales/fr-FR.js +29 -0
- package/dist/locales/fr-FR.js.map +1 -1
- package/dist/locales/pt-PT.js +29 -0
- package/dist/locales/pt-PT.js.map +1 -1
- package/dist/ostack-ui.css +5 -0
- package/dist/ostack-ui.js +1875 -622
- package/dist/ostack-ui.js.map +1 -1
- package/dist/types/components/Calendar/Calendar.d.ts +3 -3
- package/dist/types/components/Calendar/calendarUtils.d.ts +4 -0
- package/dist/types/components/Checkbox/Checkbox.d.ts +13 -1
- package/dist/types/components/CheckboxGroup/CheckboxGroup.d.ts +13 -1
- package/dist/types/components/CheckboxGroup/CheckboxGroupContext.d.ts +2 -1
- package/dist/types/components/CommandMenu/CommandMenu.d.ts +5 -3
- package/dist/types/components/Control/Control.d.ts +162 -0
- package/dist/types/components/Control/ControlContext.d.ts +13 -0
- package/dist/types/components/Control/ControlWithContainerRef.d.ts +14 -0
- package/dist/types/components/Control/controlUtils.d.ts +4 -0
- package/dist/types/components/Control/index.d.ts +4 -0
- package/dist/types/components/ControlAddon/ControlAddon.d.ts +1 -1
- package/dist/types/components/ControlCode/ControlCode.d.ts +1 -1
- package/dist/types/components/DataTable/DataTableFilter.d.ts +2 -3
- package/dist/types/components/DateInput/DateInput.d.ts +37 -7
- package/dist/types/components/DateRangeInput/DateRangeInput.d.ts +35 -9
- package/dist/types/components/Field/Field.d.ts +3 -13
- package/dist/types/components/Field/FieldContext.d.ts +7 -2
- package/dist/types/components/Field/index.d.ts +1 -1
- package/dist/types/components/Form/Form.d.ts +155 -0
- package/dist/types/components/Form/FormContext.d.ts +85 -0
- package/dist/types/components/Form/FormError.d.ts +28 -0
- package/dist/types/components/Form/FormReset.d.ts +7 -0
- package/dist/types/components/Form/FormState.d.ts +30 -0
- package/dist/types/components/Form/FormSubmit.d.ts +7 -0
- package/dist/types/components/Form/HiddenFormControl.d.ts +8 -0
- package/dist/types/components/Form/formUtils.d.ts +10 -0
- package/dist/types/components/Form/index.d.ts +6 -0
- package/dist/types/components/Form/useRegisterFormControl.d.ts +15 -0
- package/dist/types/components/Form/useResetControlOnFormReset.d.ts +2 -0
- package/dist/types/components/Input/Input.d.ts +22 -106
- package/dist/types/components/Input/index.d.ts +0 -1
- package/dist/types/components/NumericInput/NumericInput.d.ts +16 -15
- package/dist/types/components/NumericInput/numericInputUtils.d.ts +24 -0
- package/dist/types/components/Output/Output.d.ts +3 -3
- package/dist/types/components/RadioGroup/Radio.d.ts +1 -1
- package/dist/types/components/RadioGroup/RadioGroup.d.ts +17 -4
- package/dist/types/components/Select/Select.d.ts +17 -27
- package/dist/types/components/Select/SelectContext.d.ts +1 -1
- package/dist/types/components/Select/SelectNative.d.ts +2 -1
- package/dist/types/components/Stepper/Step.d.ts +1 -1
- package/dist/types/components/Table/Table.d.ts +1 -1
- package/dist/types/components/Table/TableCell.d.ts +1 -1
- package/dist/types/components/Table/TableContext.d.ts +1 -1
- package/dist/types/components/Tabs/Tab.d.ts +1 -1
- package/dist/types/components/Tabs/TabsContext.d.ts +1 -1
- package/dist/types/components/TextArea/TextArea.d.ts +14 -3
- package/dist/types/index.d.ts +2 -1
- package/dist/types/providers/LocalizationProvider/LocalizationObject.d.ts +2 -0
- package/dist/types/utils/setNativeProperty.d.ts +1 -1
- package/dist/types/utils/useConstant.d.ts +1 -1
- package/dist/types/utils/validationUtils.d.ts +4 -0
- package/package.json +5 -5
- package/scss/components/Form/_Form.scss +6 -0
- package/scss/index.scss +1 -0
- package/dist/chunks/en-DtnihkTA.js.map +0 -1
- package/dist/types/components/Input/InputWithContainerRef.d.ts +0 -14
- package/dist/types/utils/control.d.ts +0 -23
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { ComponentPropsWithRef, ReactNode, Ref, SubmitEvent } from 'react';
|
|
2
|
+
/** Form values. */
|
|
3
|
+
export type FormValues = Record<string, any>;
|
|
4
|
+
/** Validation mode of the form. */
|
|
5
|
+
export type FormValidationMode = "onSubmit" | "onChange" | "onBlur" | "onSubmitThenChange" | "onSubmitThenBlur";
|
|
6
|
+
/** Mode of displaying form errors. */
|
|
7
|
+
export type FormErrorDisplayMode = "first" | "all";
|
|
8
|
+
/** Native error codes of the form. */
|
|
9
|
+
export type FormNativeErrorCode = Exclude<keyof ValidityState, "valid" | "customError">;
|
|
10
|
+
/** Error messages to show for each native form error. */
|
|
11
|
+
export type FormNativeErrorMessages = Partial<Record<FormNativeErrorCode, ReactNode | ((el: RadioNodeList | Element) => ReactNode)>>;
|
|
12
|
+
/** Validation errors to set on a form. */
|
|
13
|
+
export type FormValidationErrors<T extends FormValues = any> = Partial<Record<keyof T | "", ReactNode>>;
|
|
14
|
+
/** Function used to validate a form. */
|
|
15
|
+
export type FormValidateFn<T extends FormValues = any> = (formValues: T) => ReactNode | FormValidationErrors | void | Promise<ReactNode | FormValidationErrors | void>;
|
|
16
|
+
/** Form API. */
|
|
17
|
+
export interface FormApi<T extends FormValues = any> {
|
|
18
|
+
getElement: (name?: keyof T | "") => RadioNodeList | Element | null;
|
|
19
|
+
getValue: <TName extends keyof T | "" | undefined>(name?: TName) => TName extends keyof T ? T[TName] : T;
|
|
20
|
+
isTouched: (name?: keyof T | "") => boolean;
|
|
21
|
+
setTouched: (name?: keyof T | "") => void;
|
|
22
|
+
setUntouched: (name?: keyof T | "") => void;
|
|
23
|
+
isDirty: (name?: keyof T | "") => boolean;
|
|
24
|
+
setDirty: (name?: keyof T | "") => void;
|
|
25
|
+
setPristine: (name?: keyof T | "") => void;
|
|
26
|
+
isValid: (name?: keyof T | "") => boolean;
|
|
27
|
+
validate: (name?: keyof T | "") => boolean | Promise<boolean>;
|
|
28
|
+
clearSubmitErrors: (name?: keyof T | "") => void;
|
|
29
|
+
clearExternalErrors: (name?: keyof T | "") => void;
|
|
30
|
+
focus: (name: keyof T | "") => void;
|
|
31
|
+
}
|
|
32
|
+
/** Properties of the form component. */
|
|
33
|
+
export interface FormProps<T extends FormValues = any> extends Omit<ComponentPropsWithRef<"form">, "noValidate" | "onSubmit"> {
|
|
34
|
+
/**
|
|
35
|
+
* Change the component to the HTML tag or custom component of the only child.
|
|
36
|
+
* This will merge the original component props with the props of the supplied
|
|
37
|
+
* element/component and change the underlying DOM node.
|
|
38
|
+
*/
|
|
39
|
+
asChild?: boolean;
|
|
40
|
+
/** Function used to validate the whole form. */
|
|
41
|
+
validate?: FormValidateFn<T>;
|
|
42
|
+
/**
|
|
43
|
+
* Specifies when the form should be validated:
|
|
44
|
+
*
|
|
45
|
+
* - `"onSubmit"`: Form is validated during submission.
|
|
46
|
+
* - `"onChange"`: Form is validated whenever controls change.
|
|
47
|
+
* - `"onBlur"`: Form is validated whenever controls lose focus.
|
|
48
|
+
* - `"onSubmitThenChange"`: Form is validated during the first submission.
|
|
49
|
+
* Afterwards, it is validated whenever controls change.
|
|
50
|
+
* - `"onSubmitThenBlur"`: Form is validated during the first submission.
|
|
51
|
+
* Afterwards, it is validated whenever controls lose focus.
|
|
52
|
+
*
|
|
53
|
+
* @default "onSubmitThenChange"
|
|
54
|
+
*/
|
|
55
|
+
validationMode?: FormValidationMode;
|
|
56
|
+
/**
|
|
57
|
+
* How errors should be displayed:
|
|
58
|
+
*
|
|
59
|
+
* - `"first"`: Only the first error of a field is shown.
|
|
60
|
+
* - `"all"`: All errors of a field are shown.
|
|
61
|
+
*
|
|
62
|
+
* @default "first"
|
|
63
|
+
*/
|
|
64
|
+
errorDisplayMode?: FormErrorDisplayMode;
|
|
65
|
+
/**
|
|
66
|
+
* External validation errors controlled manually. Each key of the object
|
|
67
|
+
* should correspond to the `name` of the field in question. A key containing
|
|
68
|
+
* an empty string represents a global error.
|
|
69
|
+
*
|
|
70
|
+
* Note that external errors provided via this property **will** prevent form
|
|
71
|
+
* submission, so they should be manually cleared as necessary, either by
|
|
72
|
+
* updating the property or using the `clearExternalErrors` API function.
|
|
73
|
+
*/
|
|
74
|
+
externalErrors?: FormValidationErrors<T>;
|
|
75
|
+
/** Error messages to show for each native form error. */
|
|
76
|
+
nativeErrorMessages?: FormNativeErrorMessages;
|
|
77
|
+
/**
|
|
78
|
+
* Whether to display a confirmation when attempting to unload the page when
|
|
79
|
+
* the form is dirty.
|
|
80
|
+
*
|
|
81
|
+
* @default false
|
|
82
|
+
*/
|
|
83
|
+
confirmUnloadWhenDirty?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Disables native validations such as `required`, `minLength`, `pattern`,
|
|
86
|
+
* _etc._ from affecting the form.
|
|
87
|
+
*/
|
|
88
|
+
disableNativeValidations?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Function called when the form is submitted. The default behaviour is
|
|
91
|
+
* automatically prevented.
|
|
92
|
+
*/
|
|
93
|
+
onSubmit?: (formValues: T, formData: FormData, evt: SubmitEvent<HTMLFormElement>) => FormValidationErrors | void | Promise<FormValidationErrors | void>;
|
|
94
|
+
/** Function called whenever a value of the form changes. */
|
|
95
|
+
onValueChange?: (formValues: T) => void;
|
|
96
|
+
/** Message shown when an error occurs while submitting the form. */
|
|
97
|
+
submitErrorMessage?: ReactNode | ((error: unknown) => ReactNode);
|
|
98
|
+
/** Reference to use the form API. */
|
|
99
|
+
apiRef?: Ref<FormApi<T> | null>;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The `Form` provides a wrapper over the browser's native validation
|
|
103
|
+
* capabilities and can be used to declaratively build simple forms.
|
|
104
|
+
*
|
|
105
|
+
* It composes the following subcomponents:
|
|
106
|
+
*
|
|
107
|
+
* - `FormError`: used to show errors associated with the form itself or one of
|
|
108
|
+
* its controls.
|
|
109
|
+
* - `FormState`: generic component which can be used to conditionally render UI
|
|
110
|
+
* based on the current state of the form.
|
|
111
|
+
* - `FormSubmit`: submit button for the form.
|
|
112
|
+
* - `FormReset`: reset button for the form.
|
|
113
|
+
*
|
|
114
|
+
* The following form controls are supported: `Checkbox`, `CheckboxGroup`,
|
|
115
|
+
* `DateInput`, `DateRangeInput`, `NumericInput`, `Input`, `RadioGroup`,
|
|
116
|
+
* `Select`, `TextArea`.
|
|
117
|
+
*
|
|
118
|
+
* Example usage:
|
|
119
|
+
*
|
|
120
|
+
* ```tsx
|
|
121
|
+
* import {
|
|
122
|
+
* Field,
|
|
123
|
+
* Form,
|
|
124
|
+
* FormError,
|
|
125
|
+
* FormSubmit,
|
|
126
|
+
* Input,
|
|
127
|
+
* Label,
|
|
128
|
+
* Stack,
|
|
129
|
+
* } from "@ostack.tech/ui";
|
|
130
|
+
*
|
|
131
|
+
* function SignInForm() {
|
|
132
|
+
* return (
|
|
133
|
+
* <Form onSubmit={signIn}>
|
|
134
|
+
* <Stack>
|
|
135
|
+
* <FormError />
|
|
136
|
+
* <Field>
|
|
137
|
+
* <Label>Email</Label>
|
|
138
|
+
* <Input type="email" name="email" required />
|
|
139
|
+
* <FormError />
|
|
140
|
+
* </Field>
|
|
141
|
+
* <Field>
|
|
142
|
+
* <Label>Password</Label>
|
|
143
|
+
* <Input type="password" name="password" required />
|
|
144
|
+
* <FormError />
|
|
145
|
+
* </Field>
|
|
146
|
+
* <FormSubmit>
|
|
147
|
+
* <Button>Sign in</Button>
|
|
148
|
+
* </FormSubmit>
|
|
149
|
+
* </Stack>
|
|
150
|
+
* </Form>
|
|
151
|
+
* );
|
|
152
|
+
* }
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
export declare const Form: import('react').ForwardRefExoticComponent<Omit<FormProps<FormValues>, "ref"> & import('react').RefAttributes<HTMLFormElement>>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { ReactNode, RefObject } from 'react';
|
|
2
|
+
import { NativeValidityStateKey } from '../../utils/validationUtils.ts';
|
|
3
|
+
import { FormApi, FormErrorDisplayMode, FormNativeErrorMessages, FormProps, FormValidationMode, FormValues } from './Form.tsx';
|
|
4
|
+
import { FormStateObject } from './FormState.tsx';
|
|
5
|
+
import { NormalizedFormValidationErrors } from './formUtils.ts';
|
|
6
|
+
export type FormControlElement = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
|
|
7
|
+
/** Value of the form's context. */
|
|
8
|
+
export interface FormContextValue<T extends FormValues = FormValues> {
|
|
9
|
+
validationMode: FormValidationMode;
|
|
10
|
+
errorDisplayMode: FormErrorDisplayMode;
|
|
11
|
+
nativeErrorMessages?: FormNativeErrorMessages;
|
|
12
|
+
disableNativeValidations?: boolean;
|
|
13
|
+
store: FormStore<T>;
|
|
14
|
+
}
|
|
15
|
+
/** State of the form's store. */
|
|
16
|
+
export interface FormStoreState<T extends FormValues = FormValues> {
|
|
17
|
+
submitting: boolean;
|
|
18
|
+
submitCount: number;
|
|
19
|
+
queuedValidate: boolean;
|
|
20
|
+
values: Partial<T>;
|
|
21
|
+
validateFns: Partial<Record<keyof T | "", (value: any) => void | Promise<void>>>;
|
|
22
|
+
touched: Partial<Record<keyof T, boolean>>;
|
|
23
|
+
dirty: Partial<Record<keyof T, boolean>>;
|
|
24
|
+
nativeErrors: Partial<Record<keyof T, FormNativeError[]>>;
|
|
25
|
+
validateErrors: NormalizedFormValidationErrors<T>;
|
|
26
|
+
formValidateErrors: NormalizedFormValidationErrors<T>;
|
|
27
|
+
submitErrors: NormalizedFormValidationErrors<T>;
|
|
28
|
+
externalErrors: NormalizedFormValidationErrors<T>;
|
|
29
|
+
actions: FormStoreActions<T>;
|
|
30
|
+
}
|
|
31
|
+
/** Form native error representation. */
|
|
32
|
+
export interface FormNativeError {
|
|
33
|
+
key: NativeValidityStateKey;
|
|
34
|
+
element: FormControlElement;
|
|
35
|
+
}
|
|
36
|
+
/** Actions of the form's store. */
|
|
37
|
+
interface FormStoreActions<T extends FormValues = FormValues> extends FormApi<T> {
|
|
38
|
+
registerControl: (name: keyof T, initialValue: unknown, validateFn: (value: any) => void | Promise<void>) => () => void;
|
|
39
|
+
onValueChange: () => void;
|
|
40
|
+
setValue: (name: keyof T, value: unknown) => void;
|
|
41
|
+
queueValidate: () => void;
|
|
42
|
+
firstInvalid: () => keyof T | "" | null;
|
|
43
|
+
runFormValidate: (formValues: T) => void | Promise<void>;
|
|
44
|
+
setNativeErrors: (name: keyof T, errors: FormNativeError[]) => void;
|
|
45
|
+
setValidateErrors: (name: keyof T | "", errors: ReactNode) => void;
|
|
46
|
+
}
|
|
47
|
+
/** Form's store. */
|
|
48
|
+
export type FormStore<T extends FormValues = FormValues> = ReturnType<typeof useCreateFormContext<T>>["store"];
|
|
49
|
+
/** Form's context. */
|
|
50
|
+
export declare const FormContext: import('react').Context<FormContextValue<FormValues> | null>;
|
|
51
|
+
/** Options used to create the form context. */
|
|
52
|
+
export interface UseCreateFormContextOptions<T extends FormValues = FormValues> extends Pick<FormProps<T>, "validationMode" | "errorDisplayMode" | "validate" | "externalErrors" | "nativeErrorMessages" | "disableNativeValidations" | "onValueChange" | "apiRef"> {
|
|
53
|
+
formRef: RefObject<HTMLFormElement | null>;
|
|
54
|
+
}
|
|
55
|
+
/** Hook which creates the form's context. */
|
|
56
|
+
export declare function useCreateFormContext<T extends FormValues = FormValues>({ validationMode, errorDisplayMode, validate, externalErrors, nativeErrorMessages, disableNativeValidations, onValueChange, formRef, apiRef, }: UseCreateFormContextOptions<T>): {
|
|
57
|
+
validationMode: FormValidationMode;
|
|
58
|
+
errorDisplayMode: FormErrorDisplayMode;
|
|
59
|
+
nativeErrorMessages: Partial<Record<import('./Form.tsx').FormNativeErrorCode, ReactNode | ((el: RadioNodeList | Element) => ReactNode)>> | undefined;
|
|
60
|
+
disableNativeValidations: boolean | undefined;
|
|
61
|
+
store: Omit<import('zustand').StoreApi<FormStoreState<T>>, "subscribe"> & {
|
|
62
|
+
subscribe: {
|
|
63
|
+
(listener: (selectedState: FormStoreState<T>, previousSelectedState: FormStoreState<T>) => void): () => void;
|
|
64
|
+
<U>(selector: (state: FormStoreState<T>) => U, listener: (selectedState: U, previousSelectedState: U) => void, options?: {
|
|
65
|
+
equalityFn?: ((a: U, b: U) => boolean) | undefined;
|
|
66
|
+
fireImmediately?: boolean;
|
|
67
|
+
} | undefined): () => void;
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
/** Hooks returning the form's context value. */
|
|
72
|
+
export declare function useFormContext<T extends FormValues = FormValues>(): FormContextValue<T>;
|
|
73
|
+
/** Hook providing access to the form's API. */
|
|
74
|
+
export declare function useFormApi<T extends FormValues = FormValues>(): FormApi<T>;
|
|
75
|
+
/** Hook providing access to a value of the form. */
|
|
76
|
+
export declare function useFormValue<T extends FormValues = FormValues>(): T;
|
|
77
|
+
export declare function useFormValue<T extends FormValues = FormValues>(name: ""): T;
|
|
78
|
+
export declare function useFormValue<T = any>(name: string): T;
|
|
79
|
+
/** Hook which returns `true` when the form is currently being submitted. */
|
|
80
|
+
export declare function useFormSubmitting(): boolean;
|
|
81
|
+
/** Hook providing access to the form's state. */
|
|
82
|
+
export declare function useFormState<T extends FormValues = FormValues>(): FormStateObject<T>;
|
|
83
|
+
export declare function useFormState<T extends FormValues = FormValues>(name: ""): FormStateObject<T>;
|
|
84
|
+
export declare function useFormState<T = any>(name: string): FormStateObject<T>;
|
|
85
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { FormErrorDisplayMode, FormNativeErrorMessages } from './Form.tsx';
|
|
3
|
+
/** Properties of the form error component. */
|
|
4
|
+
export interface FormErrorProps {
|
|
5
|
+
/**
|
|
6
|
+
* Name of the field for which to display the error.
|
|
7
|
+
*
|
|
8
|
+
* By default, the name is inherited from the parent `Field` component if one
|
|
9
|
+
* exists and was provided a name. Otherwise, it defaults to the empty string,
|
|
10
|
+
* which represents a global error.
|
|
11
|
+
*/
|
|
12
|
+
name?: string;
|
|
13
|
+
/**
|
|
14
|
+
* How errors should be displayed. This property is, by default, inherited
|
|
15
|
+
* from the `Form`.
|
|
16
|
+
*/
|
|
17
|
+
errorDisplayMode?: FormErrorDisplayMode;
|
|
18
|
+
/** Error messages to show for each native form error. */
|
|
19
|
+
nativeErrorMessages?: FormNativeErrorMessages;
|
|
20
|
+
/**
|
|
21
|
+
* Function to render the error messages. By default, the messages are
|
|
22
|
+
* rendered as an `Alert` component for global errors and as a `FeedbackList`
|
|
23
|
+
* component otherwise.
|
|
24
|
+
*/
|
|
25
|
+
children?: (messages: ReactNode[]) => ReactNode;
|
|
26
|
+
}
|
|
27
|
+
/** Error within a `Form`. */
|
|
28
|
+
export declare function FormError({ name, errorDisplayMode, nativeErrorMessages, children, }: FormErrorProps): ReactNode;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ComponentPropsWithoutRef, ReactElement } from 'react';
|
|
2
|
+
/** Properties of the form reset component. */
|
|
3
|
+
export interface FormResetProps extends Omit<ComponentPropsWithoutRef<"button">, "form" | "children"> {
|
|
4
|
+
children: ReactElement<HTMLButtonElement>;
|
|
5
|
+
}
|
|
6
|
+
/** Component used to wrap a button to reset the form. */
|
|
7
|
+
export declare function FormReset({ children, ...otherProps }: FormResetProps): ReactElement<HTMLButtonElement, string | import('react').JSXElementConstructor<any>>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
/** Object representing the state of a form or specific field. */
|
|
3
|
+
export interface FormStateObject<T = any> {
|
|
4
|
+
/** Whether the form is being submitted. */
|
|
5
|
+
submitting: boolean;
|
|
6
|
+
/** Number of submission attempts. */
|
|
7
|
+
submitCount: number;
|
|
8
|
+
/** HTML element representing the form or specific field. */
|
|
9
|
+
element: RadioNodeList | Element | null;
|
|
10
|
+
/** Value of the form or specific field. */
|
|
11
|
+
value: T;
|
|
12
|
+
/** Whether the field has been touched. */
|
|
13
|
+
touched: boolean;
|
|
14
|
+
/** Whether the field is dirty. */
|
|
15
|
+
dirty: boolean;
|
|
16
|
+
/** Whether the form or field is valid. */
|
|
17
|
+
valid: boolean;
|
|
18
|
+
}
|
|
19
|
+
/** Properties of the form state component. */
|
|
20
|
+
export interface FormStateProps<T = any> {
|
|
21
|
+
/**
|
|
22
|
+
* Name of the field for which to provide the state. By default, the state of
|
|
23
|
+
* the whole form is provided.
|
|
24
|
+
*/
|
|
25
|
+
name?: string;
|
|
26
|
+
/** Function used to render UI based on the form state. */
|
|
27
|
+
children: (state: FormStateObject<T>) => ReactNode;
|
|
28
|
+
}
|
|
29
|
+
/** Component providing access to the state of the form, or of a specific field. */
|
|
30
|
+
export declare function FormState<T = any>({ name, children }: FormStateProps<T>): ReactNode;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ComponentPropsWithoutRef, ReactElement } from 'react';
|
|
2
|
+
/** Properties of the form submit component. */
|
|
3
|
+
export interface FormSubmitProps extends Omit<ComponentPropsWithoutRef<"button">, "form" | "children"> {
|
|
4
|
+
children: ReactElement<HTMLButtonElement>;
|
|
5
|
+
}
|
|
6
|
+
/** Component used to wrap a button to submit the form. */
|
|
7
|
+
export declare function FormSubmit({ children, ...otherProps }: FormSubmitProps): ReactElement<HTMLButtonElement, string | import('react').JSXElementConstructor<any>>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ChangeEvent, ComponentPropsWithoutRef } from 'react';
|
|
2
|
+
/** Properties of the hidden native control. */
|
|
3
|
+
export type HiddenNativeControlProps<TElem extends "input" | "select" | "textarea"> = ComponentPropsWithoutRef<TElem> & {
|
|
4
|
+
actualElement: HTMLElement | null;
|
|
5
|
+
onChange: (evt: ChangeEvent<any>) => void;
|
|
6
|
+
};
|
|
7
|
+
/** Hidden native control meant to be used as the "real" control of a form. */
|
|
8
|
+
export declare function HiddenFormControl<TElem extends "input" | "select" | "textarea">({ actualElement, form, onFocus, ...otherProps }: HiddenNativeControlProps<TElem>): false | import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { FormValidationErrors, FormValues } from './Form.tsx';
|
|
3
|
+
/** Normalised form validation errors object. */
|
|
4
|
+
export type NormalizedFormValidationErrors<T extends FormValues = FormValues> = Partial<Record<keyof T | "", ReactNode[]>>;
|
|
5
|
+
/** Checks if the provided value is a form validation errors object. */
|
|
6
|
+
export declare function isFormValidationErrors(value: unknown): value is FormValidationErrors;
|
|
7
|
+
/** Normalises a form validation errors object. */
|
|
8
|
+
export declare function normalizeFormValidationErrors<T extends FormValues = FormValues>(errors?: FormValidationErrors): NormalizedFormValidationErrors<T>;
|
|
9
|
+
/** Transforms a node of errors into an array. */
|
|
10
|
+
export declare function arrayifyErrors(errors: ReactNode | undefined): ReactNode[];
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { ControlStatus } from '../Control';
|
|
3
|
+
import { FormValidationMode } from './Form.tsx';
|
|
4
|
+
/** Type of function used to validate a form control. */
|
|
5
|
+
export type FormControlValidateFn<T> = (value: T, formValues: Record<string, any>) => ReactNode | void | Promise<ReactNode | void>;
|
|
6
|
+
/** Result of the `useRegisterFormControl` hook. */
|
|
7
|
+
export interface UseRegisterFormControlResult<T = any> {
|
|
8
|
+
status: ControlStatus | undefined;
|
|
9
|
+
disabled: boolean;
|
|
10
|
+
setValue: (value: T) => void;
|
|
11
|
+
onChange: (value: T) => void;
|
|
12
|
+
onBlur: () => void;
|
|
13
|
+
}
|
|
14
|
+
/** Hook used to register a form control within a `Form`. */
|
|
15
|
+
export declare function useRegisterFormControl<T = any>(name: string | undefined, initialValue: T, validate: FormControlValidateFn<T> | undefined, validationMode?: FormValidationMode): UseRegisterFormControlResult<T>;
|
|
@@ -1,114 +1,30 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import { Spinner } from '../Spinner';
|
|
7
|
-
/** Accepted values for "show clear button". */
|
|
8
|
-
export type ShowClearButton = boolean | "auto";
|
|
9
|
-
export interface InputProps extends Omit<ComponentPropsWithoutRef<"input">, "children">, InputPropsBase {
|
|
10
|
-
}
|
|
11
|
-
/** Properties of the input component. */
|
|
12
|
-
export interface InputPropsBase {
|
|
13
|
-
/**
|
|
14
|
-
* Change the component to the HTML tag or custom component of the only child.
|
|
15
|
-
* This will merge the original component props with the props of the supplied
|
|
16
|
-
* element/component and change the underlying DOM node.
|
|
17
|
-
*/
|
|
18
|
-
asChild?: boolean;
|
|
19
|
-
children?: ReactElement;
|
|
20
|
-
/**
|
|
21
|
-
* Input's style variant. Defaults to the default variant unless when
|
|
22
|
-
* rendering inside a table cell (in which it defaults to `"cell"`).
|
|
23
|
-
*/
|
|
24
|
-
variant?: ControlVariant;
|
|
25
|
-
/** Sets the status of the input. */
|
|
26
|
-
status?: ControlStatus;
|
|
27
|
-
/** Input code. */
|
|
28
|
-
code?: ReactNode;
|
|
29
|
-
/**
|
|
30
|
-
* Renders the input in a **loading** state.
|
|
31
|
-
*
|
|
32
|
-
* When `true`, the input is “soft-disabled”: it looks disabled and ignores
|
|
33
|
-
* user interaction but remains focusable. By default, a spinner is shown
|
|
34
|
-
* (_i.e._, `showSpinner` is set to `true` unless overridden).
|
|
35
|
-
*
|
|
36
|
-
* The input's accessible description while loading can be controlled via
|
|
37
|
-
* `loadingDescription`.
|
|
38
|
-
*
|
|
39
|
-
* To only show a spinner without “soft-disabling” the input, set
|
|
40
|
-
* `showSpinner` instead.
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
*
|
|
44
|
-
* ```tsx
|
|
45
|
-
* <Input loading={fetching} loadingDescription="Fetching…" />;
|
|
46
|
-
* ```
|
|
47
|
-
*
|
|
48
|
-
* @default false
|
|
49
|
-
*/
|
|
50
|
-
loading?: boolean;
|
|
51
|
-
/** Accessible description of the input when it is in a “loading” state. */
|
|
52
|
-
loadingDescription?: ReactNode;
|
|
53
|
-
/**
|
|
54
|
-
* Whether to show a spinner in the input.
|
|
55
|
-
*
|
|
56
|
-
* Defaults to the value of the `loading` property. In most cases, prefer
|
|
57
|
-
* `loading`, which also “soft-disables” the input.
|
|
58
|
-
*
|
|
59
|
-
* Use `showSpinner` when you want a spinner **without** “soft-disabling” the
|
|
60
|
-
* input. To “soft-disable” **without** a spinner, set `loading` to `true` and
|
|
61
|
-
* `showSpinner` to `false`.
|
|
62
|
-
*
|
|
63
|
-
* @default loading
|
|
64
|
-
*/
|
|
65
|
-
showSpinner?: boolean;
|
|
66
|
-
/** Sets the input's text align. */
|
|
67
|
-
align?: Responsive<CSSProperties["textAlign"]>;
|
|
68
|
-
/** Input's start adornment. */
|
|
69
|
-
startAdornment?: ReactNode;
|
|
70
|
-
/** Input's end adornment. */
|
|
71
|
-
endAdornment?: ReactNode;
|
|
72
|
-
/**
|
|
73
|
-
* Whether to show a button to clear the input when it has content. Defaults
|
|
74
|
-
* `"auto"` when `type="search"`, `false` otherwise.
|
|
75
|
-
*/
|
|
76
|
-
showClearButton?: ShowClearButton;
|
|
77
|
-
/** Label of the clear button announced to assistive technologies. */
|
|
78
|
-
clearButtonLabel?: string;
|
|
79
|
-
/**
|
|
80
|
-
* Accessible description for how to clear the control when the clear button
|
|
81
|
-
* is visible.
|
|
82
|
-
*
|
|
83
|
-
* This field should be provided when adding extra keybinds to clear the
|
|
84
|
-
* control (_e.g._ the input is a file input and pressing Backspace/Delete
|
|
85
|
-
* clears it).
|
|
86
|
-
*/
|
|
87
|
-
clearDescription?: ReactNode;
|
|
1
|
+
import { ControlFileInputMultipleProps, ControlFileInputSingleProps, ControlNonFileInputProps } from '../Control';
|
|
2
|
+
import { FormValidationMode } from '../Form';
|
|
3
|
+
import { FormControlValidateFn } from '../Form/useRegisterFormControl.ts';
|
|
4
|
+
/** Base properties of the input component. */
|
|
5
|
+
export interface InputOwnProps<T> {
|
|
88
6
|
/**
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* @param value New value.
|
|
7
|
+
* Custom validation function meant to validate the control when it is part of
|
|
8
|
+
* a `Form`.
|
|
92
9
|
*/
|
|
93
|
-
|
|
10
|
+
validate?: FormControlValidateFn<T>;
|
|
94
11
|
/**
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
* This is useful when there is a need to know when the user stops interacting
|
|
98
|
-
* with the control.
|
|
12
|
+
* Specifies when the control should be validated. Defaults to the value
|
|
13
|
+
* specified in the `Form`.
|
|
99
14
|
*/
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
spinnerProps?: Omit<ComponentPropsWithRef<typeof Spinner>, "size">;
|
|
15
|
+
validationMode?: FormValidationMode;
|
|
16
|
+
}
|
|
17
|
+
/** Base properties of a single file input. */
|
|
18
|
+
export interface InputSingleFileProps extends ControlFileInputSingleProps, InputOwnProps<File | null> {
|
|
19
|
+
}
|
|
20
|
+
/** Base properties of a multiple file input. */
|
|
21
|
+
export interface InputMultipleFileProps extends ControlFileInputMultipleProps, InputOwnProps<File[]> {
|
|
22
|
+
}
|
|
23
|
+
/** Base properties of a non-file input. */
|
|
24
|
+
export interface InputNonFileProps extends ControlNonFileInputProps, InputOwnProps<string> {
|
|
111
25
|
}
|
|
26
|
+
/** Properties of the control component. */
|
|
27
|
+
export type InputProps = InputSingleFileProps | InputMultipleFileProps | InputNonFileProps;
|
|
112
28
|
/**
|
|
113
29
|
* The input component is used to collect user input. It is built on top of the
|
|
114
30
|
* browser's [`<input>`
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { ComponentPropsWithoutRef } from 'react';
|
|
2
1
|
import { NumericTransformerOptions } from '../../utils/useNumericTransformer.tsx';
|
|
3
|
-
import {
|
|
2
|
+
import { ControlNonFileInputProps } from '../Control';
|
|
3
|
+
import { FormValidationMode } from '../Form';
|
|
4
|
+
import { FormControlValidateFn } from '../Form/useRegisterFormControl.ts';
|
|
4
5
|
/** Properties of the numeric input component. */
|
|
5
|
-
export interface NumericInputProps extends NumericTransformerOptions, Omit<
|
|
6
|
+
export interface NumericInputProps extends NumericTransformerOptions, Omit<ControlNonFileInputProps, "defaultValue" | "value" | "onValueChange" | "validate" | "type" | "min" | "max"> {
|
|
6
7
|
type?: "text" | "tel" | "password";
|
|
7
8
|
/** Numeric input's default value. */
|
|
8
9
|
defaultValue?: string | number | bigint | null;
|
|
@@ -31,37 +32,37 @@ export interface NumericInputProps extends NumericTransformerOptions, Omit<Compo
|
|
|
31
32
|
/**
|
|
32
33
|
* Minimum number allowed. If a string is provided, it should **not** be the
|
|
33
34
|
* formatted representation, e.g. `"999999.99" should be provided instead of
|
|
34
|
-
* `"999 999,99 €"
|
|
35
|
-
* property.
|
|
35
|
+
* `"999 999,99 €"`.
|
|
36
36
|
*/
|
|
37
37
|
min?: string | number | bigint | null;
|
|
38
38
|
/**
|
|
39
39
|
* Maximum number allowed. If a string is provided, it should **not** be the
|
|
40
40
|
* formatted representation, e.g. `"999999.99" should be provided instead of
|
|
41
|
-
* `"999 999,99 €"
|
|
42
|
-
* property.
|
|
41
|
+
* `"999 999,99 €"`.
|
|
43
42
|
*/
|
|
44
43
|
max?: string | number | bigint | null;
|
|
45
44
|
/**
|
|
46
45
|
* Value that the number should be greater than. If a string is provided, it
|
|
47
46
|
* should **not** be the formatted representation, e.g. `"999999.99" should be
|
|
48
|
-
* provided instead of `"999 999,99 €"
|
|
49
|
-
* no effect on this property.
|
|
47
|
+
* provided instead of `"999 999,99 €"`.
|
|
50
48
|
*/
|
|
51
49
|
exclusiveMin?: string | number | bigint | null;
|
|
52
50
|
/**
|
|
53
51
|
* Value that the number should be less than. If a string is provided, it
|
|
54
52
|
* should **not** be the formatted representation, e.g. `"999999.99" should be
|
|
55
|
-
* provided instead of `"999 999,99 €"
|
|
56
|
-
* no effect on this property.
|
|
53
|
+
* provided instead of `"999 999,99 €"`.
|
|
57
54
|
*/
|
|
58
55
|
exclusiveMax?: string | number | bigint | null;
|
|
59
56
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
|
|
57
|
+
* Custom validation function meant to validate the control when it is part of
|
|
58
|
+
* a `Form`.
|
|
59
|
+
*/
|
|
60
|
+
validate?: FormControlValidateFn<string>;
|
|
61
|
+
/**
|
|
62
|
+
* Specifies when the control should be validated. Defaults to the value
|
|
63
|
+
* specified in the `Form`.
|
|
63
64
|
*/
|
|
64
|
-
|
|
65
|
+
validationMode?: FormValidationMode;
|
|
65
66
|
}
|
|
66
67
|
/** Representations of the numeric value. */
|
|
67
68
|
export interface NumericValueRepresentations {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { NumberFormatValues, NumericFormatProps } from 'react-number-format';
|
|
2
|
+
import { NumericInputProps, NumericValueRepresentations } from './NumericInput.tsx';
|
|
3
|
+
/** Validates numeric props. */
|
|
4
|
+
export declare function validateNumericProps({ valueIsInteger, precision, scale, decimalSeparator, groupSeparator, minString, maxString, exclusiveMinString, exclusiveMaxString, }: Pick<NumericInputProps, "valueIsInteger" | "precision" | "scale" | "decimalSeparator" | "groupSeparator"> & {
|
|
5
|
+
minString?: string;
|
|
6
|
+
maxString?: string;
|
|
7
|
+
exclusiveMinString?: string;
|
|
8
|
+
exclusiveMaxString?: string;
|
|
9
|
+
}): void;
|
|
10
|
+
/**
|
|
11
|
+
* Transforms the provided value into a string value to be accepted by
|
|
12
|
+
* `react-number-format`. In particular, the value needs transformation when it
|
|
13
|
+
* is an integer value with a scale.
|
|
14
|
+
*/
|
|
15
|
+
export declare function valueToRNFString(value: string | number | bigint | null | undefined, valueIsInteger: boolean, formatProps: NumericFormatProps): string;
|
|
16
|
+
/** Transforms the provided value into a string value. */
|
|
17
|
+
export declare function valueToStringValue(value: string | number | bigint | null | undefined, valueIsInteger: boolean, formatProps: NumericFormatProps): string;
|
|
18
|
+
/**
|
|
19
|
+
* Converts the `react-number-format` values representation into our own
|
|
20
|
+
* representation, while handling the case of integer values with a scale.
|
|
21
|
+
*/
|
|
22
|
+
export declare function transformNumberFormatValues({ formattedValue, value, floatValue }: NumberFormatValues, valueIsInteger: boolean, scale: number | undefined): NumericValueRepresentations;
|
|
23
|
+
/** Transforms the numeric scale into a `step` value. */
|
|
24
|
+
export declare function scaleToStep(valueIsInteger: boolean, scale: number | undefined): string | undefined;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { ControlOutputProps } from '../Control';
|
|
3
3
|
/** Properties of the output component. */
|
|
4
|
-
export interface OutputProps extends Omit<
|
|
4
|
+
export interface OutputProps extends Omit<ControlOutputProps, "asChild" | "disabled" | "loading" | "loadingDescription" | "showClearButton" | "clearButtonLabel" | "onValueChange" | "onBlurToOutside" | "clearButtonProps"> {
|
|
5
5
|
/**
|
|
6
6
|
* Renders the output in a **pending** state.
|
|
7
7
|
*
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { RadioGroup as RadioGroupPrimitive } from 'radix-ui';
|
|
2
2
|
import { ComponentPropsWithoutRef, ComponentPropsWithRef, ReactNode } from 'react';
|
|
3
|
-
import { ControlStatus } from '
|
|
3
|
+
import { ControlStatus } from '../Control';
|
|
4
4
|
import { ControlCode } from '../ControlCode';
|
|
5
5
|
import { Label } from '../Label';
|
|
6
6
|
import { Spinner } from '../Spinner';
|