Skip to content
This repository was archived by the owner on Jun 1, 2020. It is now read-only.

Commit 85025bf

Browse files
committed
Handle empty strings the react-jsonschema-form way
Resolves #6
1 parent bc936c8 commit 85025bf

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

example/src/body/Body.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React from 'react';
2+
23
import { withStyles } from '@material-ui/core/styles';
4+
35
import styles from './body-styles';
46
import Example from './Example';
57

src/RadioWidget/RadioWidget.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const RadioWidget = (props: WidgetProps) => {
2020
label,
2121
onChange,
2222
} = props;
23+
2324
// Generating a unique field name to identify this set of radio buttons
2425
const name = Math.random().toString();
2526
const { enumOptions, enumDisabled } = options;
@@ -28,6 +29,7 @@ const RadioWidget = (props: WidgetProps) => {
2829
// checked={checked} has been moved above name={name}, As mentioned in #349;
2930
// this is a temporary fix for radio button rendering bug in React, facebook/react#7630.
3031
const row = options ? options.inline : false;
32+
3133
return (
3234
<FormControl
3335
fullWidth={true}

src/TextWidget/TextWidget.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,31 @@ const TextWidget = ({
1313
label,
1414
value,
1515
onChange,
16+
onBlur,
17+
onFocus,
1618
autofocus,
19+
options,
1720
}: WidgetProps) => {
1821
const _onChange = ({
1922
target: { value },
20-
}: React.ChangeEvent<HTMLInputElement>) => onChange(value);
23+
}: React.ChangeEvent<HTMLInputElement>) => {
24+
let inputValue: any = value;
25+
26+
if (!inputValue) {
27+
if (options.emptyValue === undefined) {
28+
inputValue = undefined;
29+
} else {
30+
inputValue = options.emptyValue;
31+
}
32+
}
33+
34+
onChange(inputValue);
35+
};
36+
const _onBlur = ({ target: { value } }: React.FocusEvent<HTMLInputElement>) =>
37+
onBlur(id, value);
38+
const _onFocus = ({
39+
target: { value },
40+
}: React.FocusEvent<HTMLInputElement>) => onFocus(id, value);
2141

2242
return (
2343
<>
@@ -30,6 +50,8 @@ const TextWidget = ({
3050
name={name}
3151
value={value}
3252
onChange={_onChange}
53+
onBlur={_onBlur}
54+
onFocus={_onFocus}
3355
/>
3456
</>
3557
);

src/TextareaWidget/TextareaWidget.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const TextareaWidget = ({
3232
const _onFocus = ({
3333
target: { value },
3434
}: React.FocusEvent<HTMLInputElement>) => onFocus(id, value);
35+
3536
return (
3637
<>
3738
<InputLabel>{label}</InputLabel>

0 commit comments

Comments
 (0)