Skip to content

Commit

Permalink
feat(form): support deafaultValue
Browse files Browse the repository at this point in the history
  • Loading branch information
ShanaMaid committed Jan 23, 2019
1 parent 4a477dc commit 7ca7def
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## [0.8.12](https://github.com/Yoshino-UI/Yoshino/compare/v0.8.11...v0.8.12) (2019-01-22)


### Features

* **form:** check() value -> any ([fc91dfa](https://github.com/Yoshino-UI/Yoshino/commit/fc91dfa))



## [0.8.11](https://github.com/Yoshino-UI/Yoshino/compare/v0.8.10...v0.8.11) (2019-01-22)


Expand Down
9 changes: 8 additions & 1 deletion components/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export interface IFormProps extends IBaseComponent {
* 值为空时默认提示文案
*/
requiredMsg?: string;
/**
* 默认值
*/
defaultValue?: any;
}

export interface IFormState {
Expand Down Expand Up @@ -57,6 +61,7 @@ export class Form extends Component<IFormProps, IFormState> {
},
rt: false,
requiredMsg: '${name}不能为空',
defaultValue: {},
};

static childContextTypes = {
Expand All @@ -69,10 +74,11 @@ export class Form extends Component<IFormProps, IFormState> {
labelCol: PropTypes.object,
rt: PropTypes.bool,
requiredMsg: PropTypes.string,
defaultValue: PropTypes.object,
};

getChildContext() {
const { row, wrapperCol, labelCol, rt, requiredMsg } = this.props;
const { row, wrapperCol, labelCol, rt, requiredMsg, defaultValue } = this.props;
return {
onChange: this.onChange,
onDelete: this.onDelete,
Expand All @@ -83,6 +89,7 @@ export class Form extends Component<IFormProps, IFormState> {
labelCol,
rt,
requiredMsg,
defaultValue,
};
}

Expand Down
10 changes: 9 additions & 1 deletion components/Form/Validator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class Validator extends Component<IValidatorProps, IValidatorState> {
labelCol: PropTypes.object,
rt: PropTypes.bool,
requiredMsg: PropTypes.string,
defaultValue: PropTypes.object,
};

static defaultProps = {
Expand All @@ -101,7 +102,14 @@ export class Validator extends Component<IValidatorProps, IValidatorState> {
const {
name, value,
} = this.props;
this.context.onChange(name, value);
const parentValue = this.context.defaultValue[name];
if (parentValue !== undefined) {
this.setState({value: parentValue}, () => {
this.context.onChange(name, parentValue);
});
} else {
this.context.onChange(name, value);
}
this.context.onPushChecker(name, this.onCheck);
}

Expand Down
6 changes: 6 additions & 0 deletions docs/pages/components/Form/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export default [
intro: '`Validator`中`required`为`true`时,`message`默认值,`name`对应`Validator`中`name`',
type: 'string',
defaultValue: '${name}不能为空',
},
{
props: 'defaultValue',
intro: '表单默认初始值',
type: 'object',
defaultValue: '{}',
}
]
},
Expand Down
4 changes: 4 additions & 0 deletions docs/pages/components/Form/demo/formDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export default class extends React.Component {
this.form = v;
}
}}
defaultValue={{
nickname: 'shana',
phone: 11111
}}
>
<Validator
name='nickname'
Expand Down

0 comments on commit 7ca7def

Please sign in to comment.