mirror of
https://github.com/sartography/spiff-arena.git
synced 2025-01-11 18:14:20 +00:00
added a little error handling to the typeahead widget w/ burnettk
This commit is contained in:
parent
5dfd96ca9e
commit
a528792257
@ -12,6 +12,7 @@ interface typeaheadArgs {
|
|||||||
readonly?: boolean;
|
readonly?: boolean;
|
||||||
rawErrors?: any;
|
rawErrors?: any;
|
||||||
placeholder?: string;
|
placeholder?: string;
|
||||||
|
label?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function TypeaheadWidget({
|
export default function TypeaheadWidget({
|
||||||
@ -23,13 +24,31 @@ export default function TypeaheadWidget({
|
|||||||
disabled,
|
disabled,
|
||||||
readonly,
|
readonly,
|
||||||
placeholder,
|
placeholder,
|
||||||
|
label,
|
||||||
rawErrors = [],
|
rawErrors = [],
|
||||||
}: typeaheadArgs) {
|
}: typeaheadArgs) {
|
||||||
const lastSearchTerm = useRef('');
|
const lastSearchTerm = useRef('');
|
||||||
const [items, setItems] = useState<any[]>([]);
|
const [items, setItems] = useState<any[]>([]);
|
||||||
const [selectedItem, setSelectedItem] = useState<any>(null);
|
const [selectedItem, setSelectedItem] = useState<any>(null);
|
||||||
const itemFormatRegex = /[^{}]+(?=})/g;
|
const itemFormatRegex = /[^{}]+(?=})/g;
|
||||||
const itemFormatSubstitutions = itemFormat.match(itemFormatRegex);
|
|
||||||
|
let itemFormatSubstitutions: string[] | null = null;
|
||||||
|
let invalid = false;
|
||||||
|
let errorMessageForField = null;
|
||||||
|
|
||||||
|
if (itemFormat) {
|
||||||
|
try {
|
||||||
|
itemFormatSubstitutions = itemFormat.match(itemFormatRegex);
|
||||||
|
} catch (e) {
|
||||||
|
errorMessageForField = `itemFormat does not contain replacement keys in curly braces. It should be like: "{key1} ({key2} - {key3})"`;
|
||||||
|
invalid = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!category) {
|
||||||
|
errorMessageForField = `category is not set in the ui:options for this field: ${label}`;
|
||||||
|
invalid = true;
|
||||||
|
}
|
||||||
|
|
||||||
const typeaheadSearch = useCallback(
|
const typeaheadSearch = useCallback(
|
||||||
(inputText: string) => {
|
(inputText: string) => {
|
||||||
@ -65,9 +84,13 @@ export default function TypeaheadWidget({
|
|||||||
}
|
}
|
||||||
|
|
||||||
let str = itemFormat;
|
let str = itemFormat;
|
||||||
itemFormatSubstitutions.forEach((key: string) => {
|
if (itemFormatSubstitutions) {
|
||||||
str = str.replace(`{${key}}`, item[key]);
|
itemFormatSubstitutions.forEach((key: string) => {
|
||||||
});
|
str = str.replace(`{${key}}`, item[key]);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
str = JSON.stringify(item);
|
||||||
|
}
|
||||||
return str;
|
return str;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -81,9 +104,7 @@ export default function TypeaheadWidget({
|
|||||||
helperText = uiSchema['ui:help'];
|
helperText = uiSchema['ui:help'];
|
||||||
}
|
}
|
||||||
|
|
||||||
let invalid = false;
|
if (!invalid && rawErrors && rawErrors.length > 0) {
|
||||||
let errorMessageForField = null;
|
|
||||||
if (rawErrors && rawErrors.length > 0) {
|
|
||||||
invalid = true;
|
invalid = true;
|
||||||
[errorMessageForField] = rawErrors;
|
[errorMessageForField] = rawErrors;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user