-
Notifications
You must be signed in to change notification settings - Fork 947
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
WIP React experiments #1796
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
|
||
export | ||
class DropdownView extends DescriptionView { | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A JSDoc comment on public methods for improved typings. |
||
update() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this variable is used externally, add a JSDoc style comment above.
|
||
} | ||
|
||
|
||
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; | ||
} |
There was a problem hiding this comment.
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.