/* Copyright 2017-2021 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React from "react"; import { _t } from '../../../languageHandler'; import Field from "./Field"; import AccessibleButton from "./AccessibleButton"; import { replaceableComponent } from "../../../utils/replaceableComponent"; interface IItemProps { index?: number; value?: string; onRemove?(index: number): void; } interface IItemState { verifyRemove: boolean; } export class EditableItem extends React.Component { public state = { verifyRemove: false, }; private onRemove = (e) => { e.stopPropagation(); e.preventDefault(); this.setState({ verifyRemove: true }); }; private onDontRemove = (e) => { e.stopPropagation(); e.preventDefault(); this.setState({ verifyRemove: false }); }; private onActuallyRemove = (e) => { e.stopPropagation(); e.preventDefault(); if (this.props.onRemove) this.props.onRemove(this.props.index); this.setState({ verifyRemove: false }); }; render() { if (this.state.verifyRemove) { return (
{ _t("Are you sure?") } { _t("Yes") } { _t("No") }
); } return (
{ this.props.value }
); } } interface IProps { id: string; items: string[]; itemsLabel?: string; noItemsLabel?: string; placeholder?: string; newItem?: string; canEdit?: boolean; canRemove?: boolean; suggestionsListId?: string; onItemAdded?(item: string): void; onItemRemoved?(index: number): void; onNewItemChanged?(item: string): void; } @replaceableComponent("views.elements.EditableItemList") export default class EditableItemList

extends React.PureComponent { protected onItemAdded = (e) => { e.stopPropagation(); e.preventDefault(); if (this.props.onItemAdded) this.props.onItemAdded(this.props.newItem); }; protected onItemRemoved = (index) => { if (this.props.onItemRemoved) this.props.onItemRemoved(index); }; protected onNewItemChanged = (e) => { if (this.props.onNewItemChanged) this.props.onNewItemChanged(e.target.value); }; protected renderNewItemField() { return (

{ _t("Add") } ); } render() { const editableItems = this.props.items.map((item, index) => { if (!this.props.canRemove) { return
  • { item }
  • ; } return ; }); const editableItemsSection = this.props.canRemove ? editableItems :
      { editableItems }
    ; const label = this.props.items.length > 0 ? this.props.itemsLabel : this.props.noItemsLabel; return (
    { label }
    { editableItemsSection } { this.props.canEdit ? this.renderNewItemField() :
    }
    ); } }