import React from 'react'; import { Input, Button } from 'antd'; import IconFont from '../../extra/iconFont'; import styles from './styles.less'; import { LinkProps, LinkState } from '../editInterface'; class Link extends React.Component { static getDerivedStateFromProps(nextProps: LinkProps, prevState: LinkState) { const { prevProps } = prevState; let nextState: LinkState = { ...prevState, prevProps: nextProps, }; if (JSON.stringify(prevProps.value) !== JSON.stringify(nextProps.value)) { nextState.value = nextProps.value; } return nextState; } constructor(props: LinkProps) { super(props); const { value } = props; this.state = { prevProps: props, value, }; } addLink = () => { const { value } = this.state; const newValue = (value && value.slice()) || []; newValue.push({}); this.setState({ value: newValue, }); }; changeLinkValue = (index, e) => { const { value } = this.state; const temp = value[index]; temp.value = e.target.value; this.setState( { value, }, this.changeLink, ); }; changeLinkText = (index, e) => { const { value } = this.state; const temp = value[index]; temp.text = e.target.value; this.setState( { value, }, this.changeLink, ); }; changeLink = () => { const { onChange } = this.props; const { value } = this.state; if (typeof onChange === 'function') { onChange(value); } }; render() { const { value } = this.state; return (
{value && value.map((link, i) => { return (
链接地址:
显示文字:
); })}
); } } export default Link;