Skip to content

Commit

Permalink
Add defaultValue option to Control
Browse files Browse the repository at this point in the history
  • Loading branch information
unexge committed Jan 15, 2020
1 parent 834f4a8 commit 645df57
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function isValidationRulesChanged(
export interface Props {
name: string;
children: (control: Control) => JSX.Element;
defaultValue: any;

[validator: string]: any;
};
Expand All @@ -54,7 +55,12 @@ class Control extends Component<Props, State> {

static propTypes = {
name: PropTypes.string.isRequired,
children: PropTypes.func.isRequired
children: PropTypes.func.isRequired,
defaultValue: PropTypes.any,
};

static defaultProps = {
defaultValue: ''
};

constructor(props: Props, context: Context) {
Expand All @@ -67,7 +73,7 @@ class Control extends Component<Props, State> {
this.markAsUntouched = this.markAsUntouched.bind(this);
}

get value(): any { return this.context.form.getValue(this.props.name) || ''; }
get value(): any { return this.context.form.getValue(this.props.name) || this.props.defaultValue; }
get form(): Form { return this.context.form; }
get status(): Status { return this.context.form.getStatus(this.props.name); }
get errors(): Errors { return this.context.form.getErrors(this.props.name); }
Expand Down

0 comments on commit 645df57

Please sign in to comment.