-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcreate-options.tsx
213 lines (186 loc) · 5.98 KB
/
create-options.tsx
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { JSXElement } from "solid-js";
import { CreateSelectValue } from "./create-select";
import { fuzzyHighlight, fuzzySort } from "./fuzzy";
interface CreateOptionsOption {
value: CreateSelectValue;
label: JSXElement;
text: string;
disabled: boolean;
}
type CreateOptionsFilterableFunction = (
inputValue: string,
options: CreateOptionsOption[],
) => CreateOptionsOption[];
type CreateOptionsCreateableFunction = (
inputValue: string,
exists: boolean,
options: CreateOptionsOption[],
) => CreateSelectValue | CreateSelectValue[];
type CreateOptionsFormatFunction = (
value: CreateSelectValue,
type: "value" | "label",
meta: { highlight?: JSXElement; prefix?: string },
) => JSXElement;
const defaultFormat: CreateOptionsFormatFunction = (value, type, meta) =>
type === "label" ? (
<>
{meta.prefix}
{meta.highlight ?? value}
</>
) : (
value
);
interface CreateOptionsConfig {
key?: string;
format?: CreateOptionsFormatFunction;
filterable?: boolean | CreateOptionsFilterableFunction;
createable?: boolean | CreateOptionsCreateableFunction;
extractText?: (value: CreateSelectValue) => string;
disable?: (value: CreateSelectValue) => boolean;
}
const createOptions = (
values: CreateSelectValue[] | ((inputValue: string) => CreateSelectValue[]),
userConfig?: CreateOptionsConfig,
) => {
const config: CreateOptionsConfig &
Required<
Pick<CreateOptionsConfig, "extractText" | "filterable" | "disable">
> = Object.assign(
{
extractText: (value: CreateSelectValue) =>
value.toString ? value.toString() : value,
filterable: true,
disable: () => false,
},
userConfig || {},
);
if (
config.key &&
userConfig &&
(userConfig.format || userConfig.disable || userConfig.extractText)
) {
console.warn(
"When 'key' option is specified, custom 'format', 'disable' and",
"'extractText' functions will receive the keyed value rather than the",
"full object.",
);
}
if (
typeof config.createable === "function" &&
config.createable.length === 1
) {
console.warn(
'Outdated "createable" function signature detected.',
'Will only call if no option alredy "exists" as a result.',
'Please update function to accept "exists" as second parameter',
'and return "undefined" to prevent adding a create option.',
);
}
const resolveValue = (value: CreateSelectValue) =>
config.key ? value[config.key] : value;
const extractText = (value: CreateSelectValue) =>
config.extractText(resolveValue(value));
const format: CreateOptionsFormatFunction = (value, type, meta) => {
const resolvedValue = resolveValue(value);
return config.format
? config.format(resolvedValue, type, meta)
: defaultFormat(resolvedValue, type, meta);
};
const disable = (value: CreateSelectValue) =>
config.disable(resolveValue(value));
const options = (inputValue: string) => {
const initialValues =
typeof values === "function" ? values(inputValue) : values;
let createdOptions: CreateOptionsOption[] = initialValues.map((value) => {
return {
value,
label: format(value, "label", {}),
text: extractText(value),
disabled: disable(value),
};
});
if (config.filterable && inputValue) {
if (typeof config.filterable === "function") {
createdOptions = config.filterable(inputValue, createdOptions);
} else {
createdOptions = fuzzySort(inputValue, createdOptions, "text").map(
(result) => ({
...result.item,
label: format(result.item.value, "label", {
highlight: fuzzyHighlight(result),
}),
}),
);
}
}
if (config.createable !== undefined) {
const trimmedValue = inputValue.trim();
const exists = createdOptions.some((option) =>
areEqualIgnoringCase(inputValue, option.text),
);
if (trimmedValue) {
let valueOrValues: CreateSelectValue | CreateSelectValue[] | undefined;
if (typeof config.createable === "function") {
if (config.createable.length === 1) {
if (!exists) {
valueOrValues = config.createable(
trimmedValue,
exists,
createdOptions,
);
}
} else {
valueOrValues = config.createable(
trimmedValue,
exists,
createdOptions,
);
}
} else if (!exists) {
valueOrValues = config.key
? { [config.key]: trimmedValue }
: trimmedValue;
}
if (valueOrValues !== undefined) {
const values = Array.isArray(valueOrValues)
? valueOrValues
: [valueOrValues];
const optionsToAdd: CreateOptionsOption[] = [];
for (const value of values) {
optionsToAdd.push({
value: value,
label: format(value, "label", { prefix: "Create " }),
text: extractText(value),
disabled: false,
});
}
createdOptions = [...createdOptions, ...optionsToAdd];
}
}
}
return createdOptions;
};
return {
options,
optionToValue: (option: CreateOptionsOption) => option.value,
isOptionDisabled: (option: CreateOptionsOption) => option.disabled,
format: (
item: CreateOptionsOption | CreateSelectValue,
type: "option" | "value",
) =>
type === "option"
? (item as CreateOptionsOption).label
: format(item as CreateSelectValue, "value", {}),
};
};
const areEqualIgnoringCase = (firstString: string, secondString: string) =>
firstString.localeCompare(secondString, undefined, {
sensitivity: "base",
}) === 0;
export { createOptions, defaultFormat, areEqualIgnoringCase };
export type {
CreateOptionsOption,
CreateOptionsFormatFunction,
CreateOptionsFilterableFunction,
CreateOptionsCreateableFunction,
};