Skip to content

修改一个bug,增加两个功能 #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions package/components/LocalProvider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ const langs = {
'enum_desc_msg': 'enum description',
'required': 'required',
'mock': 'mock',
'mockLink': 'Help'
'mockLink': 'Help',
'integerFormat': 'integer format',
'className': 'class name'
},
zh_CN: {
'title': '标题',
Expand Down Expand Up @@ -70,7 +72,9 @@ const langs = {
'enum_desc_msg': '备注描述信息',
'required': '是否必须',
'mock': 'mock',
'mockLink': '查看文档'
'mockLink': '查看文档',
'integerFormat': '整数类型',
'className': '对象类名'
}
}

Expand Down
2 changes: 1 addition & 1 deletion package/components/SchemaComponents/SchemaJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class SchemaArray extends PureComponent {
<Input
addonAfter={<Icon type="edit" onClick={() => this.handleShowEdit('title')} />}
placeholder={LocaleProvider('title')}
value={value.title}
value={items.title}
onChange={this.handleChangeTitle}
/>
</Col>
Expand Down
285 changes: 284 additions & 1 deletion package/components/SchemaComponents/SchemaOther.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,52 @@ const changeOtherValue = (value, name, data, change) => {
change(data);
};

class SchemaObject extends PureComponent {
constructor(props) {
super(props);
this.state = {
checked: _.isUndefined(props.data.enum) ? false : true
};
}

componentWillReceiveProps(nextprops) {
if (this.props.data.enum !== nextprops.data.enum) {
this.setState({
checked: _.isUndefined(nextprops.data.enum) ? false : true
});
}
}

changeClassNameValue = (value, data) => {
data["$$ref"] = "#/definitions/" + value;
this.context.changeCustomValue(data);
};

render() {
const { data } = this.props;
return (
<div>
<div className="default-setting">{LocalProvider('base_setting')}</div>
<Row className="other-row" type="flex" align="middle">
<Col span={4} className="other-label">
{LocalProvider('className')}:
</Col>
<Col span={20}>
<Input
value={data.$$ref == null ? '' : data.$$ref.substr(14)}
placeholder={LocalProvider('className')}
onChange={e => this.changeClassNameValue(e.target.value, data)}
/>
</Col>
</Row>
</div>
);
}
}
SchemaObject.contextTypes = {
changeCustomValue: PropTypes.func
};

class SchemaString extends PureComponent {
constructor(props, context) {
super(props);
Expand Down Expand Up @@ -424,6 +470,242 @@ SchemaNumber.contextTypes = {
changeCustomValue: PropTypes.func
};

class SchemaInteger extends PureComponent {

constructor(props, context) {
super(props);
this.state = {
checked: _.isUndefined(props.data.enum) ? false : true,
enum: _.isUndefined(props.data.enum) ? '' : props.data.enum.join('\n')
};
this.format = context.Model.__jsonIntegerFormat;
}

componentWillReceiveProps(nextprops) {
const enumStr = _.isUndefined(this.props.data.enum) ? '' : this.props.data.enum.join('\n');
const nextEnumStr = _.isUndefined(nextprops.data.enum) ? '' : nextprops.data.enum.join('\n');
if (enumStr !== nextEnumStr) {
this.setState({ enum: nextEnumStr });
}
}

changeOtherValue = (value, name, data) => {
data[name] = value;
this.context.changeCustomValue(data);
};

onChangeCheckBox = (checked, data) => {
this.setState({
checked
});

if (!checked) {
delete data.enum;
this.setState({ enum: '' });
this.context.changeCustomValue(data);
}
};

changeEnumOtherValue = (value, data) => {
this.setState({ enum: value });
var arr = value.split('\n');
if (arr.length === 0 || (arr.length == 1 && !arr[0])) {
delete data.enum;
this.context.changeCustomValue(data);
} else {
data.enum = arr.map(item => +item);
this.context.changeCustomValue(data);
}
};

onEnterEnumOtherValue = (value, data) => {
let arr = value.split('\n').map(item => +item);
data.enum = arr;
this.context.changeCustomValue(data);
};

changeEnumDescOtherValue = (value, data) => {
data.enumDesc = value;
this.context.changeCustomValue(data);
};

render() {
const { data } = this.props;
return (
<div>
<div className="default-setting">{LocalProvider('base_setting')}</div>
<Row className="other-row" type="flex" align="middle">
<Col span={4} className="other-label">
{LocalProvider('integerFormat')}:
</Col>
<Col span={8}>
<Select
showSearch
style={{ width: 150 }}
value={data.format}
dropdownClassName="json-schema-react-editor-adv-modal-select"
placeholder="Select a format"
optionFilterProp="children"
optionLabelProp="value"
onChange={e => this.changeOtherValue(e, 'format', data)}
filterOption={(input, option) => {
return option.props.value.toLowerCase().indexOf(input.toLowerCase()) >= 0;
}}
>
{this.format.map(item => {
return (
<Option value={item.name} key={item.name}>
{item.name} <span className="format-items-title">{item.title}</span>
</Option>
);
})}
</Select>
</Col>
<Col span={4} className="other-label">
{LocalProvider('default')}:
</Col>
<Col span={8}>
<Input
value={data.default}
placeholder={LocalProvider('default')}
onChange={e =>
changeOtherValue(e.target.value, 'default', data, this.context.changeCustomValue)
}
/>
</Col>
</Row>
<Row className="other-row" type="flex" align="middle">
<Col span={12}>
<Row type="flex" align="middle">
<Col span={13} className="other-label">
<span>
exclusiveMinimum&nbsp;
<Tooltip title={LocalProvider('exclusiveMinimum')}>
<Icon type="question-circle-o" style={{ width: '10px' }} />
</Tooltip>
&nbsp; :
</span>
</Col>
<Col span={11}>
<Switch
checked={data.exclusiveMinimum}
placeholder="exclusiveMinimum"
onChange={e =>
changeOtherValue(e, 'exclusiveMinimum', data, this.context.changeCustomValue)
}
/>
</Col>
</Row>
</Col>
<Col span={12}>
<Row type="flex" align="middle">
<Col span={13} className="other-label">
<span>
exclusiveMaximum&nbsp;
<Tooltip title={LocalProvider('exclusiveMaximum')}>
<Icon type="question-circle-o" style={{ width: '10px' }} />
</Tooltip>
&nbsp; :
</span>
</Col>
<Col span={11}>
<Switch
checked={data.exclusiveMaximum}
placeholder="exclusiveMaximum"
onChange={e =>
changeOtherValue(e, 'exclusiveMaximum', data, this.context.changeCustomValue)
}
/>
</Col>
</Row>
</Col>
</Row>
<Row className="other-row" type="flex" align="middle">
<Col span={12}>
<Row type="flex" align="middle">
<Col span={8} className="other-label">
{LocalProvider('minimum')}:
</Col>
<Col span={16}>
<InputNumber
value={data.minimum}
placeholder={LocalProvider('minimum')}
onChange={e =>
changeOtherValue(e, 'minimum', data, this.context.changeCustomValue)
}
/>
</Col>
</Row>
</Col>
<Col span={12}>
<Row type="flex" align="middle">
<Col span={8} className="other-label">
{LocalProvider('maximum')}:
</Col>
<Col span={16}>
<InputNumber
value={data.maximum}
placeholder={LocalProvider('maximum')}
onChange={e =>
changeOtherValue(e, 'maximum', data, this.context.changeCustomValue)
}
/>
</Col>
</Row>
</Col>
</Row>
<Row className="other-row" type="flex" align="middle">
<Col span={4} className="other-label">
<span>
{LocalProvider('enum')}
<Checkbox
checked={this.state.checked}
onChange={e => this.onChangeCheckBox(e.target.checked, data)}
/>{' '}
:
</span>
</Col>
<Col span={20}>
<TextArea
// value={data.enum && data.enum.length && data.enum.join('\n')}
value={this.state.enum}
disabled={!this.state.checked}
placeholder={LocalProvider('enum_msg')}
autosize={{ minRows: 2, maxRows: 6 }}
onChange={e => {
this.changeEnumOtherValue(e.target.value, data);
}}
/>
</Col>
</Row>
{this.state.checked && (
<Row className="other-row" type="flex" align="middle">
<Col span={4} className="other-label">
<span>{LocalProvider('enum_desc')} :</span>
</Col>
<Col span={20}>
<TextArea
value={data.enumDesc}
disabled={!this.state.checked}
placeholder={LocalProvider('enum_desc_msg')}
autosize={{ minRows: 2, maxRows: 6 }}
onChange={e => {
this.changeEnumDescOtherValue(e.target.value, data);
}}
/>
</Col>
</Row>
)}
</div>
);
}
}

SchemaInteger.contextTypes = {
changeCustomValue: PropTypes.func,
Model: PropTypes.object
};

const SchemaBoolean = (props, context) => {
const { data } = props;
let value = _.isUndefined(data.default) ? '' : data.default ? 'true' : 'false';
Expand Down Expand Up @@ -526,7 +808,8 @@ const mapping = data => {
string: <SchemaString data={data} />,
number: <SchemaNumber data={data} />,
boolean: <SchemaBoolean data={data} />,
integer: <SchemaNumber data={data} />,
integer: <SchemaInteger data={data} />,
object: <SchemaObject data={data} />,
array: <SchemaArray data={data} />
}[data.type];
};
Expand Down
5 changes: 5 additions & 0 deletions package/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ module.exports = (config = {})=>{
} else {
Model.__jsonSchemaFormat = utils.format
}
if(config.integerFormat){
Model.__jsonIntegerFormat = config.integerFormat
} else {
Model.__jsonIntegerFormat = utils.integerFormat
}

if(config.mock) {
Model.__jsonSchemaMock = config.mock
Expand Down
5 changes: 5 additions & 0 deletions package/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ exports.format = [
{ name: 'ipv6' },
{ name: 'uri' }
];
exports.integerFormat = [
{ title: 'Integer', name: 'int32' },
{ title: 'Long', name: 'int64' },
{ title: 'Short', name: 'int16' }
];
const _ = require('underscore');
exports.SCHEMA_TYPE = ['string', 'number', 'array', 'object', 'boolean', 'integer'];
exports.defaultSchema = {
Expand Down