Skip to content

Commit 65597d1

Browse files
authoredMay 14, 2020
fix: jsonSchemaComponent file/files (#5997) (#6000)
oas3 - files oas2 - file
1 parent 96c7b4c commit 65597d1

File tree

1 file changed

+68
-32
lines changed

1 file changed

+68
-32
lines changed
 

‎src/core/json-schema-components.jsx

+68-32
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export class JsonSchemaForm extends Component {
4747
let { schema, errors, value, onChange, getComponent, fn, disabled } = this.props
4848
const format = schema && schema.get ? schema.get("format") : null
4949
const type = schema && schema.get ? schema.get("type") : null
50+
5051
let getComponentSilently = (name) => getComponent(name, false, { failSilently: true })
5152
let Comp = type ? format ?
5253
getComponentSilently(`JsonSchema_${type}_${format}`) :
@@ -63,7 +64,7 @@ export class JsonSchema_string extends Component {
6364
static propTypes = JsonSchemaPropShape
6465
static defaultProps = JsonSchemaDefaultProps
6566
onChange = (e) => {
66-
const value = this.props.schema && this.props.schema["type"] === "file" ? e.target.files[0] : e.target.value
67+
const value = this.props.schema && this.props.schema.get("type") === "file" ? e.target.files[0] : e.target.value
6768
this.props.onChange(value, this.props.keyName)
6869
}
6970
onEnumChange = (val) => this.props.onChange(val)
@@ -92,23 +93,27 @@ export class JsonSchema_string extends Component {
9293
const isDisabled = disabled || (schemaIn && schemaIn === "formData" && !("FormData" in window))
9394
const Input = getComponent("Input")
9495
if (type && type === "file") {
95-
return (<Input type="file"
96-
className={ errors.length ? "invalid" : ""}
97-
title={ errors.length ? errors : ""}
98-
onChange={ this.onChange }
99-
disabled={isDisabled}/>)
96+
return (
97+
<Input type="file"
98+
className={errors.length ? "invalid" : ""}
99+
title={errors.length ? errors : ""}
100+
onChange={this.onChange}
101+
disabled={isDisabled} />
102+
)
100103
}
101104
else {
102-
return (<DebounceInput
103-
type={ format && format === "password" ? "password" : "text" }
104-
className={ errors.length ? "invalid" : ""}
105-
title={ errors.length ? errors : ""}
106-
value={value}
107-
minLength={0}
108-
debounceTimeout={350}
109-
placeholder={description}
110-
onChange={ this.onChange }
111-
disabled={isDisabled}/>)
105+
return (
106+
<DebounceInput
107+
type={format && format === "password" ? "password" : "text"}
108+
className={errors.length ? "invalid" : ""}
109+
title={errors.length ? errors : ""}
110+
value={value}
111+
minLength={0}
112+
debounceTimeout={350}
113+
placeholder={description}
114+
onChange={this.onChange}
115+
disabled={isDisabled} />
116+
)
112117
}
113118
}
114119
}
@@ -170,14 +175,15 @@ export class JsonSchema_array extends PureComponent {
170175
const schemaItemsSchema = schema.getIn(["items", "schema"])
171176
let ArrayItemsComponent
172177
let isArrayItemText = false
178+
let isArrayItemFile = schemaItemsType === "file" ? true : false
173179
if (schemaItemsType && schemaItemsFormat) {
174180
ArrayItemsComponent = getComponent(`JsonSchema_${schemaItemsType}_${schemaItemsFormat}`)
175181
} else if (schemaItemsType === "boolean" || schemaItemsType === "array" || schemaItemsType === "object") {
176182
ArrayItemsComponent = getComponent(`JsonSchema_${schemaItemsType}`)
177183
}
178184
// if ArrayItemsComponent not assigned or does not exist,
179185
// use default schemaItemsType === "string" & JsonSchemaArrayItemText component
180-
if (!ArrayItemsComponent) {
186+
if (!ArrayItemsComponent && !isArrayItemFile) {
181187
isArrayItemText = true
182188
}
183189

@@ -205,22 +211,30 @@ export class JsonSchema_array extends PureComponent {
205211
return (
206212
<div key={i} className="json-schema-form-item">
207213
{
208-
isArrayItemText ?
209-
<JsonSchemaArrayItemText
210-
value={item}
211-
onChange={(val) => this.onItemChange(val, i)}
212-
disabled={disabled}
213-
errors={errors}
214-
/>
215-
: <ArrayItemsComponent {...this.props}
216-
value={item}
217-
onChange={(val) => this.onItemChange(val, i)}
218-
disabled={disabled}
219-
errors={errors}
220-
schema={schemaItemsSchema}
221-
getComponent={getComponent}
222-
fn={fn}
214+
isArrayItemFile ?
215+
<JsonSchemaArrayItemFile
216+
value={item}
217+
onChange={(val)=> this.onItemChange(val, i)}
218+
disabled={disabled}
219+
errors={errors}
220+
getComponent={getComponent}
223221
/>
222+
: isArrayItemText ?
223+
<JsonSchemaArrayItemText
224+
value={item}
225+
onChange={(val) => this.onItemChange(val, i)}
226+
disabled={disabled}
227+
errors={errors}
228+
/>
229+
: <ArrayItemsComponent {...this.props}
230+
value={item}
231+
onChange={(val) => this.onItemChange(val, i)}
232+
disabled={disabled}
233+
errors={errors}
234+
schema={schemaItemsSchema}
235+
getComponent={getComponent}
236+
fn={fn}
237+
/>
224238
}
225239
{!disabled ? (
226240
<Button
@@ -275,6 +289,28 @@ export class JsonSchemaArrayItemText extends Component {
275289
}
276290
}
277291

292+
export class JsonSchemaArrayItemFile extends Component {
293+
static propTypes = JsonSchemaPropShape
294+
static defaultProps = JsonSchemaDefaultProps
295+
296+
onFileChange = (e) => {
297+
const value = e.target.files[0]
298+
this.props.onChange(value, this.props.keyName)
299+
}
300+
301+
render() {
302+
let { getComponent, errors, disabled } = this.props
303+
const Input = getComponent("Input")
304+
const isDisabled = disabled || !("FormData" in window)
305+
306+
return (<Input type="file"
307+
className={errors.length ? "invalid" : ""}
308+
title={errors.length ? errors : ""}
309+
onChange={this.onFileChange}
310+
disabled={isDisabled} />)
311+
}
312+
}
313+
278314
export class JsonSchema_boolean extends Component {
279315
static propTypes = JsonSchemaPropShape
280316
static defaultProps = JsonSchemaDefaultProps

0 commit comments

Comments
 (0)
Please sign in to comment.