Skip to content

Commit

Permalink
fix(ts): make template types consistent (algolia#4785)
Browse files Browse the repository at this point in the history
* fix(ts): make templates types consistent

* fix: `Answers` type naming

* fix: remove `ComponentTemplates` type utils

* fix: remove unneeded comment

* fix: inline `searchBoxTemplateProps` templates

* fix: remove unneeded casts

* fix: move internal type to widget file

* rename template component type to `RefinementListComponentTemplates`

* fix: make `searchableNoResults` optional

* fix: remove unneeded default template

* fix: type `defaultTemplates`

* fix: apply changes from suggestion

Co-authored-by: Haroen Viaene <[email protected]>
  • Loading branch information
shortcuts and Haroenv authored Jul 5, 2021
1 parent 2f5af95 commit e0fbd55
Show file tree
Hide file tree
Showing 56 changed files with 431 additions and 267 deletions.
8 changes: 5 additions & 3 deletions src/components/Answers/Answers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import {
} from '../../widgets/answers/answers';
import { ComponentCSSClasses, Hits } from '../../types';

export type AnswerComponentCSSClasses = ComponentCSSClasses<AnswersCSSClasses>;
export type AnswersComponentCSSClasses = ComponentCSSClasses<AnswersCSSClasses>;

export type AnswersComponentTemplates = Required<AnswersTemplates>;

export type AnswersProps = {
hits: Hits;
isLoading: boolean;
cssClasses: AnswerComponentCSSClasses;
cssClasses: AnswersComponentCSSClasses;
templateProps: {
[key: string]: any;
templates: AnswersTemplates;
templates: AnswersComponentTemplates;
};
};

Expand Down
12 changes: 6 additions & 6 deletions src/components/Breadcrumb/Breadcrumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import { h } from 'preact';
import cx from 'classnames';
import Template from '../Template/Template';
import { BreadcrumbCSSClasses } from '../../widgets/breadcrumb/breadcrumb';
import {
BreadcrumbCSSClasses,
BreadcrumbTemplates,
} from '../../widgets/breadcrumb/breadcrumb';
import { ComponentCSSClasses } from '../../types';

type BreadcrumbItem = {
Expand All @@ -15,16 +18,13 @@ export type BreadcrumbComponentCSSClasses = ComponentCSSClasses<
BreadcrumbCSSClasses
>;

type BreadcrumbTemplates = {
home: string;
separator: string;
};
export type BreadcrumbComponentTemplates = Required<BreadcrumbTemplates>;

export type BreadcrumbProps = {
items: BreadcrumbItem[];
cssClasses: BreadcrumbComponentCSSClasses;
templateProps: {
templates: BreadcrumbTemplates;
templates: BreadcrumbComponentTemplates;
};
createURL(value: string | undefined): string;
refine(value: string | undefined): void;
Expand Down
6 changes: 5 additions & 1 deletion src/components/ClearRefinements/ClearRefinements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ export type ClearRefinementsComponentCSSClasses = ComponentCSSClasses<
ClearRefinementsCSSClasses
>;

export type ClearRefinementsComponentTemplates = Required<
ClearRefinementsTemplates
>;

export type ClearRefinementsProps = {
refine: ClearRefinementsRenderState['refine'];
cssClasses: ClearRefinementsComponentCSSClasses;
hasRefinements: ClearRefinementsRenderState['hasRefinements'];
templateProps: {
[key: string]: any;
templates: ClearRefinementsTemplates;
templates: ClearRefinementsComponentTemplates;
};
};

Expand Down
4 changes: 3 additions & 1 deletion src/components/Hits/Hits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { HitsCSSClasses, HitsTemplates } from '../../widgets/hits/hits';

export type HitsComponentCSSClasses = ComponentCSSClasses<HitsCSSClasses>;

export type HitsComponentTemplates = Required<HitsTemplates>;

export type HitsProps = {
results: SearchResults;
hits: HitsArray;
Expand All @@ -18,7 +20,7 @@ export type HitsProps = {
cssClasses: HitsComponentCSSClasses;
templateProps: {
[key: string]: any;
templates: HitsTemplates;
templates: HitsComponentTemplates;
};
};

Expand Down
4 changes: 3 additions & 1 deletion src/components/Hits/__tests__/Hits-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Hits', () => {
function shallowRender(extraProps = {}) {
const props = {
cssClasses,
templateProps: { templates: {} },
templateProps: { templates: { empty: 'No results', item: 'item' } },
...extraProps,
};

Expand Down Expand Up @@ -244,6 +244,7 @@ describe('Hits', () => {
templateProps: {
templates: {
item: 'item',
empty: 'No results',
},
},
cssClasses,
Expand Down Expand Up @@ -295,6 +296,7 @@ describe('Hits', () => {
hit,
});
},
empty: 'No results',
},
},
cssClasses,
Expand Down
4 changes: 3 additions & 1 deletion src/components/InfiniteHits/InfiniteHits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export type InfiniteHitsComponentCSSClasses = ComponentCSSClasses<
InfiniteHitsCSSClasses
>;

export type InfiniteHitsComponentTemplates = Required<InfiniteHitsTemplates>;

export type InfiniteHitsProps = {
cssClasses: InfiniteHitsComponentCSSClasses;
hits: Hits;
Expand All @@ -24,7 +26,7 @@ export type InfiniteHitsProps = {
showMore: () => void;
templateProps: {
[key: string]: any;
templates: InfiniteHitsTemplates;
templates: InfiniteHitsComponentTemplates;
};
isFirstPage: boolean;
isLastPage: boolean;
Expand Down
4 changes: 3 additions & 1 deletion src/components/MenuSelect/MenuSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export type MenuSelectComponentCSSClasses = ComponentCSSClasses<
MenuSelectCSSClasses
>;

export type MenuSelectComponentTemplates = Required<MenuSelectTemplates>;

type MenuItem = {
/**
* The value of the menu item.
Expand All @@ -39,7 +41,7 @@ type Props = {
items: MenuItem[];
refine: MenuRenderState['refine'];
templateProps: {
templates: MenuSelectTemplates;
templates: MenuSelectComponentTemplates;
};
};

Expand Down
4 changes: 3 additions & 1 deletion src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ export type PaginationComponentCSSClasses = ComponentCSSClasses<
PaginationCSSClasses
>;

export type PaginationComponentTemplates = Required<PaginationTemplates>;

export type PaginationProps = {
createURL(value: number): string;
cssClasses: PaginationComponentCSSClasses;
currentPage: number;
templates: PaginationTemplates;
templates: PaginationComponentTemplates;
nbPages?: number;
pages?: number[];
isFirstPage: boolean;
Expand Down
6 changes: 5 additions & 1 deletion src/components/Panel/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ export type PanelComponentCSSClasses = ComponentCSSClasses<
Omit<PanelCSSClasses, 'collapseIcon'>
>;

export type PanelComponentTemplates<
TWidget extends UnknownWidgetFactory
> = Required<PanelTemplates<TWidget>>;

export type PanelProps<TWidget extends UnknownWidgetFactory> = {
hidden: boolean;
collapsible: boolean;
isCollapsed: boolean;
data: RenderOptions;
cssClasses: PanelComponentCSSClasses;
templates: Required<PanelTemplates<TWidget>>;
templates: PanelComponentTemplates<TWidget>;
bodyElement: HTMLElement;
};

Expand Down
6 changes: 5 additions & 1 deletion src/components/QueryRuleCustomData/QueryRuleCustomData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ export type QueryRuleCustomDataComponentCSSClasses = ComponentCSSClasses<
QueryRuleCustomDataCSSClasses
>;

export type QueryRuleCustomDataComponentTemplates = Required<
QueryRuleCustomDataTemplates
>;

export type QueryRuleCustomDataProps = {
cssClasses: QueryRuleCustomDataComponentCSSClasses;
templates: QueryRuleCustomDataTemplates;
templates: QueryRuleCustomDataComponentTemplates;
items: any[];
};

Expand Down
4 changes: 3 additions & 1 deletion src/components/RangeInput/RangeInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ export type RangeInputComponentCSSClasses = ComponentCSSClasses<
RangeInputCSSClasses
>;

export type RangeInputComponentTemplates = Required<RangeInputTemplates>;

export type RangeInputProps = {
min?: number;
max?: number;
step: number;
values: Partial<Range>;
cssClasses: RangeInputComponentCSSClasses;
templateProps: {
templates: RangeInputTemplates;
templates: RangeInputComponentTemplates;
};
refine(rangeValue: RangeBoundaries): void;
};
Expand Down
45 changes: 29 additions & 16 deletions src/components/RefinementList/RefinementList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@ import Template from '../Template/Template';
import RefinementListItem from './RefinementListItem';
import SearchBox, {
SearchBoxComponentCSSClasses,
SearchBoxComponentTemplates,
} from '../SearchBox/SearchBox';
import { RefinementListItem as TRefinementListItem } from '../../connectors/refinement-list/connectRefinementList';
import { HierarchicalMenuItem } from '../../connectors/hierarchical-menu/connectHierarchicalMenu';
import { SearchBoxTemplates } from '../../widgets/search-box/search-box';
import { ComponentCSSClasses, CreateURL, Templates } from '../../types';
import { RefinementListOwnCSSClasses } from '../../widgets/refinement-list/refinement-list';
import {
ComponentCSSClasses,
CreateURL,
Templates,
Template as TemplateType,
} from '../../types';
import {
RefinementListOwnCSSClasses,
RefinementListOwnTemplates,
} from '../../widgets/refinement-list/refinement-list';
import { RatingMenuComponentCSSClasses } from '../../widgets/rating-menu/rating-menu';
import { HierarchicalMenuComponentCSSClasses } from '../../widgets/hierarchical-menu/hierarchical-menu';

// CSS types
type RefinementListOptionalClasses =
| 'noResults'
| 'checkbox'
Expand All @@ -29,19 +38,25 @@ type RefinementListWidgetCSSClasses = ComponentCSSClasses<
RefinementListOwnCSSClasses
>;

type RefinementListRequired = Omit<
type RefinementListRequiredCSSClasses = Omit<
RefinementListWidgetCSSClasses,
RefinementListOptionalClasses
> &
Partial<Pick<RefinementListWidgetCSSClasses, RefinementListOptionalClasses>>;

export type RefinementListComponentCSSClasses = RefinementListRequired & {
export type RefinementListComponentCSSClasses = RefinementListRequiredCSSClasses & {
searchable?: SearchBoxComponentCSSClasses;
} & Partial<Pick<RatingMenuComponentCSSClasses, 'disabledItem'>> &
Partial<
Pick<HierarchicalMenuComponentCSSClasses, 'childList' | 'parentItem'>
>;

export type RefinementListComponentTemplates = Required<
RefinementListOwnTemplates
> & {
searchableNoResults?: TemplateType<void>;
};

type FacetValue = TRefinementListItem | HierarchicalMenuItem;
type FacetValues = TRefinementListItem[] | HierarchicalMenuItem[];

Expand All @@ -51,20 +66,22 @@ export type RefinementListProps<TTemplates extends Templates> = {
depth?: number;
facetValues?: FacetValues;
attribute?: string;
templateProps?: PreparedTemplateProps<TTemplates>;
searchBoxTemplateProps?: PreparedTemplateProps<SearchBoxTemplates>;
templateProps: PreparedTemplateProps<TTemplates>;
toggleRefinement: (value: string) => void;
searchFacetValues?: (query: string) => void;
searchPlaceholder?: string;
isFromSearch?: boolean;
showMore?: boolean;
toggleShowMore?: () => void;
isShowingMore?: boolean;
hasExhaustiveItems?: boolean;
canToggleShowMore?: boolean;
searchIsAlwaysActive?: boolean;
className?: string;
children?: h.JSX.Element;

// searchable props are optional, but will definitely be present in a searchable context
isFromSearch?: boolean;
searchIsAlwaysActive?: boolean;
searchFacetValues?: (query: string) => void;
searchPlaceholder?: string;
searchBoxTemplateProps?: PreparedTemplateProps<SearchBoxComponentTemplates>;
};

const defaultProps = {
Expand Down Expand Up @@ -302,18 +319,14 @@ class RefinementList<TTemplates extends Templates> extends Component<
this.props.searchIsAlwaysActive !== true &&
!(this.props.isFromSearch || !this.props.hasExhaustiveItems);

const templates = this.props.searchBoxTemplateProps
? this.props.searchBoxTemplateProps.templates
: undefined;

const searchBox = this.props.searchFacetValues && (
<div className={this.props.cssClasses.searchBox}>
<SearchBox
ref={this.searchBox}
placeholder={this.props.searchPlaceholder}
disabled={shouldDisableSearchBox}
cssClasses={this.props.cssClasses.searchable!}
templates={templates}
templates={this.props.searchBoxTemplateProps!.templates}
onChange={(event: Event) =>
this.props.searchFacetValues!(
(event.target as HTMLInputElement).value
Expand Down
10 changes: 2 additions & 8 deletions src/components/RefinementList/__tests__/RefinementList-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,7 @@ describe('RefinementList', () => {
canToggleShowMore: true,
templateProps: {
templatesConfig: {},
templates: {
item: (item: RefinementListItemData) => item.value,
showMoreText: (x: any) => JSON.stringify(x),
},
templates,
useCustomCompileOptions: {},
},
toggleRefinement: () => {},
Expand Down Expand Up @@ -588,10 +585,7 @@ describe('RefinementList', () => {
canToggleShowMore: false,
templateProps: {
templatesConfig: {},
templates: {
item: (item: RefinementListItemData) => item.value,
showMoreText: (x: any) => JSON.stringify(x),
},
templates,
useCustomCompileOptions: {},
},
toggleRefinement: () => {},
Expand Down
4 changes: 3 additions & 1 deletion src/components/RelevantSort/RelevantSort.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ export type RelevantSortComponentCSSClasses = ComponentCSSClasses<
RelevantSortCSSClasses
>;

export type RelevantSortComponentTemplates = Required<RelevantSortTemplates>;

type RelevantSortProps = {
cssClasses: RelevantSortComponentCSSClasses;
templates: RelevantSortTemplates;
templates: RelevantSortComponentTemplates;
isRelevantSorted: boolean;
isVirtualReplica: boolean;
refine(relevancyStrictness: number | undefined): void;
Expand Down
4 changes: 3 additions & 1 deletion src/components/SearchBox/SearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ export type SearchBoxComponentCSSClasses = ComponentCSSClasses<
SearchBoxCSSClasses
>;

export type SearchBoxComponentTemplates = Required<SearchBoxTemplates>;

type SearchBoxProps = {
placeholder?: string;
cssClasses: SearchBoxComponentCSSClasses;
templates?: SearchBoxTemplates;
templates: SearchBoxComponentTemplates;
query?: string;
showSubmit?: boolean;
showReset?: boolean;
Expand Down
4 changes: 3 additions & 1 deletion src/components/Stats/Stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import { ComponentCSSClasses } from '../../types';

export type StatsComponentCSSClasses = ComponentCSSClasses<StatsCSSClasses>;

export type StatsComponentTemplates = Required<StatsTemplates>;

type StatsProps = {
cssClasses: StatsComponentCSSClasses;
templateProps: {
[key: string]: any;
templates: StatsTemplates;
templates: StatsComponentTemplates;
};
hitsPerPage: number | undefined;
nbHits: number;
Expand Down
Loading

0 comments on commit e0fbd55

Please sign in to comment.