Skip to content

Commit

Permalink
resolve pr comments
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Malton <smalton@mirantis.com>
  • Loading branch information
Sebastian Malton committed Aug 19, 2020
1 parent 0a86e01 commit 348a23a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/renderer/components/editable-list/editable-list.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.EditableList {
.EditableListContents {
.el-contents {
display: grid;
grid-template-columns: 1fr auto;

.ValueRemove {
.el-value-remove {
.Icon {
justify-content: unset;
}
Expand Down
36 changes: 24 additions & 12 deletions src/renderer/components/editable-list/editable-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Icon } from "../icon";
import { Input } from "../input";
import { observable } from "mobx";
import { observer } from "mobx-react";
import { autobind } from "../../utils";

export interface Props<T> {
items: T[],
Expand All @@ -14,37 +15,48 @@ export interface Props<T> {

// An optional prop used to convert T to a displayable string
// defaults to `String`
display?: (item: T) => string,
renderItem?: (item: T, index: number) => React.ReactNode,
}

const defaultProps: Partial<Props<any>> = {
placeholder: "Add new item...",
renderItem: (item: any, index: number) => <React.Fragment key={index}>{item}</React.Fragment>
}

@observer
export class EditableList<T> extends React.Component<Props<T>> {
static defaultProps = defaultProps as Props<any>;
@observable currentNewItem = "";

@autobind()
onSubmit(val: string) {
const { add } = this.props

if (val) {
add(val)
this.currentNewItem = ""
}
}

render() {
const { items, add, remove, display = String, placeholder = "Add new item..." } = this.props;
const { items, remove, renderItem, placeholder } = this.props;

return (
<div className="EditableList">
<div className="EditableListHeader">
<div className="el-header">
<Input
value={this.currentNewItem}
onSubmit={val => {
if (val) {
add(val);
}
this.currentNewItem = "";
}}
onSubmit={this.onSubmit}
placeholder={placeholder}
onChange={val => this.currentNewItem = val}
/>
</div>
<div className="EditableListContents">
<div className="el-contents">
{
items
.map((item, index) => [
<span key={`${item}-value`}>{display(item)}</span>,
<div key={`${item}-remove`} className="ValueRemove">
<span key={`${item}-value`}>{renderItem(item, index)}</span>,
<div key={`${item}-remove`} className="el-value-remove">
<Icon material="delete_outline" onClick={() => remove(({ index, oldItem: item }))} />
</div>
])
Expand Down

0 comments on commit 348a23a

Please sign in to comment.