Skip to content

Commit

Permalink
paste feature requested on yuvadm#48
Browse files Browse the repository at this point in the history
  • Loading branch information
ranga543 committed Jan 28, 2022
1 parent e2e0f8d commit 4e96dd9
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class IPAddress extends Component {
};
this.handleChange = this.handleChange.bind(this);
this.handleKeyDown = this.handleKeyDown.bind(this);
this.handlePaste = this.handlePaste.bind(this);
}

handleChange(event) {
Expand Down Expand Up @@ -64,6 +65,30 @@ class IPAddress extends Component {
}
}

handlePaste(event) {
const pasteText = event.clipboardData.getData('Text')
const regExp = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
const cidrParts = pasteText.split('/')
if (!regExp.test(cidrParts[0])) {
alert('You have pasted an invalid IP address!')
return false
}
var octets = this.state.octets;
const octetParts = cidrParts[0].split('.')
octetParts.map((octet, i) => {
octets[i] = octet;
})
this.setState({
octets: octets
});

if (+cidrParts[1] <= 32) {
this.setState({
cidr: +cidrParts[1]
});
}
}

getPretty() {
return this.state.octets.join('.') + '/' + this.state.cidr;
}
Expand All @@ -82,6 +107,7 @@ class IPAddress extends Component {
data-octet={octet}
onChange={this.handleChange}
onKeyDown={this.handleKeyDown}
onPaste={this.handlePaste}
value={this.state.octets[octet]}
/>
<span className="dot">{octet == '3' ? '/' : '.'}</span>
Expand All @@ -93,6 +119,7 @@ class IPAddress extends Component {
data-octet="cidr"
onChange={this.handleChange}
onKeyDown={this.handleKeyDown}
onPaste={this.handlePaste}
value={this.state.cidr}
/>
</div>
Expand Down

0 comments on commit 4e96dd9

Please sign in to comment.