Ivan - Add language selector using FormField [skip ci]

This commit is contained in:
ivan 2016-10-13 17:58:48 -03:00
parent b6f2fa0b81
commit cc4858715d
6 changed files with 35 additions and 12 deletions

View File

@ -16,7 +16,7 @@ const languageCodes = Object.values(codeLanguages).concat(['en']);
class LanguageSelector extends React.Component { class LanguageSelector extends React.Component {
static propTypes = { static propTypes = {
language: React.PropTypes.oneOf(languageCodes) value: React.PropTypes.oneOf(languageCodes)
}; };
render() { render() {
@ -30,7 +30,8 @@ class LanguageSelector extends React.Component {
className: this.getClass(), className: this.getClass(),
items: this.getLanguageList(), items: this.getLanguageList(),
selectedIndex: this.getSelectedIndex(), selectedIndex: this.getSelectedIndex(),
onChange: this.changeLanguage.bind(this) onChange: this.changeLanguage.bind(this),
size: this.props.size
}; };
} }
@ -60,7 +61,7 @@ class LanguageSelector extends React.Component {
} }
getPropLanguage() { getPropLanguage() {
let language = this.props.language; let language = this.props.value;
if (language === 'en') { if (language === 'en') {
language = 'us'; language = 'us';
@ -77,7 +78,11 @@ class LanguageSelector extends React.Component {
} }
if (this.props.onChange) { if (this.props.onChange) {
this.props.onChange(language); this.props.onChange({
target: {
value: language
}
});
} }
} }
} }

View File

@ -8,6 +8,7 @@ import API from 'lib-app/api-call';
import AdminDataActions from 'actions/admin-data-actions'; import AdminDataActions from 'actions/admin-data-actions';
import AreYouSure from 'app-components/are-you-sure'; import AreYouSure from 'app-components/are-you-sure';
import LanguageSelector from 'app-components/language-selector';
import Icon from 'core-components/icon'; import Icon from 'core-components/icon';
import Button from 'core-components/button'; import Button from 'core-components/button';
@ -28,7 +29,11 @@ class AdminPanelCustomResponses extends React.Component {
selectedIndex: -1, selectedIndex: -1,
edited: false, edited: false,
errors: {}, errors: {},
form: {} form: {
title: '',
content: RichTextEditor.createEmptyValue(),
language: 'en'
}
}; };
componentDidMount() { componentDidMount() {
@ -54,7 +59,14 @@ class AdminPanelCustomResponses extends React.Component {
</div> </div>
<div className="col-md-9"> <div className="col-md-9">
<Form {...this.getFormProps()}> <Form {...this.getFormProps()}>
<FormField label={i18n('TITLE')} name="title" validation="TITLE" required fieldProps={{size: 'large'}}/> <div className="row">
<div className="col-md-7">
<FormField label={i18n('TITLE')} name="title" validation="TITLE" required fieldProps={{size: 'large'}}/>
</div>
<div className="col-md-5">
<FormField label={i18n('LANGUAGE')} name="language" field="input" decorator={LanguageSelector} fieldProps={{size: 'medium'}} />
</div>
</div>
<FormField label={i18n('CONTENT')} name="content" validation="TEXT_AREA" required field="textarea" /> <FormField label={i18n('CONTENT')} name="content" validation="TEXT_AREA" required field="textarea" />
<div className="admin-panel-custom-responses__actions"> <div className="admin-panel-custom-responses__actions">
<div className="admin-panel-custom-responses__save-button"> <div className="admin-panel-custom-responses__save-button">
@ -154,7 +166,6 @@ class AdminPanelCustomResponses extends React.Component {
API.call({ API.call({
path: '/ticket/add-custom-response', path: '/ticket/add-custom-response',
data: { data: {
id: this.state.selectedIndex,
name: form.title, name: form.title,
content: form.content, content: form.content,
language: form.language language: form.language
@ -188,6 +199,7 @@ class AdminPanelCustomResponses extends React.Component {
form.title = (this.props.items[index] && this.props.items[index].name) || ''; form.title = (this.props.items[index] && this.props.items[index].name) || '';
form.content = RichTextEditor.createValueFromString((this.props.items[index] && this.props.items[index].content) || '', 'html'); form.content = RichTextEditor.createValueFromString((this.props.items[index] && this.props.items[index].content) || '', 'html');
form.language = (this.props.items[index] && this.props.items[index].language) || 'en';
this.setState({ this.setState({
selectedIndex: index, selectedIndex: index,

View File

@ -43,13 +43,13 @@ class MainLayoutHeader extends React.Component {
getLanguageSelectorProps() { getLanguageSelectorProps() {
return { return {
className: 'main-layout-header__languages', className: 'main-layout-header__languages',
language: this.props.config.language, value: this.props.config.language,
onChange: this.changeLanguage.bind(this) onChange: this.changeLanguage.bind(this)
}; };
} }
changeLanguage(language) { changeLanguage(event) {
this.props.dispatch(ConfigActions.changeLanguage(language)); this.props.dispatch(ConfigActions.changeLanguage(event.target.value));
} }
} }

View File

@ -14,6 +14,7 @@ class FormField extends React.Component {
}; };
static propTypes = { static propTypes = {
decorator: React.PropTypes.func,
validation: React.PropTypes.string, validation: React.PropTypes.string,
onChange: React.PropTypes.func, onChange: React.PropTypes.func,
onBlur: React.PropTypes.func, onBlur: React.PropTypes.func,
@ -62,13 +63,17 @@ class FormField extends React.Component {
} }
renderField() { renderField() {
const Field = { let Field = {
'input': Input, 'input': Input,
'textarea': TextEditor, 'textarea': TextEditor,
'select': DropDown, 'select': DropDown,
'checkbox': Checkbox 'checkbox': Checkbox
}[this.props.field]; }[this.props.field];
if(this.props.decorator) {
Field = this.props.decorator;
}
return <Field {...this.getFieldProps()} />; return <Field {...this.getFieldProps()} />;
} }

View File

@ -59,6 +59,7 @@ export default {
'SAVE': 'Save', 'SAVE': 'Save',
'DISCARD_CHANGES': 'Discard changes', 'DISCARD_CHANGES': 'Discard changes',
'DELETE': 'Delete', 'DELETE': 'Delete',
'LANGUAGE': 'Language',
//VIEW DESCRIPTIONS //VIEW DESCRIPTIONS
'CREATE_TICKET_DESCRIPTION': 'This is a form for creating tickets. Fill the form and send us your issues/doubts/suggestions. Our support system will answer it as soon as possible.', 'CREATE_TICKET_DESCRIPTION': 'This is a form for creating tickets. Fill the form and send us your issues/doubts/suggestions. Our support system will answer it as soon as possible.',

View File

@ -8,7 +8,7 @@ class AlphaNumericValidator extends Validator {
} }
validate(value, form) { validate(value, form) {
let alphaMatch = /^[ A-Za-z0-9_@./#&+-]*$/; let alphaMatch = /^[ A-Za-z0-9_@./#&+-äöüÄÖÜß]*$/;
if (!alphaMatch.test(value)) return this.getError(this.errorKey); if (!alphaMatch.test(value)) return this.getError(this.errorKey);
} }