/** * 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 type { InputProps, InputValidator } from "../input"; import { Input } from "../input"; import { autoBind } from "../../utils"; import type { SingleOrMany } from "../../utils"; export interface EditableListProps { items: T[]; add: (newItem: string) => void; remove: (info: { oldItem: T; index: number }) => void; placeholder?: string; validators?: SingleOrMany>; // 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 = { placeholder: "Add new item...", renderItem: (item: any, index: number) => {item}, inputTheme: "round", }; @observer class DefaultedEditableList extends React.Component & typeof defaultProps> { static defaultProps = defaultProps as EditableListProps; constructor(props: EditableListProps & typeof defaultProps) { super(props); autoBind(this); } 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 }))} />
)) }
); } } export function EditableList(props: EditableListProps) { return ; }