| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- import React, { Component } from "react";
- import styled from "styled-components";
- type PropsType = {
- label?: string;
- values: string[];
- setValues: (x: string[]) => void;
- width?: string;
- disabled?: boolean;
- };
- type StateType = {};
- export default class InputArray extends Component<PropsType, StateType> {
- dict2arr = (dict: Record<string, any>) => {
- let arr = [];
- for (let key in dict) {
- arr.push(`${key}: ${dict[key]}`);
- }
- return arr;
- };
- renderDeleteButton = (values: string[], i: number) => {
- if (!this.props.disabled) {
- return (
- <DeleteButton
- onClick={() => {
- let v = [...values];
- v.splice(i, 1);
- this.props.setValues(v);
- }}
- >
- <i className="material-icons">cancel</i>
- </DeleteButton>
- );
- }
- };
- renderInputList = (values: string[]) => {
- return (
- <>
- {values.map((value: string, i: number) => {
- return (
- <InputWrapper>
- <Input
- placeholder=""
- width="270px"
- value={value}
- onChange={(e: any) => {
- let v = [...values];
- v[i] = e.target.value;
- this.props.setValues(v);
- }}
- disabled={this.props.disabled}
- />
- {this.renderDeleteButton(values, i)}
- </InputWrapper>
- );
- })}
- </>
- );
- };
- render() {
- let { values } = this.props;
- if (!Array.isArray(values)) {
- values = this.dict2arr(values);
- }
- return (
- <StyledInputArray>
- <Label>{this.props.label}</Label>
- {values.length === 0 ? <></> : this.renderInputList(values)}
- <AddRowButton
- onClick={() => {
- let v = [...values];
- v.push("");
- this.props.setValues(v);
- }}
- >
- <i className="material-icons">add</i> Add Row
- </AddRowButton>
- </StyledInputArray>
- );
- }
- }
- const AddRowButton = styled.div`
- display: flex;
- align-items: center;
- margin-top: 5px;
- width: 270px;
- font-size: 13px;
- color: #aaaabb;
- height: 30px;
- border-radius: 3px;
- cursor: pointer;
- background: #ffffff11;
- :hover {
- background: #ffffff22;
- }
- > i {
- color: #ffffff44;
- font-size: 16px;
- margin-left: 8px;
- margin-right: 10px;
- display: flex;
- align-items: center;
- justify-content: center;
- }
- `;
- const DeleteButton = styled.div`
- width: 15px;
- height: 15px;
- display: flex;
- align-items: center;
- margin-left: 8px;
- margin-top: -3px;
- justify-content: center;
- > i {
- font-size: 17px;
- color: #ffffff44;
- display: flex;
- align-items: center;
- justify-content: center;
- cursor: pointer;
- :hover {
- color: #ffffff88;
- }
- }
- `;
- const InputWrapper = styled.div`
- display: flex;
- align-items: center;
- `;
- const Input = styled.input`
- outline: none;
- border: none;
- margin-bottom: 5px;
- font-size: 13px;
- background: #ffffff11;
- border: 1px solid #ffffff55;
- border-radius: 3px;
- width: ${(props: { disabled?: boolean; width: string }) =>
- props.width ? props.width : "270px"};
- color: ${(props: { disabled?: boolean; width: string }) =>
- props.disabled ? "#ffffff44" : "white"};
- padding: 5px 10px;
- height: 35px;
- `;
- const Label = styled.div`
- color: #ffffff;
- margin-bottom: 10px;
- `;
- const StyledInputArray = styled.div`
- margin-bottom: 15px;
- margin-top: 22px;
- `;
|