2024-05-06 18:08:26 +02:00
|
|
|
import type { ReactNode } from 'react';
|
2024-06-04 23:06:34 +02:00
|
|
|
import * as s from 'superstruct';
|
2024-05-06 18:08:26 +02:00
|
|
|
|
|
|
|
import type { Loader } from './hooks';
|
|
|
|
|
2024-06-04 23:06:34 +02:00
|
|
|
export const Item = s.object({
|
|
|
|
label: s.string(),
|
|
|
|
value: s.string(),
|
|
|
|
data: s.any()
|
2024-05-06 18:08:26 +02:00
|
|
|
});
|
2024-06-04 23:06:34 +02:00
|
|
|
export type Item = s.Infer<typeof Item>;
|
2024-05-06 18:08:26 +02:00
|
|
|
|
2024-06-04 23:06:34 +02:00
|
|
|
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 ComboBoxPropsSchema = s.partial(
|
|
|
|
s.object({
|
|
|
|
id: s.string(),
|
|
|
|
className: s.string(),
|
|
|
|
name: s.string(),
|
|
|
|
label: s.string(),
|
|
|
|
description: s.string(),
|
|
|
|
isRequired: s.boolean(),
|
|
|
|
'aria-label': s.string(),
|
|
|
|
'aria-labelledby': s.string(),
|
|
|
|
'aria-describedby': s.string(),
|
|
|
|
items: s.union([s.array(Item), ArrayOfStrings, ArrayOfTuples]),
|
|
|
|
formValue: s.enums(['text', 'key']),
|
|
|
|
form: s.string(),
|
|
|
|
data: s.record(s.string(), s.string())
|
2024-05-06 18:08:26 +02:00
|
|
|
})
|
2024-06-04 23:06:34 +02:00
|
|
|
);
|
|
|
|
export const SingleComboBoxProps = s.assign(
|
|
|
|
ComboBoxPropsSchema,
|
|
|
|
s.partial(
|
|
|
|
s.object({
|
|
|
|
selectedKey: s.nullable(s.string()),
|
2024-07-09 16:16:07 +02:00
|
|
|
emptyFilterKey: s.nullable(s.string())
|
2024-06-04 23:06:34 +02:00
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
export const MultiComboBoxProps = s.assign(
|
|
|
|
ComboBoxPropsSchema,
|
|
|
|
s.partial(
|
|
|
|
s.object({
|
|
|
|
selectedKeys: s.array(s.string()),
|
|
|
|
allowsCustomValue: s.boolean(),
|
2024-09-23 15:20:59 +02:00
|
|
|
valueSeparator: s.union([s.string(), s.literal(false)])
|
2024-06-04 23:06:34 +02:00
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
export const RemoteComboBoxProps = s.assign(
|
|
|
|
ComboBoxPropsSchema,
|
|
|
|
s.partial(
|
|
|
|
s.object({
|
|
|
|
selectedKey: s.nullable(s.string()),
|
|
|
|
minimumInputLength: s.number(),
|
|
|
|
limit: s.number(),
|
2024-07-12 16:30:43 +02:00
|
|
|
allowsCustomValue: s.boolean(),
|
2024-07-12 17:31:38 +02:00
|
|
|
debounce: s.number(),
|
|
|
|
coerce: s.enums(['Default', 'AnnuaireEducation'])
|
2024-06-04 23:06:34 +02:00
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
export type SingleComboBoxProps = s.Infer<typeof SingleComboBoxProps> & {
|
2024-05-06 18:08:26 +02:00
|
|
|
children?: ReactNode;
|
|
|
|
};
|
2024-06-04 23:06:34 +02:00
|
|
|
export type MultiComboBoxProps = s.Infer<typeof MultiComboBoxProps>;
|
|
|
|
export type RemoteComboBoxProps = s.Infer<typeof RemoteComboBoxProps> & {
|
2024-05-06 18:08:26 +02:00
|
|
|
children?: ReactNode;
|
|
|
|
loader: Loader | string;
|
|
|
|
onChange?: (item: Item | null) => void;
|
|
|
|
};
|