index.js 1.15 KB
Newer Older
DarkForst's avatar
DarkForst committed
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
import React, { Component, Fragment } from 'react';
import { Row, Col, Card, Input, Select } from 'antd';

const Option = Select.Option;
export default class HttpPrefix extends Component {

  constructor(props) {
    super(props);
  }

  state = {
    prefix: null,
    value: ''
  }

  componentDidMount() {
    const linkUrl = this.props.value && this.props.value.split("://") || [];
    this.setState({
      prefix: linkUrl.length > 1 ? `${linkUrl[0]}://`: 'http://',
      value: linkUrl.length > 1 ? linkUrl[1]: linkUrl[0],
    });
  }

  changeValue = (value) => {
    this.setState({
      value,
    });
    this.props.onChange(this.state.prefix + value);
  }

  changePrefix = (prefix) => {
    this.setState({
      prefix,
    });
    this.props.onChange(prefix + this.state.value);
  }

  render() {
    const prefixSelector = <Select onChange={(value) => this.changePrefix(value)} style={{ width: 100 }} value={this.state.prefix}>
      <Option value="http://">http://</Option>
      <Option value="https://">https://</Option>
    </Select>
    return (
      <Input addonBefore={prefixSelector} onChange={(e)=> this.changeValue(e.target.value)} />
    )
  }
};