Skip to content

Commit

Permalink
chore: refactor FilmSelect into new private dev package (#5632)
Browse files Browse the repository at this point in the history
  • Loading branch information
adidahiya authored Sep 30, 2022
1 parent 9a9cc2c commit b0d5005
Show file tree
Hide file tree
Showing 28 changed files with 1,173 additions and 1,014 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lerna": "4.0.0",
"lerna": "5.5.4",
"npmClient": "yarn",
"useWorkspaces": true,
"version": "independent"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"eslint-plugin-prettier": "^4.2.1",
"gh-pages": "^4.0.0",
"http-server": "^14.1.1",
"lerna": "^5.1.8",
"lerna": "^5.5.4",
"npm-run-all": "^4.1.5",
"prettier": "~2.7.1",
"sinon": "^9.2.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ import {
Popover2SharedProps,
StrictModifierNames,
} from "@blueprintjs/popover2";

import FilmSelect from "../../common/filmSelect";
import { FilmSelect } from "@blueprintjs/select/examples";

const POPPER_DOCS_URL = "https://popper.js.org/docs/v2/";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,34 @@ import { Code, H5, Intent, MenuItem, Switch, TagProps } from "@blueprintjs/core"
import { Example, ExampleProps } from "@blueprintjs/docs-theme";
import { Popover2 } from "@blueprintjs/popover2";
import { ItemRenderer, MultiSelect2 } from "@blueprintjs/select";

import {
areFilmsEqual,
arrayContainsFilm,
createFilms,
Film,
filterFilm,
getFilmItemProps,
IFilm,
maybeAddCreatedFilmToArrays,
maybeDeleteCreatedFilmFromArrays,
renderCreateFilmsMenuItem,
TOP_100_FILMS,
} from "../../common/films";
} from "@blueprintjs/select/examples";

import { PropCodeTooltip } from "../../common/propCodeTooltip";

const FilmMultiSelect = MultiSelect2.ofType<IFilm>();
const FilmMultiSelect = MultiSelect2.ofType<Film>();

const INTENTS = [Intent.NONE, Intent.PRIMARY, Intent.SUCCESS, Intent.DANGER, Intent.WARNING];

export interface IMultiSelectExampleState {
allowCreate: boolean;
createdItems: IFilm[];
createdItems: Film[];
disabled: boolean;
fill: boolean;
films: IFilm[];
films: Film[];
hasInitialContent: boolean;
intent: boolean;
items: IFilm[];
items: Film[];
matchTargetWidth: boolean;
openOnKeyDown: boolean;
popoverMinimal: boolean;
Expand Down Expand Up @@ -225,9 +225,9 @@ export class MultiSelectExample extends React.PureComponent<ExampleProps, IMulti
);
}

private renderTag = (film: IFilm) => film.title;
private renderTag = (film: Film) => film.title;

