import "./editable-list.scss"; import React from "react"; import { Icon } from "../icon"; import { Input } from "../input"; import { observable } from "mobx"; import { observer } from "mobx-react"; import { autobind } from "../../utils"; export interface Props { items: T[], add: (newItem: string) => void, remove: (info: { oldItem: T, index: number }) => void, placeholder?: string, // An optional prop used to convert T to a displayable string // defaults to `String` renderItem?: (item: T, index: number) => React.ReactNode, } const defaultProps: Partial> = { placeholder: "Add new item...", renderItem: (item: any, index: number) => {item} }; @observer export class EditableList extends React.Component> { static defaultProps = defaultProps as Props; @observable currentNewItem = ""; @autobind() onSubmit(val: string) { const { add } = this.props; if (val) { add(val); this.currentNewItem = ""; } } render() { const { items, remove, renderItem, placeholder } = this.props; return (
this.currentNewItem = val} />
{ items.map((item, index) => (
{renderItem(item, index)}
remove(({ index, oldItem: item }))} />
)) }
); } }