-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
FindAndReplaceComponent.tsx
112 lines (104 loc) · 3.27 KB
/
FindAndReplaceComponent.tsx
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
import { Button, TextField, Typography } from "@material-ui/core";
import React from "react";
import { LocalizeContextProps, withLocalize } from "react-localize-redux";
import CharacterReplaceDialog from "goals/CharInventoryCreation/components/CharacterDetail/FindAndReplace/CharacterReplaceDialog";
interface FindAndReplaceProps {
initialFindValue: string;
allWords: string[];
findAndReplace: (findValue: string, replaceValue: string) => void;
}
interface FindAndReplaceState {
findValue: string;
replaceValue: string;
warningDialogOpen: boolean;
}
export class FindAndReplace extends React.Component<
FindAndReplaceProps & LocalizeContextProps,
FindAndReplaceState
> {
constructor(props: FindAndReplaceProps & LocalizeContextProps) {
super(props);
this.state = {
warningDialogOpen: false,
findValue: props.initialFindValue,
replaceValue: "",
};
}
componentDidUpdate(prevProps: FindAndReplaceProps & LocalizeContextProps) {
if (prevProps.initialFindValue !== this.props.initialFindValue) {
this.setState((_, props) => ({
findValue: props.initialFindValue,
replaceValue: "",
}));
}
}
/** Updates the state to match the value in a textbox */
updateField<K extends keyof FindAndReplaceState>(
e: React.ChangeEvent<
HTMLTextAreaElement | HTMLInputElement | HTMLSelectElement
>,
field: K
) {
const value = e.target.value;
this.setState({
[field]: value,
} as Pick<FindAndReplaceState, K>);
}
render() {
return (
<React.Fragment>
<Typography variant="overline">
{this.props.translate("charInventory.characterSet.findAndReplace")}
</Typography>
<TextField
label={this.props.translate("charInventory.characterSet.find")}
value={this.state.findValue}
onChange={(e) => this.updateField(e, "findValue")}
variant="outlined"
style={{ width: "100%" }}
margin="normal"
inputProps={{ maxLength: 100 }}
/>
<TextField
label={this.props.translate("charInventory.characterSet.replaceWith")}
value={this.state.replaceValue}
onChange={(e) => this.updateField(e, "replaceValue")}
variant="outlined"
style={{ width: "100%" }}
margin="normal"
inputProps={{ maxLength: 100 }}
/>
<Button
color="primary"
onClick={() => {
this.setState(() => ({
warningDialogOpen: true,
}));
}}
>
{this.props.translate("charInventory.characterSet.apply")}
</Button>
<CharacterReplaceDialog
open={this.state.warningDialogOpen}
dialogFindValue={this.state.findValue}
dialogReplaceValue={this.state.replaceValue}
handleCancel={() => {
this.setState(() => ({
warningDialogOpen: false,
}));
}}
handleAccept={() => {
this.setState(() => ({
warningDialogOpen: false,
}));
this.props.findAndReplace(
this.state.findValue,
this.state.replaceValue
);
}}
/>
</React.Fragment>
);
}
}
export default withLocalize(FindAndReplace);