-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCodeAutocompleteField.stories.tsx
More file actions
106 lines (97 loc) · 3.71 KB
/
CodeAutocompleteField.stories.tsx
File metadata and controls
106 lines (97 loc) · 3.71 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
import React from "react";
import { OverlaysProvider } from "@blueprintjs/core";
import { Meta, StoryFn } from "@storybook/react";
import { fn } from "@storybook/test";
import { helpersArgTypes } from "../../../.storybook/helpers";
import { CodeAutocompleteField, CodeAutocompleteFieldProps } from "../../../index";
import { CodeAutocompleteFieldPartialAutoCompleteResult } from "../AutoSuggestion/AutoSuggestion";
export default {
title: "Forms/CodeAutocompleteField",
component: CodeAutocompleteField,
argTypes: {
intent: {
...helpersArgTypes.exampleIntent,
options: ["UNDEFINED", "primary", "accent", "success", "warning", "danger"],
},
},
args: {
onInputChecked: fn(),
},
} as Meta<typeof CodeAutocompleteField>;
let forcedUpdateKey = 0; // @see https://github.com/storybookjs/storybook/issues/13375#issuecomment-1291011856
const Template: StoryFn<typeof CodeAutocompleteField> = (args) => (
<OverlaysProvider>
<CodeAutocompleteField {...args} key={++forcedUpdateKey} />
</OverlaysProvider>
);
const resultList = ["find me", "item", "auto-completion result"];
const defaultProps: CodeAutocompleteFieldProps = {
initialValue: "",
fetchSuggestions(
inputString: string,
cursorPosition: number
):
| CodeAutocompleteFieldPartialAutoCompleteResult
| undefined
| Promise<CodeAutocompleteFieldPartialAutoCompleteResult | undefined> {
const stringBeforeCursor = inputString.substring(0, cursorPosition);
const lastSpaceIdx = stringBeforeCursor.lastIndexOf(" ");
const searchWordStart = lastSpaceIdx >= 0 ? lastSpaceIdx + 1 : 0;
const lastWordBeforeCursor = stringBeforeCursor
.substring(searchWordStart, stringBeforeCursor.length)
.toLowerCase()
.trim();
const replacements = resultList.filter((item) => item.toLowerCase().includes(lastWordBeforeCursor));
if (replacements.length) {
return {
cursorPosition: cursorPosition,
inputString: inputString,
replacementResults: [
{
extractedQuery: lastWordBeforeCursor,
replacementInterval: {
from: searchWordStart,
length: lastWordBeforeCursor.length,
},
replacements: replacements.map((v) => ({
label: `Label of '${v}'`,
description: `Description of '${v}'`,
value: v,
})),
},
],
};
}
},
placeholder:
"The word before the cursor will be auto-completed. At the beginning or after a space, all results are shown.",
onChange(): void {
// Do nothing
},
};
/** Offers basic auto-completion for the word right before the cursor. */
export const Default = Template.bind({});
Default.args = defaultProps;
/** Shows validation error if the input string contains the word 'not'. */
export const WithValidation = Template.bind({});
WithValidation.args = {
...defaultProps,
initialValue: "Contains not",
checkInput: (inputString) => {
const notIndex = inputString.indexOf("not");
if (notIndex >= 0) {
return {
valid: false,
parseError: {
message: "Strings containing the sub-string 'not' are NOT allowed.",
start: notIndex,
end: notIndex + 3,
},
};
} else {
return {
valid: true,
};
}
},
};