/** * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ import "./editable-list.scss"; import { observer } from "mobx-react"; import React from "react"; import { Icon } from "../icon"; import { Input, InputProps, InputValidator } from "../input"; import { boundMethod } from "../../utils"; export interface Props { items: T[], add: (newItem: string) => void, remove: (info: { oldItem: T, index: number }) => void, placeholder?: string, validators?: InputValidator | InputValidator[]; // An optional prop used to convert T to a displayable string // defaults to `String` renderItem?: (item: T, index: number) => React.ReactNode, inputTheme?: InputProps["theme"]; } const defaultProps: Partial> = { placeholder: "Add new item...", renderItem: (item: any, index: number) => {item}, inputTheme: "round", }; @observer export class EditableList extends React.Component> { static defaultProps = defaultProps as Props; @boundMethod onSubmit(val: string, evt: React.KeyboardEvent) { if (val) { evt.preventDefault(); this.props.add(val); } } render() { const { items, remove, renderItem, placeholder, validators, inputTheme } = this.props; return (
isDirty ? : null} />
{ items.map((item, index) => (
{renderItem(item, index)}
remove(({ index, oldItem: item }))} />
)) }
); } }