mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-07-05 06:20:09 +05:30
39 lines
840 B
JavaScript
39 lines
840 B
JavaScript
|
export default class Form {
|
||
|
fields = {};
|
||
|
|
||
|
/**
|
||
|
* Connects form with React's component
|
||
|
*
|
||
|
* Usage:
|
||
|
* <input {...this.form.bindField('foo')} type="text" />
|
||
|
*
|
||
|
* @param {string} name - the name of field
|
||
|
*
|
||
|
* @return {Object} ref and name props for component
|
||
|
*/
|
||
|
bindField(name) {
|
||
|
return {
|
||
|
name,
|
||
|
ref: (el) => {
|
||
|
this.fields[name] = el;
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
focus(fieldId) {
|
||
|
if (!this.fields[fieldId]) {
|
||
|
throw new Error(`The field with an id ${fieldId} does not exists`);
|
||
|
}
|
||
|
|
||
|
this.fields[fieldId].focus();
|
||
|
}
|
||
|
|
||
|
serialize() {
|
||
|
return Object.keys(this.fields).reduce((acc, key) => {
|
||
|
acc[key] = this.fields[key].getValue();
|
||
|
|
||
|
return acc;
|
||
|
}, {});
|
||
|
}
|
||
|
}
|