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

poc: common JSX components #5952

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ module.exports = (api) => {
// false positive (babel doesn't know types)
// this is actually only called on arrays
'String.prototype.includes',

// just for the PoC
'Object.assign',
];
if (defaultShouldInject && !exclude.includes(name)) {
throw new Error(
Expand Down
51 changes: 51 additions & 0 deletions packages/instantsearch-jsx/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "instantsearch-jsx",
"version": "1.0.0",
"description": "Common JSX components for InstantSearch flavors",
"types": "dist/es/index.d.ts",
"main": "dist/cjs/index.js",
"module": "dist/es/index.js",
"type": "module",
"exports": {
".": {
"import": "./dist/es/index.js",
"require": "./dist/cjs/index.js"
}
},
"sideEffects": false,
"license": "MIT",
"homepage": "https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/react/",
"repository": {
"type": "git",
"url": "https://github.com/algolia/instantsearch"
},
"author": {
"name": "Algolia, Inc.",
"url": "https://www.algolia.com"
},
"keywords": [
"algolia",
"components",
"fast",
"instantsearch",
"react",
"search"
],
"files": [
"README.md",
"dist"
],
"scripts": {
"clean": "rm -rf dist",
"watch": "yarn build:cjs --watch",
"build": "yarn build:cjs && yarn build:es && yarn build:types",
"build:cjs": "BABEL_ENV=cjs babel src --root-mode upward --extensions '.js,.ts,.tsx' --out-dir dist/cjs --ignore '**/__tests__/**/*','**/__mocks__/**/*' --quiet && ../../scripts/prepare-cjs.sh",
"build:es": "BABEL_ENV=es babel src --root-mode upward --extensions '.js,.ts,.tsx' --out-dir dist/es --ignore '**/__tests__/**/*','**/__mocks__/**/*' --quiet",
"build:types": "tsc -p ./tsconfig.declaration.json --outDir ./dist/es",
"test:exports": "node ./test/module/is-es-module.mjs && node ./test/module/is-cjs-module.cjs",
"version": "./scripts/version.cjs"
},
"dependencies": {
"@babel/runtime": "^7.1.2"
}
}
100 changes: 100 additions & 0 deletions packages/instantsearch-jsx/src/Hits.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/** @jsx createElement */

export type HitsClassNames = {
/**
* Class names to apply to the root element
*/
root: string;
/**
* Class names to apply to the root element without results
*/
emptyRoot: string;
/**
* Class names to apply to the list element
*/
list: string;
/**
* Class names to apply to each item element
*/
item: string;
};

type BaseHit = {
objectID: string;
};

export type HitsProps<T extends BaseHit> = {
hitComponent?: (props: {
hit: T;
item: T;
sendEvent: (eventName: string, hit: T, event: string) => void;
}) => JSX.Element;
itemComponent?: (props: { hit: T; index: number }) => JSX.Element;
hits: T[];
className?: string;
classNames?: Partial<HitsClassNames>;
sendEvent: (eventName: string, hit: T, event: string) => void;
};

export function cx(
...classNames: Array<string | number | boolean | undefined | null>
) {
return classNames.filter(Boolean).join(' ');
}

export function createHits({ createElement }: any) {
function DefaultHitComponent({ hit }: { hit: BaseHit }) {
return (
<div style={{ wordBreak: 'break-all' }}>
{JSON.stringify(hit).slice(0, 100)}…
</div>
);
}

return function Hits<T extends BaseHit>({
hitComponent: HitComponent,
itemComponent: ItemComponent,
classNames = {},
hits,
sendEvent,
...props
}: HitsProps<T>) {
return (
<div
{...props}
className={cx(
'ais-Hits',
classNames.root,
hits.length === 0 && cx('ais-Hits--empty', classNames.emptyRoot),
props.className
)}
>
<ol className={cx('ais-Hits-list', classNames.list)}>
{hits.map((hit, index) =>
ItemComponent ? (
<ItemComponent hit={hit} index={index} />
) : (
<li
key={hit.objectID}
className={cx('ais-Hits-item', classNames.item)}
onClick={() => {
sendEvent('click:internal', hit, 'Hit Clicked');
}}
onAuxClick={() => {
sendEvent('click:internal', hit, 'Hit Clicked');
}}
>
{HitComponent ? (
// Vue uses `item` and React uses `hit`
<HitComponent item={hit} hit={hit} sendEvent={sendEvent} />
) : (
<DefaultHitComponent hit={hit} />
)}
</li>
)
)}
</ol>
</div>
);
};
}
1 change: 1 addition & 0 deletions packages/instantsearch-jsx/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Hits';
3 changes: 3 additions & 0 deletions packages/instantsearch-jsx/tsconfig.declaration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.declaration"
}
1 change: 1 addition & 0 deletions packages/instantsearch.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@types/hogan.js": "^3.0.0",
"@types/qs": "^6.5.3",
"algoliasearch-helper": "3.15.0",
"instantsearch-jsx": "1.0.0",
"hogan.js": "^3.0.2",
"htm": "^3.0.0",
"preact": "^10.10.0",
Expand Down
76 changes: 40 additions & 36 deletions packages/instantsearch.js/src/components/Hits/Hits.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @jsx h */

import { cx } from '@algolia/ui-components-shared';
import { createHits } from 'instantsearch-jsx';
import { h } from 'preact';

