import React, { Component } from 'react';
import { Col, Form, Input, Row, Select } from 'antd';
import { macAddressValidate } from 'utils/validate';
class MacAddressInput extends Component {
constructor(props) {
super(props);
this.state = {
name: {
type: '',
mac: '',
},
};
}
handleSelectChange = (e) => {
const { name } = this.state;
this.setState(
{
name: {
...name,
type: e,
},
},
() => {
const { onChange } = this.props;
if (onChange) {
onChange(this.state.name);
}
}
);
};
handleInputChange = (e) => {
const { name } = this.state;
this.setState({
name: {
...name,
mac: e,
},
});
};
render() {
const { value, name, options } = this.props;
const { type } = value || { type: undefined };
return (
<Row gutter={16}>
<Col span={12}>
<Form.Item name={[name, 'type']} style={{ marginBottom: 0 }}>
<Select
placeholder={t('Please select')}
options={
options || [
{
label: t('Auto allocate MAC address'),
value: 'auto',
},
{
label: t('Manual input'),
value: 'manual',
},
]
}
onChange={this.handleSelectChange}
/>
</Form.Item>
</Col>
<Col span={12}>
{
value && type === 'manual' ? (
<Form.Item
hidden={type === 'auto' || type === undefined}
name={[name, 'mac']}
rules={[
{
validator: macAddressValidate,
required: true,
message: t(
'Invalid MAC Address. Please use ":" as separator.'
),
},
]}
style={{ marginBottom: 0 }}
>
<Input maxLength={17} onChange={this.handleInputChange} />
</Form.Item>
) : null
// <span style={{ lineHeight: '32px', display: type !== 'auto' ? 'none' : '' }}>
// {t('MAC Address Numbers can be use {num}'
// , { num: 123 })}
// </span>
}
</Col>
</Row>
);
}
}
export default MacAddressInput;