Skip to content

Commit 92aeaab

Browse files
committed
feat(foxy-experimental-add-to-cart-builder): autofill last custom option name on arrow up/down
1 parent ee1946f commit 92aeaab

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

src/elements/public/ExperimentalAddToCartBuilder/internal/InternalExperimentalAddToCartBuilderCustomOptionForm/InternalExperimentalAddToCartBuilderCustomOptionForm.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,29 @@ describe('ExperimentalAddToCartBuilder', () => {
115115

116116
it('renders text control for option name inside of the basics group', async () => {
117117
const form = await fixture<Form>(
118-
html`<foxy-internal-experimental-add-to-cart-builder-custom-option-form></foxy-internal-experimental-add-to-cart-builder-custom-option-form>`
118+
html`
119+
<foxy-internal-experimental-add-to-cart-builder-custom-option-form
120+
.existingOptions=${[
121+
{ name: 'foo', value: 'bar' },
122+
{ name: 'baz', value: 'qux' },
123+
{ name: 'quux', value: 'corge' },
124+
]}
125+
>
126+
</foxy-internal-experimental-add-to-cart-builder-custom-option-form>
127+
`
119128
);
120129

121130
const control = form.renderRoot.querySelector(
122131
'[infer="basics-group"] foxy-internal-text-control[infer="name"]'
123132
);
124133

125134
expect(control).to.exist;
135+
136+
control?.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowUp' }));
137+
expect(form.form.name).to.equal('quux');
138+
139+
control?.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' }));
140+
expect(form.form.name).to.equal('foo');
126141
});
127142

128143
it('renders text control for option value inside of the basics group if value is not configurable', async () => {

src/elements/public/ExperimentalAddToCartBuilder/internal/InternalExperimentalAddToCartBuilderCustomOptionForm/InternalExperimentalAddToCartBuilderCustomOptionForm.ts

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ export class InternalExperimentalAddToCartBuilderCustomOptionForm extends Base<D
3232

3333
currencyCode: string | null = null;
3434

35-
get disabledSelector(): BooleanSelector {
36-
const alwaysMatch = [super.disabledSelector.toString()];
37-
38-
if (!this.href && this.existingOptions.some(o => o.name === this.form.name)) {
39-
alwaysMatch.unshift('basics-group:value-configurable');
40-
}
41-
35+
private readonly __nameSetValue = (newValue: string) => {
36+
this.edit({ name: newValue });
37+
if (this.__isAlternative) this.edit({ value_configurable: false, required: false });
38+
};
39+
40+
get readonlySelector(): BooleanSelector {
41+
const alwaysMatch = [super.readonlySelector.toString()];
42+
if (this.__isAlternative) alwaysMatch.unshift('basics-group:value-configurable');
4243
return new BooleanSelector(alwaysMatch.join(' ').trim());
4344
}
4445

@@ -57,7 +58,28 @@ export class InternalExperimentalAddToCartBuilderCustomOptionForm extends Base<D
5758
renderBody(): TemplateResult {
5859
return html`
5960
<foxy-internal-summary-control infer="basics-group">
60-
<foxy-internal-text-control layout="summary-item" infer="name"></foxy-internal-text-control>
61+
<foxy-internal-text-control
62+
layout="summary-item"
63+
infer="name"
64+
.setValue=${this.__nameSetValue}
65+
@keydown=${(evt: KeyboardEvent) => {
66+
if (!['ArrowUp', 'ArrowDown'].includes(evt.key)) return;
67+
evt.preventDefault();
68+
69+
const existingNames = this.existingOptions
70+
.map(option => option.name)
71+
.filter((v, i, a) => a.indexOf(v) === i);
72+
73+
const currentIndex = existingNames.indexOf(this.form.name ?? '');
74+
let nextIndex = evt.key === 'ArrowUp' ? currentIndex - 1 : currentIndex + 1;
75+
if (nextIndex < 0) nextIndex = existingNames.length - 1;
76+
if (nextIndex >= existingNames.length) nextIndex = 0;
77+
78+
const nextName = existingNames[nextIndex];
79+
if (nextName && nextName !== this.form.name) this.edit({ name: nextName });
80+
}}
81+
>
82+
</foxy-internal-text-control>
6183
6284
<foxy-internal-text-control
6385
property="value"
@@ -121,6 +143,8 @@ export class InternalExperimentalAddToCartBuilderCustomOptionForm extends Base<D
121143
@update=${() => this.requestUpdate()}
122144
>
123145
</foxy-nucleon>
146+
147+
${super.renderBody()}
124148
`;
125149
}
126150

@@ -143,4 +167,9 @@ export class InternalExperimentalAddToCartBuilderCustomOptionForm extends Base<D
143167
const loader = this.renderRoot.querySelector<Loader>('#itemCategoryLoader');
144168
return loader?.data?.default_weight_unit ?? this.defaultWeightUnit;
145169
}
170+
171+
private get __isAlternative() {
172+
const existing = this.existingOptions.filter(o => o.name === this.form.name);
173+
return this.href ? existing.length > 1 : existing.length > 0;
174+
}
146175
}

0 commit comments

Comments
 (0)