-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor field creation to FieldFactory.js
- Loading branch information
Showing
2 changed files
with
38 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import Field from './Field.js'; | ||
import TextField from './TextField.js'; | ||
import CheckboxField from './CheckboxField.js'; | ||
import DropdownField from './DropdownField.js'; | ||
import NotValidFieldException from '../Exceptions/NotValidFieldException.js'; | ||
|
||
export default class FieldFactory { | ||
constructor(){ } | ||
|
||
createFieldByElement(element){ | ||
switch (element.type) { | ||
case 'text': | ||
return new TextField(element.id); | ||
case 'email': | ||
return new TextField(element.id); | ||
case 'tel': | ||
return new TextField(element.id); | ||
case 'checkbox': | ||
return new CheckboxField(element.id); | ||
case 'select-one': | ||
return new DropdownField(element.id); | ||
case 'hidden': | ||
return null; | ||
default: | ||
try{ | ||
return new Field(element.id); | ||
}catch(e){ | ||
if(e instanceof NotValidFieldException) return null; | ||
else throw e; | ||
} | ||
} | ||
} | ||
} |