import { createInsightsEventHandler } from '../../lib/insights/listener';
Expand All @@ -26,13 +27,15 @@ export type HitsProps = {
templateProps: PreparedTemplateProps<HitsComponentTemplates>;
};

const UiHits = createHits({ createElement: h });

export default function Hits({
results,
hits,
insights,
bindEvent,
sendEvent,
cssClasses,
bindEvent,
templateProps,
}: HitsProps) {
const handleInsightsClick = createInsightsEventHandler({
Expand All @@ -55,40 +58,41 @@ export default function Hits({
}

return (
<div className={cssClasses.root}>
<ol className={cssClasses.list}>
{hits.map((hit, index) => (
<Template
{...templateProps}
templateKey="item"
rootTagName="li"
rootProps={{
className: cssClasses.item,
onClick: (event: MouseEvent) => {
handleInsightsClick(event);
sendEvent('click:internal', hit, 'Hit Clicked');
},
onAuxClick: (event: MouseEvent) => {
handleInsightsClick(event);
sendEvent('click:internal', hit, 'Hit Clicked');
},
}}
key={hit.objectID}
data={{
...hit,
get __hitIndex() {
warning(
false,
'The `__hitIndex` property is deprecated. Use the absolute `__position` instead.'
);
return index;
},
}}
bindEvent={bindEvent}
sendEvent={sendEvent}
/>
))}
</ol>
</div>
<UiHits
hits={hits}
sendEvent={sendEvent}
classNames={cssClasses}
itemComponent={({ hit, index }) => (
<Template
{...templateProps}
templateKey="item"
rootTagName="li"
rootProps={{
className: cssClasses.item,
onClick: (event: MouseEvent) => {
handleInsightsClick(event);
sendEvent('click:internal', hit, 'Hit Clicked');
},
onAuxClick: (event: MouseEvent) => {
handleInsightsClick(event);
sendEvent('click:internal', hit, 'Hit Clicked');
},
}}
key={hit.objectID}
data={{
...hit,
get __hitIndex() {
warning(
false,
'The `__hitIndex` property is deprecated. Use the absolute `__position` instead.'
);
return index;
},
}}
bindEvent={bindEvent}
sendEvent={sendEvent}
/>
)}
/>
);
}
1 change: 1 addition & 0 deletions packages/react-instantsearch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"dependencies": {
"@babel/runtime": "^7.1.2",
"instantsearch.js": "4.61.0",
"instantsearch-jsx": "1.0.0",
"react-instantsearch-core": "7.4.0"
},
"peerDependencies": {
Expand Down
84 changes: 5 additions & 79 deletions packages/react-instantsearch/src/ui/Hits.tsx
Original file line number Diff line number Diff line change
@@ -1,80 +1,6 @@
import React from 'react';
import { createHits } from 'instantsearch-jsx';
import { createElement } from 'react';

import { cx } from './lib/cx';

import type { Hit } from 'instantsearch.js';
import type { SendEventForHits } from 'instantsearch.js/es/lib/utils';

export type HitsProps<THit> = React.ComponentProps<'div'> & {
hits: THit[];
sendEvent: SendEventForHits;
hitComponent?: React.JSXElementConstructor<{
hit: THit;
sendEvent: SendEventForHits;
}>;
classNames?: Partial<HitsClassNames>;
};

function DefaultHitComponent({ hit }: { hit: Hit }) {
return (
<div style={{ wordBreak: 'break-all' }}>
{JSON.stringify(hit).slice(0, 100)}…
</div>
);
}

export type HitsClassNames = {
/**
* Class names to apply to the root element
*/
root: string;
/**
* Class names to apply to the root element without results
*/
emptyRoot: string;
/**
* Class names to apply to the list element
*/
list: string;
/**
* Class names to apply to each item element
*/
item: string;
};

export function Hits<THit extends Hit>({
hits,
sendEvent,
hitComponent: HitComponent = DefaultHitComponent,
classNames = {},
...props
}: HitsProps<THit>) {
return (
<div
{...props}
className={cx(
'ais-Hits',
classNames.root,
hits.length === 0 && cx('ais-Hits--empty', classNames.emptyRoot),
props.className
)}
>
<ol className={cx('ais-Hits-list', classNames.list)}>
{hits.map((hit) => (
<li
key={hit.objectID}
className={cx('ais-Hits-item', classNames.item)}
onClick={() => {
sendEvent('click:internal', hit, 'Hit Clicked');
}}
onAuxClick={() => {
sendEvent('click:internal', hit, 'Hit Clicked');
}}
>
<HitComponent hit={hit} sendEvent={sendEvent} />
</li>
))}
</ol>
</div>
);
}
export const Hits = createHits({
createElement,
});
2 changes: 1 addition & 1 deletion packages/react-instantsearch/src/widgets/Hits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useHits } from 'react-instantsearch-core';

import { Hits as HitsUiComponent } from '../ui/Hits';

import type { HitsProps as HitsUiComponentProps } from '../ui/Hits';
import type { HitsProps as HitsUiComponentProps } from 'instantsearch-jsx';
import type { Hit, BaseHit } from 'instantsearch.js';
import type { UseHitsProps } from 'react-instantsearch-core';

Expand Down
1 change: 1 addition & 0 deletions packages/vue-instantsearch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
},
"dependencies": {
"instantsearch.js": "4.61.0",
"instantsearch-jsx": "1.0.0",
"mitt": "^2.1.0"
},
"peerDependencies": {
Expand Down