Fix FieldString not giving error on empty string as initial value

pull/133/head
Kelvin Schoofs 6 years ago
parent 80ddb934f5
commit 27317f8031

@ -24,9 +24,9 @@ export abstract class FieldBase<T, P = {}, S = {}> extends React.Component<Props
constructor(props: Props<T> & P) {
super(props);
this.state = {
...this.getInitialSubState(props) as any,
newValue: props.value,
oldValue: props.value,
...this.getInitialSubState(props) as any,
}
}
public getInitialSubState(props: Props<T> & P): S {
@ -35,6 +35,11 @@ export abstract class FieldBase<T, P = {}, S = {}> extends React.Component<Props
public onChange = (newValue?: T) => {
this.setState({ newValue }, () => this.props.onChange(newValue));
}
public componentDidUpdate(oldProps: Props<T>) {
const { value } = this.props;
if (oldProps.value === value) return;
this.setState({ oldValue: value, newValue: value });
}
public abstract renderInput(): React.ReactNode;
public getError(): string | null {
const { newValue } = this.state;

@ -2,6 +2,13 @@ import * as React from 'react';
import { FieldBase } from './base';
export class FieldString extends FieldBase<string | undefined> {
constructor(props: FieldString['props']) {
super(props);
}
public getInitialSubState({ value }: FieldString['props']): FieldString['state'] {
value = value || undefined;
return { oldValue: value, newValue: value };
}
public renderInput() {
return <input value={this.state.newValue || ''} onChange={this.onChangeEvent} />;
}

Loading…
Cancel
Save