aboutsummaryrefslogtreecommitdiff
path: root/src/settings/components/form/PartialBlacklistForm.tsx
blob: 4c6bd356894960e2cbfb915c1939059772a33c1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import React from "react";
import styled from "styled-components";
import AddButton from "../ui/AddButton";
import DeleteButton from "../ui/DeleteButton";
import Blacklist, { BlacklistItem } from "../../../shared/settings/Blacklist";

const Grid = styled.div``;

const GridRow = styled.div`
  display: flex;
`;

const GridCell = styled.div<{ grow?: number }>`
  &:nth-child(1) {
    flex-grow: 5;
  }

  &:nth-child(2) {
    flex-shrink: 1;
    min-width: 20%;
    max-width: 20%;
  }

  &:nth-child(3) {
    flex-shrink: 1;
  }
`;

const Input = styled.input`
  width: 100%;
  box-sizing: border-box;
`;

interface Props {
  value: Blacklist;
  onChange: (value: Blacklist) => void;
  onBlur: () => void;
}

class PartialBlacklistForm extends React.Component<Props> {
  public static defaultProps: Props = {
    value: new Blacklist([]),
    onChange: () => {},
    onBlur: () => {},
  };

  render() {
    return (
      <>
        <Grid>
          <GridRow>
            <GridCell>URL</GridCell>
            <GridCell>Keys</GridCell>
          </GridRow>
          {this.props.value.items.map((item, index) => {
            if (!item.partial) {
              return null;
            }
            return (
              <GridRow key={index}>
                <GridCell>
                  <Input
                    data-index={index}
                    type="text"
                    name="url"
                    value={item.pattern}
                    placeholder="example.com/mail/*"
                    onChange={this.bindValue.bind(this)}
                    onBlur={this.props.onBlur}
                  />
                </GridCell>
                <GridCell>
                  <Input
                    data-index={index}
                    type="text"
                    name="keys"
                    value={item.keys.join(",")}
                    placeholder="j,k,<C-d>,<C-u>"
                    onChange={this.bindValue.bind(this)}
                    onBlur={this.props.onBlur}
                  />
                </GridCell>
                <GridCell>
                  <DeleteButton
                    data-index={index}
                    name="delete"
                    onClick={this.bindValue.bind(this)}
                    onBlur={this.props.onBlur}
                  />
                </GridCell>
              </GridRow>
            );
          })}
        </Grid>
        <AddButton
          name="add"
          style={{ float: "right" }}
          onClick={this.bindValue.bind(this)}
        />
      </>
    );
  }

  bindValue(e: any) {
    const name = e.target.name;
    const index = e.target.getAttribute("data-index");
    const items = this.props.value.items;

    if (name === "url") {
      const current = items[index];
      items[index] = new BlacklistItem(e.target.value, true, current.keys);
    } else if (name === "keys") {
      const current = items[index];
      items[index] = new BlacklistItem(
        current.pattern,
        true,
        e.target.value.split(",")
      );
    } else if (name === "add") {
      items.push(new BlacklistItem("", true, []));
    } else if (name === "delete") {
      items.splice(index, 1);
    }

    this.props.onChange(new Blacklist(items));
    if (name === "delete") {
      this.props.onBlur();
    }
  }
}

export default PartialBlacklistForm;