Skip to content

Commit 9fc8ccf

Browse files
committed
feat(foxy-filter-attribute-form): add support for filter query defaults
1 parent e5d886c commit 9fc8ccf

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/elements/public/FilterAttributeForm/FilterAttributeForm.stories.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ const summary: Summary = {
1919

2020
export default getMeta(summary);
2121

22-
export const Playground = getStory({ ...summary, code: true });
23-
export const Empty = getStory(summary);
24-
export const Error = getStory(summary);
25-
export const Busy = getStory(summary);
22+
const ext = `defaults="size=large&color=blue" pathname="/stores/0/transactions"`;
23+
24+
export const Playground = getStory({ ...summary, ext, code: true });
25+
export const Empty = getStory({ ...summary, ext });
26+
export const Error = getStory({ ...summary, ext });
27+
export const Busy = getStory({ ...summary, ext });
2628

2729
Empty.args.href = '';
2830
Error.args.href = 'https://demo.api/virtual/empty?status=404';

src/elements/public/FilterAttributeForm/FilterAttributeForm.test.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ describe('FilterAttributeForm', () => {
6666
expect(new Form()).to.have.property('ns', 'filter-attribute-form');
6767
});
6868

69+
it('has a reactive property "defaults"', () => {
70+
expect(Form).to.have.deep.nested.property('properties.defaults', {});
71+
expect(new Form()).to.have.property('defaults', null);
72+
});
73+
6974
it('has a reactive property "pathname"', async () => {
7075
expect(Form).to.have.deep.nested.property('properties.pathname', {});
7176

@@ -85,12 +90,15 @@ describe('FilterAttributeForm', () => {
8590
});
8691

8792
it('renders query builder', async () => {
88-
const layout = html`<foxy-filter-attribute-form pathname="/foo"></foxy-filter-attribute-form>`;
89-
const element = await fixture<Form>(layout);
90-
const control = element.renderRoot.querySelector<QueryBuilder>('[infer="filter-query"]')!;
93+
const element = await fixture<Form>(html`
94+
<foxy-filter-attribute-form pathname="/foo" defaults="color=blue">
95+
</foxy-filter-attribute-form>
96+
`);
9197

98+
const control = element.renderRoot.querySelector<QueryBuilder>('[infer="filter-query"]')!;
9299
expect(control).to.exist;
93100
expect(control).to.be.instanceOf(customElements.get('foxy-query-builder'));
101+
expect(control).to.have.property('value', 'color=blue');
94102

95103
const options: Form['options'] = [];
96104
element.options = options;

src/elements/public/FilterAttributeForm/FilterAttributeForm.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ export class FilterAttributeForm extends Base<Data> {
3434
static get properties(): PropertyDeclarations {
3535
return {
3636
...super.properties,
37+
defaults: {},
3738
pathname: {},
3839
options: { type: Array },
3940
};
4041
}
4142

43+
/** Default filter query. */
44+
defaults: string | null = null;
45+
4246
/** Admin page pathname. */
4347
pathname: string | null = null;
4448

@@ -115,8 +119,26 @@ export class FilterAttributeForm extends Base<Data> {
115119

116120
private __getValueParam(key: string) {
117121
try {
122+
const constructor = this.constructor as typeof FilterAttributeForm;
118123
const url = new URL(decode(this.form.value ?? ''), 'https://example.com');
119-
return url.searchParams.get(key) ?? '';
124+
125+
let result = url.searchParams.get(key) ?? '';
126+
127+
if (
128+
key === constructor.filterQueryKey &&
129+
(this.in({ idle: { snapshot: 'clean' } }) || this.in({ idle: { template: 'clean' } }))
130+
) {
131+
try {
132+
const fullQuery = new URLSearchParams(result);
133+
const defaults = new URLSearchParams(this.defaults ?? '');
134+
fullQuery.forEach((v, k) => defaults.set(k, v));
135+
result = defaults.toString();
136+
} catch {
137+
// no-op
138+
}
139+
}
140+
141+
return result;
120142
} catch {
121143
return '';
122144
}

0 commit comments

Comments
 (0)