-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathprops.ts
More file actions
141 lines (131 loc) · 3.82 KB
/
Copy pathprops.ts
File metadata and controls
141 lines (131 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import type { ReactNode } from 'react';
import * as s from 'superstruct';
import type { Loader } from './hooks';
export const Item = s.object({
id: s.optional(s.string()),
label: s.string(),
value: s.string(),
data: s.any()
});
export type Item = s.Infer<typeof Item>;
const ArrayOfTuples = s.coerce(
s.array(Item),
s.array(s.tuple([s.string(), s.union([s.string(), s.number()])])),
(items) =>
items.map<Item>(([label, value]) => ({ label, value: String(value) }))
);
const ArrayOfStrings = s.coerce(s.array(Item), s.array(s.string()), (items) =>
items.map<Item>((label) => ({ label, value: label }))
);
const ItemsSchema = s.union([s.array(Item), ArrayOfStrings, ArrayOfTuples]);
export const Section = s.object({
label: s.string(),
items: ItemsSchema
});
export type Section = { label: string; items: Item[] };
const ComboBoxPropsSchema = s.partial(
s.object({
id: s.string(),
inputId: s.string(),
className: s.string(),
name: s.string(),
label: s.string(),
labelId: s.string(), // if label is not in the component, we need to pass the label id
ariaLabelledbyPrefix: s.string(),
description: s.string(),
isRequired: s.boolean(),
isDisabled: s.boolean(),
'aria-label': s.string(),
'aria-labelledby': s.string(),
'aria-describedby': s.string(),
items: ItemsSchema,
formValue: s.enums(['text', 'key']),
form: s.string(),
data: s.record(s.string(), s.string())
})
);
export const SingleComboBoxProps = s.assign(
ComboBoxPropsSchema,
s.partial(
s.object({
selectedKey: s.nullable(s.string()),
emptyFilterKey: s.nullable(s.string()),
placeholder: s.string(),
sections: s.array(Section)
})
)
);
export const MultiComboBoxProps = s.assign(
ComboBoxPropsSchema,
s.partial(
s.object({
selectedKeys: s.array(s.string()),
allowsCustomValue: s.boolean(),
valueSeparator: s.union([s.string(), s.literal(false)]),
focusOnSelect: s.string(),
placeholder: s.string(),
tagsBelow: s.boolean(),
hideSelectedTags: s.boolean(),
sections: s.array(Section)
})
)
);
export const RemoteComboBoxProps = s.assign(
ComboBoxPropsSchema,
s.partial(
s.object({
selectedKey: s.nullable(s.string()),
minimumInputLength: s.number(),
limit: s.number(),
debounce: s.number(),
coerce: s.enums(['Default', 'AnnuaireEducation']),
placeholder: s.string(),
usePost: s.defaulted(s.boolean(), false),
translations: s.record(s.string(), s.string())
})
)
);
export type SingleComboBoxProps = s.Infer<typeof SingleComboBoxProps> & {
children?: ReactNode;
};
export type MultiComboBoxProps = s.Infer<typeof MultiComboBoxProps>;
export type RemoteComboBoxProps = s.Infer<typeof RemoteComboBoxProps> & {
children?: ReactNode;
loader: Loader | string;
translation?: Record<string, string>;
onChange?: (item: Item | null) => void;
};
const SelectProps = s.partial(
s.object({
items: s.union([s.array(Item), ArrayOfStrings, ArrayOfTuples]),
sections: s.array(Section),
id: s.string(),
triggerId: s.string(),
className: s.string(),
name: s.string(),
label: s.string(),
description: s.string(),
isRequired: s.boolean(),
isDisabled: s.boolean(),
'aria-label': s.string(),
'aria-labelledby': s.string(),
'aria-describedby': s.string(),
placeholder: s.string(),
data: s.record(s.string(), s.string()),
labelId: s.string(), // if label is not in the component, we need to pass the label id
ariaLabelledbyPrefix: s.string(),
alwaysShowKey: s.string()
})
);
export const SingleSelectProps = s.assign(
SelectProps,
s.object({
value: s.defaulted(s.nullable(s.string()), '')
})
);
export const MultipleSelectProps = s.assign(
SelectProps,
s.object({
value: s.defaulted(s.array(s.string()), [])
})
);