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

WIP React experiments #1796

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 2 additions & 0 deletions packages/controls/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
"d3-format": "^0.5.1",
"jquery": "^3.1.1",
"jquery-ui": "^1.12.1",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"underscore": "^1.8.3"
}
}
85 changes: 85 additions & 0 deletions packages/controls/src/dropdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import {
DescriptionView, DescriptionStyleModel
} from './widget_description';

import {
uuid
} from './utils';

import * as ReactDOM from 'react-dom';

import * as React from 'react';

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a fan of a minimum class comment in JSDoc style. This will help if users/developers with types with helpful hints in an editor like VSCode.

export
class DropdownView extends DescriptionView {
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Style nit: Could we do 2 spaces instead of 4?

* Called when view is rendered.
*/
render() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Explicit return type. And everywhere else this occurs.

super.render();

this.el.classList.add('jupyter-widgets');
this.el.classList.add('widget-inline-hbox');
this.el.classList.add('widget-dropdown');

this.listbox = document.createElement('div');
this.label.htmlFor = uuid();
this.el.appendChild(this.listbox);
this.update();
}

Copy link
Contributor

Choose a reason for hiding this comment

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

A JSDoc comment on public methods for improved typings.

update() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Explicit return of void.

let labels = this.model.get('_options_labels');
let selectedIndex = this.model.get('index');
let value = selectedIndex === null ? '' : selectedIndex;
let props = {
disabled: this.model.get('disabled'),
labels: this.model.get('_options_labels'),
selectedIndex: this.model.get('index'),
id: this.label.htmlFor,
handleChange: (event) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Define an explicit return type. This appears to be void.

this.model.set('index', parseInt(event.target.value, 10));
this.touch();
}
};
ReactDOM.render(React.createElement(DropDown, props), this.listbox);
}

listbox: HTMLDivElement;
Copy link
Contributor

Choose a reason for hiding this comment

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

If this variable is used externally, add a JSDoc style comment above.

/**
 * list box is an HTMLDivElement for foo bar.
 */

}


interface IProps {
disabled: boolean;
labels: string[];
id: string;
selectedIndex: number;
handleChange: (event: Event) => void;
}

class DropDown extends React.Component<IProps, {}> {
render() {
let options = this.props.labels.map((label, index) => {
return (
<option value={index}>
{label.replace(/ /g, '\xa0')}
</option>);
});
let selectedIndex = this.props.selectedIndex;
let value = selectedIndex === null ? '' : selectedIndex;
return (
<select id={this.props.id}
value={value}
disabled={this.props.disabled}
onChange={this.props.handleChange}>
<option value='' disabled></option>
{options}
</select>
);
}

props: IProps;
}
2 changes: 2 additions & 0 deletions packages/controls/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ export * from './widget_selectioncontainer';
export * from './widget_string';
export * from './widget_description';

export * from './dropdown';

export
const version = (require('../package.json') as any).version;
3 changes: 2 additions & 1 deletion packages/controls/src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"module": "commonjs",
"moduleResolution": "node",
"target": "ES5",
"outDir": "../lib"
"outDir": "../lib",
"jsx": "react"
},
"exclude": ["typedoc.d.ts"]
}
2 changes: 1 addition & 1 deletion packages/controls/src/widget_selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class DropdownModel extends SelectionModel {
// For the old code, see commit f68bfbc566f3a78a8f3350b438db8ed523ce3642

export
class DropdownView extends DescriptionView {
class DropdownViewOld extends DescriptionView {
/**
* Public constructor.
*/
Expand Down