Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for passing options to the search component #43

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"release:packages": "pnpm --filter \"./packages/**\" release"
},
"devDependencies": {
"@halo-dev/api-client": "2.17.0",
"@rushstack/eslint-patch": "^1.5.1",
"@types/node": "^18.18.6",
"@typescript-eslint/eslint-plugin": "^6.9.0",
Expand Down
1 change: 0 additions & 1 deletion packages/search-widget/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"prettier": "prettier \"**/*.{cjs,html,js,json,md,ts}\" --ignore-path ./.gitignore --write"
},
"dependencies": {
"@halo-dev/api-client": "2.17.0",
"lit": "^3.1.4",
"lodash-es": "^4.17.21"
},
Expand Down
26 changes: 13 additions & 13 deletions packages/search-widget/src/search-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ import varStyles from './styles/var';

@customElement('search-form')
export class SearchForm extends LitElement {
constructor() {
super();
this.addEventListener('keydown', this.handleKeydown);
}

@property({ type: String })
baseUrl = '';

@property({ type: Object })
options = {};

@state()
private searchResult?: SearchResult;

Expand All @@ -30,6 +28,12 @@ export class SearchForm extends LitElement {

inputRef: Ref<HTMLInputElement> = createRef<HTMLInputElement>();

constructor() {
super();

this.addEventListener('keydown', this.handleKeydown);
}

override render() {
return html`
<div class="search-form__input">
Expand Down Expand Up @@ -117,25 +121,21 @@ export class SearchForm extends LitElement {

fetchHits: DebouncedFunc<(keyword: string) => Promise<void>> = debounce(
async (keyword: string) => {
const options: SearchOption = {
annotations: {},
const searchOptions: SearchOption = {
...this.options,
highlightPostTag: '</mark>',
highlightPreTag: '<mark>',
includeCategoryNames: [],
includeOwnerNames: [],
includeTagNames: [],
includeTypes: ['post'],
keyword,
limit: 20,
};

const response = await fetch(
`/apis/api.halo.run/v1alpha1/indices/-/search?keyword=${keyword}`,
`/apis/api.halo.run/v1alpha1/indices/-/search`,
{
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(options),
body: JSON.stringify(searchOptions),
method: 'post',
}
);
Expand Down
8 changes: 7 additions & 1 deletion packages/search-widget/src/search-modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export class SearchModal extends LitElement {
@property({ type: String })
baseUrl = '';

@property({ type: Object })
options = {};

override render() {
return html`<div
class="modal__wrapper"
Expand All @@ -23,7 +26,10 @@ export class SearchModal extends LitElement {
<div class="modal__layer" @click="${this.close}"></div>
<div class="modal__content">
${this.open
? html`<search-form baseUrl=${this.baseUrl}></search-form>`
? html`<search-form
.baseUrl=${this.baseUrl}
.options=${this.options}
></search-form>`
: ''}
</div>
</div>`;
Expand Down
4 changes: 3 additions & 1 deletion packages/widget/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SearchModal } from '@halo-dev/search-widget';
import '@halo-dev/search-widget/var.css';
import type { SearchOption } from '@halo-dev/api-client';

export { SearchModal };

Expand All @@ -9,6 +10,7 @@ const searchModalElement = document.createElement(

document.body.append(searchModalElement);

export function open() {
export function open(options: SearchOption) {
searchModalElement.options = options;
searchModalElement.open = true;
}
11 changes: 8 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading