Skip to content

Commit a522cca

Browse files
Mudaafideomsj
andauthored
[PSL-1596] Making onSubmit a required prop (#33)
* fix: remove unresolved merge conflicts * qol: Makes onSubmit Required * Docs: Update docs to include now required onSubmit * lint * fix: Standardize double quotes and fix brace padding * build: docs * build: rebuilt docs * sync docs with main * sync docs with main --------- Co-authored-by: Jesse DeOms <[email protected]>
1 parent eb852c2 commit a522cca

14 files changed

+179
-118
lines changed

src/constants.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { AutocompleteSubmitEvent } from './types';
2+
13
// Autocomplete key index
24
export const apiKey = 'key_Gep3oQOu5IMcNh9A';
35

@@ -7,7 +9,7 @@ export const apiKey = 'key_Gep3oQOu5IMcNh9A';
79

810
export const componentDescription = `- import \`CioAutocomplete\` to render in your JSX.
911
- This component handles state management, data fetching, and rendering logic.
10-
- To use this component, an \`apiKey\` or \`cioJsClient\` are required, all other values are optional.
12+
- To use this component, an \`apiKey\` or \`cioJsClient\` are required, and an \`onSubmit\` callback must be passed. All other values are optional.
1113
- Use different props to configure the behavior of this component.
1214
- The following stories shows how different props affect the component's behavior
1315
@@ -16,7 +18,7 @@ export const componentDescription = `- import \`CioAutocomplete\` to render in y
1618

1719
export const hookDescription = `- import \`useCioAutocomplete\` and call this custom hook in a functional component.
1820
- This hook handles state management & data fetching, but leaves rendering logic up to you
19-
- To use this hook, an \`apiKey\` or \`cioJsClient\` are required, all other values are optional.
21+
- To use this hook, an \`apiKey\` or \`cioJsClient\` are required, and an \`onSubmit\` callback must be passed. All other values are optional.
2022
- Pass different options to the \`useCioAutocomplete\` hook to configure behavior.
2123
- The following stories shows how different options affect the hook's behavior
2224
@@ -50,7 +52,7 @@ const {
5052
/// //////////////////////////////
5153

5254
export const sectionsDescription = `- by default, typing a query will fetch data for search suggestions and Products
53-
- to override this, pass a an array of sections objects
55+
- to override this, pass an array of sections objects
5456
- the order of the objects in the \`sections\` array determines the order of the results
5557
- each section object must have an \`identifier\`
5658
- each section object can specify a \`type\`
@@ -77,7 +79,7 @@ When no values are passed for the \`sections\` argument, the following defaults
7779
export const userEventsDescription = `- pass callback functions to respond to user events
7880
- if provided, the onFocus callback function will be called each time the user focuses on the text input field
7981
- if provided, the onChange callback function will be called each time the user changes the value in the text input field
80-
- if provided, the onSubmit callback function will be called each time the user submits the form
82+
- the onSubmit callback function will be called each time the user submits the form
8183
- the user can submit the form by pressing the enter key in the text input field, clicking a submit button within the form, clicking on a result, or pressing enter while a result is selected
8284
8385
> ⚠️ NOTE ⚠️ Use the Storybook Canvas Actions tab to explore the behavior of all of these \`OnEvent\` callback functions as you interact with our Default User Events example rendered in the Canvas. In the stories below, Storybook Canvas Actions have been disabled to focus on each of these callback functions in isolation. Each of the example callback functions in the stories below log output to the console tab of the browser's developer tools.`;
@@ -118,6 +120,8 @@ export const onSubmitDescription = `Pass an \`onSubmit\` callback function to ex
118120
- if the user selects a suggested item from the dropdown list:
119121
- the \`originalQuery\` field will provide the value of the input field that generated the selected item
120122
- an \`item\` object with information about the suggestion that the user selected`;
123+
// eslint-disable-next-line no-console
124+
export const onSubmitDefault = (submitEvent: AutocompleteSubmitEvent) => console.dir(submitEvent);
121125
export const zeroStateSectionsDescription = `Use \`zeroStateSections\` to show suggestions after a user applies focus to the search input field and before they start typing a query`;
122126
export const openOnFocusDescription = `Use \`openOnFocus: false\` to show suggestions after a user clears their query, but not when they initially apply focus to the search input field`;
123127
export const multipleSectionsDescription = `Use as many different \`recommendations\` and \`custom\` sections as you'd like and in whatever order you would like!`;

src/hooks/useDownShift.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let idCounter = 0;
77
type UseDownShiftOptions = {
88
setQuery: React.Dispatch<React.SetStateAction<string>>;
99
items: Item[];
10-
onSubmit?: OnSubmit;
10+
onSubmit: OnSubmit;
1111
previousQuery?: string;
1212
cioClient?: ConstructorIOClient;
1313
onChange?: (string) => void;

src/stories/Autocomplete/Component/Sections.stories.tsx

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CioAutocomplete } from '../../../index';
22
import { argTypes } from '../argTypes';
3-
import { stringify } from '../../../utils';
3+
import { stringifyWithDefaults } from '../../../utils';
44
import {
55
contentDescription,
66
customSectionDescription,
@@ -11,6 +11,7 @@ import {
1111
sectionOrderDescription,
1212
sectionsDescription,
1313
apiKey,
14+
onSubmitDefault as onSubmit,
1415
} from '../../../constants';
1516
import { ComponentTemplate, getComponentStoryParams, addComponentStoryDescription } from '.';
1617

@@ -28,12 +29,13 @@ export default {
2829
};
2930

3031
export const Default = ComponentTemplate.bind({});
31-
Default.args = { apiKey };
32-
Default.parameters = getComponentStoryParams(`const args = ${stringify(Default.args)}`);
32+
Default.args = { apiKey, onSubmit };
33+
Default.parameters = getComponentStoryParams(`const args = ${stringifyWithDefaults(Default.args)}`);
3334

3435
export const RenderSearchSuggestions = ComponentTemplate.bind({});
3536
RenderSearchSuggestions.args = {
3637
apiKey,
38+
onSubmit,
3739
sections: [
3840
{
3941
identifier: 'Search Suggestions',
@@ -42,13 +44,14 @@ RenderSearchSuggestions.args = {
4244
};
4345
addComponentStoryDescription(
4446
RenderSearchSuggestions,
45-
`const args = ${stringify(RenderSearchSuggestions.args)}`,
47+
`const args = ${stringifyWithDefaults(RenderSearchSuggestions.args)}`,
4648
searchSuggestionsDescription
4749
);
4850

4951
export const RenderSuggestedProducts = ComponentTemplate.bind({});
5052
RenderSuggestedProducts.args = {
5153
apiKey,
54+
onSubmit,
5255
sections: [
5356
{
5457
identifier: 'Products',
@@ -57,13 +60,14 @@ RenderSuggestedProducts.args = {
5760
};
5861
addComponentStoryDescription(
5962
RenderSuggestedProducts,
60-
`const args = ${stringify(RenderSuggestedProducts.args)}`,
63+
`const args = ${stringifyWithDefaults(RenderSuggestedProducts.args)}`,
6164
productsDescription
6265
);
6366

6467
export const RenderSuggestedContent = ComponentTemplate.bind({});
6568
RenderSuggestedContent.args = {
6669
apiKey,
70+
onSubmit,
6771
sections: [
6872
{
6973
identifier: 'Content',
@@ -72,13 +76,14 @@ RenderSuggestedContent.args = {
7276
};
7377
addComponentStoryDescription(
7478
RenderSuggestedContent,
75-
`const args = ${stringify(RenderSuggestedContent.args)}`,
79+
`const args = ${stringifyWithDefaults(RenderSuggestedContent.args)}`,
7680
contentDescription
7781
);
7882

7983
export const ConfigureNumberOfResultsPerSection = ComponentTemplate.bind({});
8084
ConfigureNumberOfResultsPerSection.args = {
8185
apiKey,
86+
onSubmit,
8287
sections: [
8388
{
8489
identifier: 'Products',
@@ -88,13 +93,14 @@ ConfigureNumberOfResultsPerSection.args = {
8893
};
8994
addComponentStoryDescription(
9095
ConfigureNumberOfResultsPerSection,
91-
`const args = ${stringify(ConfigureNumberOfResultsPerSection.args)}`,
96+
`const args = ${stringifyWithDefaults(ConfigureNumberOfResultsPerSection.args)}`,
9297
numResultsDescription
9398
);
9499

95100
export const ConfigureOrderOfRenderedSections = ComponentTemplate.bind({});
96101
ConfigureOrderOfRenderedSections.args = {
97102
apiKey,
103+
onSubmit,
98104
sections: [
99105
{
100106
identifier: 'Products',
@@ -106,13 +112,14 @@ ConfigureOrderOfRenderedSections.args = {
106112
};
107113
addComponentStoryDescription(
108114
ConfigureOrderOfRenderedSections,
109-
`const args = ${stringify(ConfigureOrderOfRenderedSections.args)}`,
115+
`const args = ${stringifyWithDefaults(ConfigureOrderOfRenderedSections.args)}`,
110116
sectionOrderDescription
111117
);
112118

113119
export const RenderRecommendations = ComponentTemplate.bind({});
114120
RenderRecommendations.args = {
115121
apiKey,
122+
onSubmit,
116123
sections: [
117124
{
118125
identifier: 'Search Suggestions',
@@ -129,13 +136,14 @@ RenderRecommendations.args = {
129136
};
130137
addComponentStoryDescription(
131138
RenderRecommendations,
132-
`const args = ${stringify(RenderRecommendations.args)}`,
139+
`const args = ${stringifyWithDefaults(RenderRecommendations.args)}`,
133140
recommendationsDescription
134141
);
135142

136143
export const RenderCustomSection = ComponentTemplate.bind({});
137144
RenderCustomSection.args = {
138145
apiKey,
146+
onSubmit,
139147
sections: [
140148
{
141149
identifier: 'Search Suggestions',
@@ -165,6 +173,6 @@ RenderCustomSection.args = {
165173
};
166174
addComponentStoryDescription(
167175
RenderCustomSection,
168-
`const args = ${stringify(RenderCustomSection.args)}`,
176+
`const args = ${stringifyWithDefaults(RenderCustomSection.args)}`,
169177
customSectionDescription
170178
);
Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/* eslint-disable no-console */
22
import { CioAutocomplete } from '../../../index';
33
import { argTypes } from '../argTypes';
4-
import { stringify, disableStoryActions } from '../../../utils';
4+
import { stringifyWithDefaults, disableStoryActions, defaultArgumentsCode } from '../../../utils';
55
import {
66
onChangeDescription,
77
onFocusDescription,
88
onSubmitDescription,
99
userEventsDescription,
1010
apiKey,
11+
onSubmitDefault,
1112
} from '../../../constants';
1213
import { ComponentTemplate, getComponentStoryParams, addComponentStoryDescription } from '.';
1314

@@ -25,20 +26,20 @@ export default {
2526
};
2627

2728
export const Default = ComponentTemplate.bind({});
28-
Default.args = { apiKey };
29-
Default.parameters = getComponentStoryParams(`const args = ${stringify(Default.args)}`);
29+
Default.args = { apiKey, onSubmit: onSubmitDefault };
30+
Default.parameters = getComponentStoryParams(`const args = ${stringifyWithDefaults(Default.args)}`);
3031

3132
const onFocus = () => {
3233
console.log('Focus!');
3334
};
3435
export const OnFocus = ComponentTemplate.bind({});
35-
OnFocus.args = { apiKey, onFocus };
36+
OnFocus.args = { apiKey, onSubmit: onSubmitDefault, onFocus };
3637
addComponentStoryDescription(
3738
OnFocus,
3839
`const args = {
39-
apiKey: '${apiKey}',
40-
onFocus: () => { console.log('Focus!') }
41-
}`,
40+
${defaultArgumentsCode(apiKey)},
41+
"onFocus": () => { console.log("Focus!") }
42+
}`,
4243
onFocusDescription
4344
);
4445
disableStoryActions(OnFocus);
@@ -47,15 +48,15 @@ const onChange = (inputFieldValue) => {
4748
console.log(`New Query: ${inputFieldValue}`);
4849
};
4950
export const OnChange = ComponentTemplate.bind({});
50-
OnChange.args = { apiKey, onChange };
51+
OnChange.args = { apiKey, onSubmit: onSubmitDefault, onChange };
5152
addComponentStoryDescription(
5253
OnChange,
5354
`const args = {
54-
apiKey: '${apiKey}',
55-
onChange: (inputFieldValue) => {
56-
console.log('New Query: ' + inputFieldValue);
57-
}
58-
}`,
55+
${defaultArgumentsCode(apiKey)}
56+
"onChange": (inputFieldValue) => {
57+
console.log("New Query: " + inputFieldValue);
58+
}
59+
}`,
5960
onChangeDescription
6061
);
6162
disableStoryActions(OnChange);
@@ -74,17 +75,17 @@ OnSubmit.args = { apiKey, onSubmit };
7475
addComponentStoryDescription(
7576
OnSubmit,
7677
`const args = {
77-
apiKey: '${apiKey}',
78-
onSubmit: (submitEvent) => {
79-
const { query, item, originalQuery } = submitEvent;
80-
if (query) {
81-
console.log('Submitted query: ' + query);
82-
} else {
83-
console.log('Selected a search suggestion for: ' + originalQuery);
84-
console.dir(item);
85-
}
78+
"apiKey": "${apiKey}",
79+
"onSubmit": (submitEvent) => {
80+
const { query, item, originalQuery } = submitEvent;
81+
if (query) {
82+
console.log("Submitted query: " + query);
83+
} else {
84+
console.log("Selected a search suggestion for: " + originalQuery);
85+
console.dir(item);
8686
}
87-
}`,
87+
}
88+
}`,
8889
onSubmitDescription
8990
);
9091
disableStoryActions(OnSubmit);

src/stories/Autocomplete/Component/ZeroState.stories.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CioAutocomplete } from '../../../index';
22
import { argTypes } from '../argTypes';
3-
import { stringify } from '../../../utils';
3+
import { stringifyWithDefaults } from '../../../utils';
44
import {
55
customSectionDescription,
66
multipleSectionsDescription,
@@ -9,6 +9,7 @@ import {
99
zeroStateDescription,
1010
zeroStateSectionsDescription,
1111
apiKey,
12+
onSubmitDefault as onSubmit,
1213
} from '../../../constants';
1314
import { ComponentTemplate, getComponentStoryParams, addComponentStoryDescription } from '.';
1415

@@ -26,12 +27,13 @@ export default {
2627
};
2728

2829
export const Default = ComponentTemplate.bind({});
29-
Default.args = { apiKey };
30-
Default.parameters = getComponentStoryParams(`const args = ${stringify(Default.args)}`);
30+
Default.args = { apiKey, onSubmit };
31+
Default.parameters = getComponentStoryParams(`const args = ${stringifyWithDefaults(Default.args)}`);
3132

3233
export const RenderSections = ComponentTemplate.bind({});
3334
RenderSections.args = {
3435
apiKey,
36+
onSubmit,
3537
zeroStateSections: [
3638
{
3739
identifier: 'bestsellers',
@@ -42,13 +44,14 @@ RenderSections.args = {
4244
};
4345
addComponentStoryDescription(
4446
RenderSections,
45-
`const args = ${stringify(RenderSections.args)}`,
47+
`const args = ${stringifyWithDefaults(RenderSections.args)}`,
4648
zeroStateSectionsDescription
4749
);
4850

4951
export const NoOpenOnFocus = ComponentTemplate.bind({});
5052
NoOpenOnFocus.args = {
5153
apiKey,
54+
onSubmit,
5255
openOnFocus: false,
5356
zeroStateSections: [
5457
{
@@ -59,13 +62,14 @@ NoOpenOnFocus.args = {
5962
};
6063
addComponentStoryDescription(
6164
NoOpenOnFocus,
62-
`const args = ${stringify(NoOpenOnFocus.args)}`,
65+
`const args = ${stringifyWithDefaults(NoOpenOnFocus.args)}`,
6366
openOnFocusDescription
6467
);
6568

6669
export const RenderRecommendations = ComponentTemplate.bind({});
6770
RenderRecommendations.args = {
6871
apiKey,
72+
onSubmit,
6973
zeroStateSections: [
7074
{
7175
identifier: 'bestsellers',
@@ -75,13 +79,14 @@ RenderRecommendations.args = {
7579
};
7680
addComponentStoryDescription(
7781
RenderRecommendations,
78-
`const args = ${stringify(RenderRecommendations.args)}`,
82+
`const args = ${stringifyWithDefaults(RenderRecommendations.args)}`,
7983
recommendationsDescription
8084
);
8185

8286
export const RenderCustomSection = ComponentTemplate.bind({});
8387
RenderCustomSection.args = {
8488
apiKey,
89+
onSubmit,
8590
zeroStateSections: [
8691
{
8792
identifier: 'recent_searches',
@@ -108,13 +113,14 @@ RenderCustomSection.args = {
108113
};
109114
addComponentStoryDescription(
110115
RenderCustomSection,
111-
`const args = ${stringify(RenderCustomSection.args)}`,
116+
`const args = ${stringifyWithDefaults(RenderCustomSection.args)}`,
112117
customSectionDescription
113118
);
114119

115120
export const RenderMultipleSections = ComponentTemplate.bind({});
116121
RenderMultipleSections.args = {
117122
apiKey,
123+
onSubmit,
118124
zeroStateSections: [
119125
{
120126
identifier: 'bestsellers',
@@ -145,6 +151,6 @@ RenderMultipleSections.args = {
145151
};
146152
addComponentStoryDescription(
147153
RenderMultipleSections,
148-
`const args = ${stringify(RenderMultipleSections.args)}`,
154+
`const args = ${stringifyWithDefaults(RenderMultipleSections.args)}`,
149155
multipleSectionsDescription
150156
);

0 commit comments

Comments
 (0)