@sybilion/uilib 1.4.16 → 1.4.18
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/agent-glossary/workspace.generated.ts +1 -0
- package/dist/agent-glossary/workspace.index.md +1 -0
- package/dist/agent-glossary/workspace.md +1 -0
- package/dist/esm/components/ui/AppHeader/AppHeader.js +1 -1
- package/dist/esm/components/ui/Chip/Chip.js +1 -1
- package/dist/esm/components/ui/Form/Form.js +96 -0
- package/dist/esm/components/ui/Form/Form.styl.js +7 -0
- package/dist/esm/components/widgets/DriversComparisonChart/DriversComparisonChart.js +5 -18
- package/dist/esm/components/widgets/DriversComparisonChart/DriversComparisonChart.styl.js +1 -1
- package/dist/esm/components/widgets/DriversComparisonChart/driversComparisonChart.helpers.js +22 -5
- package/dist/esm/components/widgets/SybilionAppHeader/SybilionAppHeader.styl.js +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/types/src/components/ui/Form/Form.d.ts +48 -0
- package/dist/esm/types/src/components/ui/Form/Form.example.d.ts +1 -0
- package/dist/esm/types/src/components/ui/Form/Form.types.d.ts +14 -0
- package/dist/esm/types/src/components/ui/Form/index.d.ts +2 -0
- package/dist/esm/types/src/components/widgets/DriversComparisonChart/driversComparisonChart.helpers.d.ts +10 -0
- package/dist/esm/types/src/docs/pages/FormPage.d.ts +1 -0
- package/dist/esm/types/src/index.d.ts +1 -0
- package/dist/esm/types/tests/form.test.d.ts +1 -0
- package/package.json +9 -3
- package/src/components/ui/AppHeader/AppHeader.tsx +1 -0
- package/src/components/ui/Chip/Chip.tsx +3 -3
- package/src/components/ui/Form/AGENT.md +29 -0
- package/src/components/ui/Form/Form.example.tsx +193 -0
- package/src/components/ui/Form/Form.styl +12 -0
- package/src/components/ui/Form/Form.styl.d.ts +9 -0
- package/src/components/ui/Form/Form.tsx +231 -0
- package/src/components/ui/Form/Form.types.ts +32 -0
- package/src/components/ui/Form/index.ts +11 -0
- package/src/components/widgets/DriversComparisonChart/DriversComparisonChart.styl +4 -4
- package/src/components/widgets/DriversComparisonChart/DriversComparisonChart.tsx +21 -37
- package/src/components/widgets/DriversComparisonChart/driversComparisonChart.helpers.test.ts +49 -0
- package/src/components/widgets/DriversComparisonChart/driversComparisonChart.helpers.ts +40 -9
- package/src/components/widgets/SybilionAppHeader/SybilionAppHeader.styl +7 -2
- package/src/docs/App/App.tsx +17 -18
- package/src/docs/pages/FormPage.tsx +21 -0
- package/src/docs/registry.ts +6 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import { Button } from '#uilib/components/ui/Button';
|
|
4
|
+
import { Checkbox } from '#uilib/components/ui/Checkbox';
|
|
5
|
+
import { Dialog } from '#uilib/components/ui/Dialog';
|
|
6
|
+
import { Input } from '#uilib/components/ui/Input';
|
|
7
|
+
import { PageContentSection } from '#uilib/components/ui/Page';
|
|
8
|
+
import {
|
|
9
|
+
Select,
|
|
10
|
+
SelectContent,
|
|
11
|
+
SelectItem,
|
|
12
|
+
SelectTrigger,
|
|
13
|
+
SelectValue,
|
|
14
|
+
} from '#uilib/components/ui/Select';
|
|
15
|
+
import { Switch } from '#uilib/components/ui/Switch';
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
Form,
|
|
20
|
+
FormControl,
|
|
21
|
+
FormField,
|
|
22
|
+
FormItem,
|
|
23
|
+
FormLabel,
|
|
24
|
+
FormMessage,
|
|
25
|
+
} from './Form';
|
|
26
|
+
|
|
27
|
+
const REGION_OPTIONS = [
|
|
28
|
+
{ value: 'eu-west', label: 'EU West' },
|
|
29
|
+
{ value: 'us-east', label: 'US East' },
|
|
30
|
+
{ value: 'apac', label: 'APAC' },
|
|
31
|
+
] as const;
|
|
32
|
+
|
|
33
|
+
const schema = z.object({
|
|
34
|
+
name: z.string().min(1, 'Enter a name.'),
|
|
35
|
+
notes: z.string().optional(),
|
|
36
|
+
region: z.string().min(1, 'Pick a region.'),
|
|
37
|
+
notifications: z.boolean(),
|
|
38
|
+
termsAccepted: z.boolean().refine(value => value, {
|
|
39
|
+
message: 'Accept the terms to continue.',
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
type FormValues = z.infer<typeof schema>;
|
|
44
|
+
|
|
45
|
+
const defaultValues: FormValues = {
|
|
46
|
+
name: '',
|
|
47
|
+
notes: '',
|
|
48
|
+
region: '',
|
|
49
|
+
notifications: false,
|
|
50
|
+
termsAccepted: false,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export function FormExample() {
|
|
54
|
+
const [dialogOpen, setDialogOpen] = useState(false);
|
|
55
|
+
const [submittedValues, setSubmittedValues] = useState<FormValues | null>(
|
|
56
|
+
null,
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const handleSubmit = (values: FormValues) => {
|
|
60
|
+
setSubmittedValues(values);
|
|
61
|
+
setDialogOpen(true);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<PageContentSection>
|
|
66
|
+
<Form
|
|
67
|
+
schema={schema}
|
|
68
|
+
defaultValues={defaultValues}
|
|
69
|
+
onSubmit={handleSubmit}
|
|
70
|
+
style={{
|
|
71
|
+
display: 'flex',
|
|
72
|
+
flexDirection: 'column',
|
|
73
|
+
gap: '1.25rem',
|
|
74
|
+
width: '300px',
|
|
75
|
+
}}
|
|
76
|
+
>
|
|
77
|
+
<FormField
|
|
78
|
+
name="name"
|
|
79
|
+
render={({ field }) => (
|
|
80
|
+
<FormItem>
|
|
81
|
+
<FormLabel>Name</FormLabel>
|
|
82
|
+
<FormControl>
|
|
83
|
+
<Input placeholder="Your name" {...field} />
|
|
84
|
+
</FormControl>
|
|
85
|
+
<FormMessage />
|
|
86
|
+
</FormItem>
|
|
87
|
+
)}
|
|
88
|
+
/>
|
|
89
|
+
|
|
90
|
+
<FormField
|
|
91
|
+
name="notes"
|
|
92
|
+
render={({ field }) => (
|
|
93
|
+
<FormItem>
|
|
94
|
+
<FormLabel>Notes</FormLabel>
|
|
95
|
+
<FormControl>
|
|
96
|
+
<Input
|
|
97
|
+
type="textarea"
|
|
98
|
+
placeholder="Optional notes"
|
|
99
|
+
{...field}
|
|
100
|
+
/>
|
|
101
|
+
</FormControl>
|
|
102
|
+
<FormMessage />
|
|
103
|
+
</FormItem>
|
|
104
|
+
)}
|
|
105
|
+
/>
|
|
106
|
+
|
|
107
|
+
<FormField
|
|
108
|
+
name="region"
|
|
109
|
+
render={({ field }) => (
|
|
110
|
+
<FormItem>
|
|
111
|
+
<FormLabel>Region</FormLabel>
|
|
112
|
+
<Select
|
|
113
|
+
onValueChange={field.onChange}
|
|
114
|
+
value={field.value || undefined}
|
|
115
|
+
>
|
|
116
|
+
<FormControl>
|
|
117
|
+
<SelectTrigger style={{ width: '100%' }}>
|
|
118
|
+
<SelectValue placeholder="Pick a region" />
|
|
119
|
+
</SelectTrigger>
|
|
120
|
+
</FormControl>
|
|
121
|
+
<SelectContent>
|
|
122
|
+
{REGION_OPTIONS.map(option => (
|
|
123
|
+
<SelectItem key={option.value} value={option.value}>
|
|
124
|
+
{option.label}
|
|
125
|
+
</SelectItem>
|
|
126
|
+
))}
|
|
127
|
+
</SelectContent>
|
|
128
|
+
</Select>
|
|
129
|
+
<FormMessage />
|
|
130
|
+
</FormItem>
|
|
131
|
+
)}
|
|
132
|
+
/>
|
|
133
|
+
|
|
134
|
+
<FormField
|
|
135
|
+
name="notifications"
|
|
136
|
+
render={({ field }) => (
|
|
137
|
+
<FormItem>
|
|
138
|
+
<FormLabel>Email notifications</FormLabel>
|
|
139
|
+
<FormControl>
|
|
140
|
+
<Switch
|
|
141
|
+
checked={field.value}
|
|
142
|
+
onCheckedChange={field.onChange}
|
|
143
|
+
/>
|
|
144
|
+
</FormControl>
|
|
145
|
+
<FormMessage />
|
|
146
|
+
</FormItem>
|
|
147
|
+
)}
|
|
148
|
+
/>
|
|
149
|
+
|
|
150
|
+
<FormField
|
|
151
|
+
name="termsAccepted"
|
|
152
|
+
render={({ field }) => (
|
|
153
|
+
<FormItem>
|
|
154
|
+
<FormLabel>Terms</FormLabel>
|
|
155
|
+
<FormControl>
|
|
156
|
+
<Checkbox
|
|
157
|
+
checked={field.value}
|
|
158
|
+
onCheckedChange={field.onChange}
|
|
159
|
+
/>
|
|
160
|
+
</FormControl>
|
|
161
|
+
<FormMessage />
|
|
162
|
+
</FormItem>
|
|
163
|
+
)}
|
|
164
|
+
/>
|
|
165
|
+
|
|
166
|
+
<Button type="submit">Submit</Button>
|
|
167
|
+
</Form>
|
|
168
|
+
|
|
169
|
+
<Dialog
|
|
170
|
+
open={dialogOpen}
|
|
171
|
+
onOpenChange={setDialogOpen}
|
|
172
|
+
title="Submitted values"
|
|
173
|
+
content={
|
|
174
|
+
<pre
|
|
175
|
+
style={{
|
|
176
|
+
margin: 0,
|
|
177
|
+
whiteSpace: 'pre-wrap',
|
|
178
|
+
fontFamily: 'var(--font-mono, monospace)',
|
|
179
|
+
fontSize: 'var(--text-sm)',
|
|
180
|
+
}}
|
|
181
|
+
>
|
|
182
|
+
{submittedValues ? JSON.stringify(submittedValues, null, 2) : ''}
|
|
183
|
+
</pre>
|
|
184
|
+
}
|
|
185
|
+
footer={
|
|
186
|
+
<Button type="button" onClick={() => setDialogOpen(false)}>
|
|
187
|
+
Close
|
|
188
|
+
</Button>
|
|
189
|
+
}
|
|
190
|
+
/>
|
|
191
|
+
</PageContentSection>
|
|
192
|
+
);
|
|
193
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import cn from 'classnames';
|
|
2
|
+
import {
|
|
3
|
+
type ComponentProps,
|
|
4
|
+
type HTMLAttributes,
|
|
5
|
+
createContext,
|
|
6
|
+
useContext,
|
|
7
|
+
useId,
|
|
8
|
+
} from 'react';
|
|
9
|
+
import {
|
|
10
|
+
Controller,
|
|
11
|
+
type FieldPath,
|
|
12
|
+
type FieldValues,
|
|
13
|
+
FormProvider,
|
|
14
|
+
type Resolver,
|
|
15
|
+
type UseFormReturn,
|
|
16
|
+
useForm,
|
|
17
|
+
useFormContext,
|
|
18
|
+
useFormState,
|
|
19
|
+
} from 'react-hook-form';
|
|
20
|
+
|
|
21
|
+
import { Label } from '#uilib/components/ui/Label';
|
|
22
|
+
import { zodResolver } from '@hookform/resolvers/zod';
|
|
23
|
+
import { Slot } from '@radix-ui/react-slot';
|
|
24
|
+
|
|
25
|
+
import S from './Form.styl';
|
|
26
|
+
import type { FormFieldProps, FormProps } from './Form.types';
|
|
27
|
+
|
|
28
|
+
type FormFieldContextValue<
|
|
29
|
+
TFieldValues extends FieldValues = FieldValues,
|
|
30
|
+
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
|
31
|
+
> = {
|
|
32
|
+
name: TName;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const FormFieldContext = createContext<FormFieldContextValue | null>(null);
|
|
36
|
+
|
|
37
|
+
type FormItemContextValue = {
|
|
38
|
+
id: string;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const FormItemContext = createContext<FormItemContextValue | null>(null);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Root validated form. Owns `useForm`, applies `zodResolver(schema)` when `schema` is
|
|
45
|
+
* passed, and renders a native `<form>` wired to `onSubmit`. Wrap fields in {@link FormField}.
|
|
46
|
+
*/
|
|
47
|
+
function Form<TFieldValues extends FieldValues>({
|
|
48
|
+
schema,
|
|
49
|
+
defaultValues,
|
|
50
|
+
onSubmit,
|
|
51
|
+
resolver,
|
|
52
|
+
formOptions,
|
|
53
|
+
form: externalForm,
|
|
54
|
+
className,
|
|
55
|
+
children,
|
|
56
|
+
...formHTMLProps
|
|
57
|
+
}: FormProps<TFieldValues>) {
|
|
58
|
+
const internalForm = useForm<TFieldValues>({
|
|
59
|
+
defaultValues,
|
|
60
|
+
resolver:
|
|
61
|
+
resolver ??
|
|
62
|
+
(schema ? (zodResolver(schema) as Resolver<TFieldValues>) : undefined),
|
|
63
|
+
...formOptions,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const methods: UseFormReturn<TFieldValues> = externalForm ?? internalForm;
|
|
67
|
+
|
|
68
|
+
return (
|
|
69
|
+
<FormProvider {...methods}>
|
|
70
|
+
<form
|
|
71
|
+
data-slot="form"
|
|
72
|
+
className={cn(className)}
|
|
73
|
+
onSubmit={methods.handleSubmit(onSubmit)}
|
|
74
|
+
{...formHTMLProps}
|
|
75
|
+
>
|
|
76
|
+
{children}
|
|
77
|
+
</form>
|
|
78
|
+
</FormProvider>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Connects a field to react-hook-form via `Controller`. Reads `control` from the nearest
|
|
84
|
+
* {@link Form} context — do not pass `control` manually. Must wrap {@link FormItem} inside `render`.
|
|
85
|
+
*/
|
|
86
|
+
function FormField<
|
|
87
|
+
TFieldValues extends FieldValues = FieldValues,
|
|
88
|
+
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
|
89
|
+
>({ ...props }: FormFieldProps<TFieldValues, TName>) {
|
|
90
|
+
const { control } = useFormContext<TFieldValues>();
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<FormFieldContext.Provider value={{ name: props.name }}>
|
|
94
|
+
<Controller {...props} control={control} />
|
|
95
|
+
</FormFieldContext.Provider>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Vertical field shell; generates stable ids for label, control, description, and message. */
|
|
100
|
+
function FormItem({ className, ...props }: HTMLAttributes<HTMLDivElement>) {
|
|
101
|
+
const id = useId();
|
|
102
|
+
|
|
103
|
+
return (
|
|
104
|
+
<FormItemContext.Provider value={{ id }}>
|
|
105
|
+
<div data-slot="form-item" className={cn(S.item, className)} {...props} />
|
|
106
|
+
</FormItemContext.Provider>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Field metadata for {@link FormLabel}, {@link FormControl}, {@link FormDescription}, and
|
|
112
|
+
* {@link FormMessage}. Requires {@link FormField} + {@link FormItem} ancestors.
|
|
113
|
+
*/
|
|
114
|
+
function useFormField() {
|
|
115
|
+
const fieldContext = useContext(FormFieldContext);
|
|
116
|
+
const itemContext = useContext(FormItemContext);
|
|
117
|
+
const { getFieldState, control } = useFormContext();
|
|
118
|
+
const formState = useFormState({ control, name: fieldContext?.name });
|
|
119
|
+
const fieldState = getFieldState(fieldContext?.name ?? '', formState);
|
|
120
|
+
|
|
121
|
+
if (!fieldContext) {
|
|
122
|
+
throw new Error('useFormField should be used within <FormField>');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (!itemContext) {
|
|
126
|
+
throw new Error('useFormField should be used within <FormItem>');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const { id } = itemContext;
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
id,
|
|
133
|
+
name: fieldContext.name,
|
|
134
|
+
formItemId: `${id}-form-item`,
|
|
135
|
+
formDescriptionId: `${id}-form-item-description`,
|
|
136
|
+
formMessageId: `${id}-form-item-message`,
|
|
137
|
+
...fieldState,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Accessible label linked to the control id from {@link FormItem}. */
|
|
142
|
+
function FormLabel({ className, ...props }: ComponentProps<typeof Label>) {
|
|
143
|
+
const { formItemId } = useFormField();
|
|
144
|
+
|
|
145
|
+
return (
|
|
146
|
+
<Label
|
|
147
|
+
data-slot="form-label"
|
|
148
|
+
className={cn(className)}
|
|
149
|
+
htmlFor={formItemId}
|
|
150
|
+
{...props}
|
|
151
|
+
/>
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Slot wrapper for the actual input. Forwards `id`, `aria-invalid`, and `aria-describedby`
|
|
157
|
+
* so uilib controls show validation styling (P-07 inline errors).
|
|
158
|
+
*/
|
|
159
|
+
function FormControl({ ...props }: ComponentProps<typeof Slot>) {
|
|
160
|
+
const { error, formItemId, formDescriptionId, formMessageId } =
|
|
161
|
+
useFormField();
|
|
162
|
+
|
|
163
|
+
return (
|
|
164
|
+
<Slot
|
|
165
|
+
data-slot="form-control"
|
|
166
|
+
id={formItemId}
|
|
167
|
+
aria-describedby={
|
|
168
|
+
error ? `${formDescriptionId} ${formMessageId}` : formDescriptionId
|
|
169
|
+
}
|
|
170
|
+
aria-invalid={!!error}
|
|
171
|
+
{...props}
|
|
172
|
+
/>
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Optional helper text below the label; referenced by `aria-describedby` on {@link FormControl}. */
|
|
177
|
+
function FormDescription({
|
|
178
|
+
className,
|
|
179
|
+
...props
|
|
180
|
+
}: HTMLAttributes<HTMLParagraphElement>) {
|
|
181
|
+
const { formDescriptionId } = useFormField();
|
|
182
|
+
|
|
183
|
+
return (
|
|
184
|
+
<p
|
|
185
|
+
data-slot="form-description"
|
|
186
|
+
id={formDescriptionId}
|
|
187
|
+
className={cn(S.description, className)}
|
|
188
|
+
{...props}
|
|
189
|
+
/>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Inline validation message (`role="alert"`). Shows `fieldState.error.message` when present;
|
|
195
|
+
* omit for empty fields — never use toast for field errors.
|
|
196
|
+
*/
|
|
197
|
+
function FormMessage({
|
|
198
|
+
className,
|
|
199
|
+
children,
|
|
200
|
+
...props
|
|
201
|
+
}: HTMLAttributes<HTMLParagraphElement>) {
|
|
202
|
+
const { error, formMessageId } = useFormField();
|
|
203
|
+
const body = error ? String(error?.message ?? '') : children;
|
|
204
|
+
|
|
205
|
+
if (!body) {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return (
|
|
210
|
+
<p
|
|
211
|
+
data-slot="form-message"
|
|
212
|
+
id={formMessageId}
|
|
213
|
+
role="alert"
|
|
214
|
+
className={cn(S.message, className)}
|
|
215
|
+
{...props}
|
|
216
|
+
>
|
|
217
|
+
{body}
|
|
218
|
+
</p>
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export {
|
|
223
|
+
Form,
|
|
224
|
+
FormControl,
|
|
225
|
+
FormDescription,
|
|
226
|
+
FormField,
|
|
227
|
+
FormItem,
|
|
228
|
+
FormLabel,
|
|
229
|
+
FormMessage,
|
|
230
|
+
useFormField,
|
|
231
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ComponentPropsWithoutRef, ReactNode } from 'react';
|
|
2
|
+
import type {
|
|
3
|
+
ControllerProps,
|
|
4
|
+
DefaultValues,
|
|
5
|
+
FieldPath,
|
|
6
|
+
FieldValues,
|
|
7
|
+
Resolver,
|
|
8
|
+
SubmitHandler,
|
|
9
|
+
UseFormProps,
|
|
10
|
+
UseFormReturn,
|
|
11
|
+
} from 'react-hook-form';
|
|
12
|
+
|
|
13
|
+
import type { z } from 'zod';
|
|
14
|
+
|
|
15
|
+
export type FormProps<TFieldValues extends FieldValues = FieldValues> = Omit<
|
|
16
|
+
ComponentPropsWithoutRef<'form'>,
|
|
17
|
+
'onSubmit' | 'children'
|
|
18
|
+
> & {
|
|
19
|
+
schema?: z.ZodType<TFieldValues>;
|
|
20
|
+
defaultValues?: DefaultValues<TFieldValues>;
|
|
21
|
+
onSubmit: SubmitHandler<TFieldValues>;
|
|
22
|
+
resolver?: Resolver<TFieldValues>;
|
|
23
|
+
formOptions?: Omit<UseFormProps<TFieldValues>, 'defaultValues' | 'resolver'>;
|
|
24
|
+
form?: UseFormReturn<TFieldValues>;
|
|
25
|
+
children?: ReactNode;
|
|
26
|
+
className?: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type FormFieldProps<
|
|
30
|
+
TFieldValues extends FieldValues = FieldValues,
|
|
31
|
+
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
|
32
|
+
> = Omit<ControllerProps<TFieldValues, TName>, 'control'>;
|
|
@@ -104,10 +104,10 @@
|
|
|
104
104
|
position relative
|
|
105
105
|
cursor pointer
|
|
106
106
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
107
|
+
th:not(:first-child) > caption
|
|
108
|
+
justify-content flex-end
|
|
109
|
+
td:not(:first-child)
|
|
110
|
+
text-align right
|
|
111
111
|
|
|
112
112
|
.seriesColSeries
|
|
113
113
|
text-align left
|
|
@@ -37,6 +37,7 @@ import {
|
|
|
37
37
|
buildDriversComparisonChartData,
|
|
38
38
|
computeDriversComparisonHistoricalWindowFloor,
|
|
39
39
|
formatSeriesImportance,
|
|
40
|
+
getDriversWithDataSortedByImportance,
|
|
40
41
|
getLagDisplayForView,
|
|
41
42
|
mergeBacktestsChartData,
|
|
42
43
|
mergeDatasetHistoricalWithBacktestsChartData,
|
|
@@ -158,19 +159,10 @@ export function DriversComparisonChart({
|
|
|
158
159
|
[payload, viewTab],
|
|
159
160
|
);
|
|
160
161
|
|
|
161
|
-
const sortedDriversWithData = useMemo(
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
.filter(
|
|
166
|
-
d =>
|
|
167
|
-
d.normalized_series &&
|
|
168
|
-
Object.keys(d.normalized_series).some(
|
|
169
|
-
key => d.normalized_series![key] != null,
|
|
170
|
-
),
|
|
171
|
-
)
|
|
172
|
-
.sort((a, b) => String(a.id).localeCompare(String(b.id)));
|
|
173
|
-
}, [payloadForView?.drivers]);
|
|
162
|
+
const sortedDriversWithData = useMemo(
|
|
163
|
+
() => getDriversWithDataSortedByImportance(payloadForView?.drivers),
|
|
164
|
+
[payloadForView?.drivers],
|
|
165
|
+
);
|
|
174
166
|
|
|
175
167
|
const mergedChartData = useMemo(
|
|
176
168
|
() => mergeBacktestsChartData(payloadForView),
|
|
@@ -192,15 +184,9 @@ export function DriversComparisonChart({
|
|
|
192
184
|
datasetHistorical,
|
|
193
185
|
laggedMerged,
|
|
194
186
|
);
|
|
195
|
-
const sortedDrivers =
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
d.normalized_series &&
|
|
199
|
-
Object.keys(d.normalized_series).some(
|
|
200
|
-
key => d.normalized_series![key] != null,
|
|
201
|
-
),
|
|
202
|
-
)
|
|
203
|
-
.sort((a, b) => String(a.id).localeCompare(String(b.id)));
|
|
187
|
+
const sortedDrivers = getDriversWithDataSortedByImportance(
|
|
188
|
+
payload?.drivers,
|
|
189
|
+
);
|
|
204
190
|
const forecastIds = sortedDrivers.map(
|
|
205
191
|
(_, idx) => DRIVER_FORECAST_ID_BASE + idx,
|
|
206
192
|
);
|
|
@@ -226,21 +212,19 @@ export function DriversComparisonChart({
|
|
|
226
212
|
|
|
227
213
|
const tableSeriesRows = useMemo(() => {
|
|
228
214
|
if (sortedDriversWithData.length === 0) return [];
|
|
229
|
-
return sortedDriversWithData
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
})
|
|
243
|
-
.sort((a, b) => b.importance - a.importance);
|
|
215
|
+
return sortedDriversWithData.map((driver, idx) => {
|
|
216
|
+
const raw = driver.importance;
|
|
217
|
+
const importance =
|
|
218
|
+
typeof raw === 'number' && Number.isFinite(raw) ? raw : null;
|
|
219
|
+
const lagSource = originalDriversById.get(String(driver.id)) ?? driver;
|
|
220
|
+
return {
|
|
221
|
+
id: DRIVER_FORECAST_ID_BASE + idx,
|
|
222
|
+
name: driver.name || String(driver.id),
|
|
223
|
+
color: getForecastColor(idx + 1),
|
|
224
|
+
importance,
|
|
225
|
+
lag: getLagDisplayForView(resolveDriverLagLabel(lagSource), viewTab),
|
|
226
|
+
};
|
|
227
|
+
});
|
|
244
228
|
}, [originalDriversById, sortedDriversWithData, viewTab]);
|
|
245
229
|
|
|
246
230
|
const seriesInitKeyResolved =
|
package/src/components/widgets/DriversComparisonChart/driversComparisonChart.helpers.test.ts
CHANGED
|
@@ -5,8 +5,10 @@ import {
|
|
|
5
5
|
DRIVER_FORECAST_ID_BASE,
|
|
6
6
|
applyDriversComparisonViewToPayload,
|
|
7
7
|
buildDriversComparisonChartData,
|
|
8
|
+
compareDriversByImportance,
|
|
8
9
|
computeDriversComparisonHistoricalWindowFloor,
|
|
9
10
|
formatLagMonthsLabel,
|
|
11
|
+
getDriversWithDataSortedByImportance,
|
|
10
12
|
getLagDisplayForView,
|
|
11
13
|
mergeBacktestsChartData,
|
|
12
14
|
mergeDatasetHistoricalWithBacktestsChartData,
|
|
@@ -14,9 +16,56 @@ import {
|
|
|
14
16
|
resolveDriverLagLabel,
|
|
15
17
|
shiftNormalizedSeriesBackward,
|
|
16
18
|
shiftNormalizedSeriesForward,
|
|
19
|
+
sortDriversByImportance,
|
|
17
20
|
subtractMonthsFromMonthStart,
|
|
18
21
|
} from './driversComparisonChart.helpers';
|
|
19
22
|
|
|
23
|
+
describe('sortDriversByImportance', () => {
|
|
24
|
+
it('orders drivers by importance descending before id tie-break', () => {
|
|
25
|
+
const drivers = [
|
|
26
|
+
{ id: 'b', importance: 50, normalized_series: { '2015-01-01': 1 } },
|
|
27
|
+
{ id: 'a', importance: 100, normalized_series: { '2015-01-01': 1 } },
|
|
28
|
+
{ id: 'c', importance: 100, normalized_series: { '2015-01-01': 1 } },
|
|
29
|
+
];
|
|
30
|
+
expect(sortDriversByImportance(drivers).map(d => d.id)).toEqual([
|
|
31
|
+
'a',
|
|
32
|
+
'c',
|
|
33
|
+
'b',
|
|
34
|
+
]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('puts drivers without importance last', () => {
|
|
38
|
+
const drivers = [
|
|
39
|
+
{
|
|
40
|
+
id: 'z',
|
|
41
|
+
name: 'Z',
|
|
42
|
+
importance: null,
|
|
43
|
+
normalized_series: { '2015-01-01': 1 },
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
id: 'a',
|
|
47
|
+
name: 'A',
|
|
48
|
+
importance: 10,
|
|
49
|
+
normalized_series: { '2015-01-01': 1 },
|
|
50
|
+
},
|
|
51
|
+
];
|
|
52
|
+
expect(
|
|
53
|
+
getDriversWithDataSortedByImportance(drivers).map(d => d.id),
|
|
54
|
+
).toEqual(['a', 'z']);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe('compareDriversByImportance', () => {
|
|
59
|
+
it('breaks equal importance ties by id', () => {
|
|
60
|
+
expect(
|
|
61
|
+
compareDriversByImportance(
|
|
62
|
+
{ id: 'b', importance: 100 },
|
|
63
|
+
{ id: 'a', importance: 100 },
|
|
64
|
+
),
|
|
65
|
+
).toBeGreaterThan(0);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
20
69
|
describe('parseLagMonthsFromLabel', () => {
|
|
21
70
|
it('uses max month for ranges', () => {
|
|
22
71
|
expect(parseLagMonthsFromLabel('9 to 12 month(s)')).toBe(12);
|