private renderFilm: ItemRenderer<IFilm> = (film, props) => {
private renderFilm: ItemRenderer<Film> = (film, props) => {
if (!props.modifiers.matchesPredicate) {
return null;
}
Expand All @@ -246,19 +246,19 @@ export class MultiSelectExample extends React.PureComponent<ExampleProps, IMulti
this.deselectFilm(index);
};

private getSelectedFilmIndex(film: IFilm) {
private getSelectedFilmIndex(film: Film) {
return this.state.films.indexOf(film);
}

private isFilmSelected(film: IFilm) {
private isFilmSelected(film: Film) {
return this.getSelectedFilmIndex(film) !== -1;
}

private selectFilm(film: IFilm) {
private selectFilm(film: Film) {
this.selectFilms([film]);
}

private selectFilms(filmsToSelect: IFilm[]) {
private selectFilms(filmsToSelect: Film[]) {
this.setState(({ createdItems, films, items }) => {
let nextCreatedItems = createdItems.slice();
let nextFilms = films.slice();
Expand Down Expand Up @@ -299,15 +299,15 @@ export class MultiSelectExample extends React.PureComponent<ExampleProps, IMulti
});
}

private handleFilmSelect = (film: IFilm) => {
private handleFilmSelect = (film: Film) => {
if (!this.isFilmSelected(film)) {
this.selectFilm(film);
} else {
this.deselectFilm(this.getSelectedFilmIndex(film));
}
};

private handleFilmsPaste = (films: IFilm[]) => {
private handleFilmsPaste = (films: Film[]) => {
// On paste, don't bother with deselecting already selected values, just
// add the new ones.
this.selectFilms(films);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@ import * as React from "react";
import { Button, H5, HotkeysTarget2, KeyCombo, MenuItem, Position, Switch, Toaster } from "@blueprintjs/core";
import { Example, ExampleProps, handleBooleanChange } from "@blueprintjs/docs-theme";
import { Omnibar } from "@blueprintjs/select";

import {
areFilmsEqual,
createFilm,
Film,
filterFilm,
IFilm,
renderCreateFilmMenuItem,
renderFilm,
TOP_100_FILMS,
} from "./../../common/films";
} from "@blueprintjs/select/examples";

const FilmOmnibar = Omnibar.ofType<IFilm>();
const FilmOmnibar = Omnibar.ofType<Film>();

export interface IOmnibarExampleState {
allowCreate: boolean;
Expand Down Expand Up @@ -115,7 +114,7 @@ export class OmnibarExample extends React.PureComponent<ExampleProps, IOmnibarEx
this.setState({ isOpen: true });
};

private handleItemSelect = (film: IFilm) => {
private handleItemSelect = (film: Film) => {
this.setState({ isOpen: false });

this.toaster.show({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ import * as React from "react";

import { H5, MenuItem, Switch } from "@blueprintjs/core";
import { Example, ExampleProps } from "@blueprintjs/docs-theme";

import { IFilm, TOP_100_FILMS } from "../../common/films";
import FilmSelect from "../../common/filmSelect";
import { Film, FilmSelect, TOP_100_FILMS } from "@blueprintjs/select/examples";

export interface ISelectExampleState {
allowCreate: boolean;
createFirst: boolean;
createdItems: IFilm[];
createdItems: Film[];
disableItems: boolean;
disabled: boolean;
fill: boolean;
Expand Down Expand Up @@ -168,5 +166,5 @@ export class SelectExample extends React.PureComponent<ExampleProps, ISelectExam
};
}

private isItemDisabled = (film: IFilm) => this.state.disableItems && film.year < 2000;
private isItemDisabled = (film: Film) => this.state.disableItems && film.year < 2000;
}
27 changes: 12 additions & 15 deletions packages/docs-app/src/examples/select-examples/suggestExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,26 @@ import * as React from "react";
import { H5, MenuItem, Switch } from "@blueprintjs/core";
import { Example, ExampleProps } from "@blueprintjs/docs-theme";
import { Suggest2 } from "@blueprintjs/select";

import {
areFilmsEqual,
createFilm,
Film,
filterFilm,
IFilm,
maybeAddCreatedFilmToArrays,
maybeDeleteCreatedFilmFromArrays,
renderCreateFilmMenuItem,
renderFilm,
TOP_100_FILMS,
} from "./../../common/films";

const FilmSuggest = Suggest2.ofType<IFilm>();
} from "@blueprintjs/select/examples";

export interface ISuggestExampleState {
export interface SuggestExampleState {
allowCreate: boolean;
closeOnSelect: boolean;
createdItems: IFilm[];
createdItems: Film[];
disabled: boolean;
fill: boolean;
film: IFilm;
items: IFilm[];
film: Film;
items: Film[];
matchTargetWidth: boolean;
minimal: boolean;
openOnKeyDown: boolean;
Expand All @@ -50,8 +47,8 @@ export interface ISuggestExampleState {
resetOnSelect: boolean;
}

export class SuggestExample extends React.PureComponent<ExampleProps, ISuggestExampleState> {
public state: ISuggestExampleState = {
export class SuggestExample extends React.PureComponent<ExampleProps, SuggestExampleState> {
public state: SuggestExampleState = {
allowCreate: false,
closeOnSelect: true,
createdItems: [],
Expand Down Expand Up @@ -95,7 +92,7 @@ export class SuggestExample extends React.PureComponent<ExampleProps, ISuggestEx

return (
<Example options={this.renderOptions()} {...this.props}>
<FilmSuggest
<Suggest2<Film>
{...flags}
createNewItemFromQuery={maybeCreateNewItemFromQuery}
createNewItemRenderer={maybeCreateNewItemRenderer}
Expand Down Expand Up @@ -164,9 +161,9 @@ export class SuggestExample extends React.PureComponent<ExampleProps, ISuggestEx
);
}

private renderInputValue = (film: IFilm) => film.title;
private renderInputValue = (film: Film) => film.title;

private handleValueChange = (film: IFilm) => {
private handleValueChange = (film: Film) => {
// delete the old film from the list if it was newly created
const { createdItems, items } = maybeDeleteCreatedFilmFromArrays(
this.state.items,
Expand All @@ -182,7 +179,7 @@ export class SuggestExample extends React.PureComponent<ExampleProps, ISuggestEx
this.setState({ createdItems: nextCreatedItems, film, items: nextItems });
};

private handleSwitchChange(prop: keyof ISuggestExampleState) {
private handleSwitchChange(prop: keyof SuggestExampleState) {
return (event: React.FormEvent<HTMLInputElement>) => {
const checked = event.currentTarget.checked;
this.setState(state => ({ ...state, [prop]: checked }));
Expand Down
6 changes: 5 additions & 1 deletion packages/eslint-config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ module.exports = {
},
overrides: [
{
files: ["*.js"],
files: ["*.js", "*.mjs"],
env: {
node: true,
es6: true,
},
parserOptions: {
ecmaVersion: 2022,
sourceType: "module",
},
},
{
files: ["**/*.{ts,tsx}"],
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-config/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"core-js",
"date-fns",
"react-dom",
"@blueprintjs/select/examples",
"@blueprintjs/table/src",
"@blueprintjs/test-commons/bootstrap",
"tsutils"
Expand Down
4 changes: 0 additions & 4 deletions packages/node-build-scripts/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
{
"parserOptions": {
"ecmaVersion": 2022,
"sourceType": "module"
},
"rules": {
"no-console": "off"
}
Expand Down
2 changes: 2 additions & 0 deletions packages/select/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*.js
/*.d.ts
1 change: 1 addition & 0 deletions packages/select/examples.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./lib/esm/__examples__";
1 change: 1 addition & 0 deletions packages/select/examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./lib/esm/__examples__";
1 change: 1 addition & 0 deletions packages/select/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { createKarmaConfig } = require("@blueprintjs/karma-build-scripts");
module.exports = function (config) {
const baseConfig = createKarmaConfig({
dirname: __dirname,
coverageExcludes: ["src/__examples__/*"],
});
config.set(baseConfig);
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,23 @@
import * as React from "react";

import { Button, MenuItem } from "@blueprintjs/core";
import { ItemRenderer, Select2, Select2Props } from "@blueprintjs/select";

import type { ItemRenderer } from "../common";
import { Select2, Select2Props } from "../components/select/select2";
import {
areFilmsEqual,
createFilm,
Film,
filterFilm,
getFilmItemProps,
IFilm,
maybeAddCreatedFilmToArrays,
maybeDeleteCreatedFilmFromArrays,
renderCreateFilmMenuItem,
TOP_100_FILMS,
} from "./films";

const FilmSelect = Select2.ofType<IFilm>();

type Props = Omit<
Select2Props<IFilm>,
type FilmSelectProps = Omit<
Select2Props<Film>,
| "createNewItemFromQuery"
| "createNewItemRenderer"
| "itemPredicate"
Expand All @@ -47,15 +46,11 @@ type Props = Omit<
allowCreate?: boolean;
};

// eslint-disable-next-line import/no-default-export
export default function ({ allowCreate = false, fill, ...restProps }: Props) {
const maybeCreateNewItemFromQuery = allowCreate ? createFilm : undefined;
const maybeCreateNewItemRenderer = allowCreate ? renderCreateFilmMenuItem : null;

export function FilmSelect({ allowCreate = false, fill, ...restProps }: FilmSelectProps) {
const [items, setItems] = React.useState([...TOP_100_FILMS]);
const [createdItems, setCreatedItems] = React.useState<IFilm[]>([]);
const [createdItems, setCreatedItems] = React.useState<Film[]>([]);
const [selectedFilm, setSelectedFilm] = React.useState(TOP_100_FILMS[0]);
const handleItemSelect = React.useCallback((newFilm: IFilm) => {
const handleItemSelect = React.useCallback((newFilm: Film) => {
// Delete the old film from the list if it was newly created.
const step1Result = maybeDeleteCreatedFilmFromArrays(items, createdItems, selectedFilm);
// Add the new film to the list if it is newly created.
Expand All @@ -65,7 +60,7 @@ export default function ({ allowCreate = false, fill, ...restProps }: Props) {
setItems(step2Result.items);
}, []);

const itemRenderer = React.useCallback<ItemRenderer<IFilm>>(
const itemRenderer = React.useCallback<ItemRenderer<Film>>(
(film, props) => {
if (!props.modifiers.matchesPredicate) {
return null;
Expand All @@ -76,9 +71,9 @@ export default function ({ allowCreate = false, fill, ...restProps }: Props) {
);

return (
<FilmSelect
createNewItemFromQuery={maybeCreateNewItemFromQuery}
createNewItemRenderer={maybeCreateNewItemRenderer}
<Select2<Film>
createNewItemFromQuery={allowCreate ? createFilm : undefined}
createNewItemRenderer={allowCreate ? renderCreateFilmMenuItem : undefined}
fill={fill}
itemPredicate={filterFilm}
itemRenderer={itemRenderer}
Expand All @@ -96,6 +91,6 @@ export default function ({ allowCreate = false, fill, ...restProps }: Props) {
rightIcon="caret-down"
text={selectedFilm ? `${selectedFilm.title} (${selectedFilm.year})` : "(No selection)"}
/>
</FilmSelect>
</Select2>
);
}
Loading

1 comment on commit b0d5005

@blueprint-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chore: refactor FilmSelect into new private dev package (#5632)

Previews: documentation | landing | table | demo

Please sign in to comment.