This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
180 additions
and
8 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
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 |
---|---|---|
@@ -1,8 +1,51 @@ | ||
import * as React from 'react'; | ||
import { RouteComponentProps } from 'react-router'; | ||
|
||
import MainViewContainer from './MainViewContainer'; | ||
import SchemaForm from './SchemaForm'; | ||
import { Field } from './SchemaForm/models'; | ||
|
||
|
||
|
||
interface FormViewRouterProps { | ||
formSlug: string; | ||
} | ||
|
||
interface FormViewState { | ||
title?: string; | ||
introductionText?: string; | ||
fields: Field[]; | ||
} | ||
|
||
|
||
export default class FormView extends React.Component<RouteComponentProps<FormViewRouterProps>, FormViewState> { | ||
state: FormViewState = { | ||
title: 'Dynaamine lomake höhöhö', | ||
introductionText: 'Lorem ipsum höhöhö sit amet.\n\nWith paragraphs!', | ||
fields: [ | ||
{ | ||
type: 'Input', | ||
name: 'foo', | ||
title: 'Foo', | ||
}, | ||
{ | ||
type: 'TextArea', | ||
name: 'bar', | ||
title: 'Bar', | ||
}, | ||
], | ||
}; | ||
|
||
export default class FormView extends React.Component<{}, {}> { | ||
render() { | ||
return <div>form view</div>; | ||
const { title, fields, introductionText } = this.state; | ||
|
||
return ( | ||
<MainViewContainer> | ||
{title ? <h1>{title}</h1> : null} | ||
{introductionText ? introductionText.split('\n\n').map((paragraph, index) => <p key={index}>{paragraph}</p>) : null} | ||
|
||
<SchemaForm fields={fields} layout="horizontal" /> | ||
</MainViewContainer> | ||
); | ||
} | ||
} |
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,3 @@ | ||
.MainViewContainer { | ||
padding-top: 2em; | ||
} |
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,12 @@ | ||
import React from 'react'; | ||
|
||
import Container from "reactstrap/lib/Container"; | ||
|
||
import './index.css'; | ||
|
||
|
||
export default class MainViewContainer extends React.Component<{}, {}> { | ||
render() { | ||
return <Container className='MainViewContainer'>{this.props.children}</Container>; | ||
} | ||
} |
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,78 @@ | ||
import React from 'react'; | ||
import { NamespacesConsumer } from 'react-i18next'; | ||
|
||
import Button from 'reactstrap/lib/Button'; | ||
import ButtonGroup from 'reactstrap/lib/ButtonGroup'; | ||
import Col from 'reactstrap/lib/Col'; | ||
import Form from 'reactstrap/lib/Form'; | ||
import FormGroup from 'reactstrap/lib/FormGroup'; | ||
import Input from 'reactstrap/lib/Input'; | ||
import Label from 'reactstrap/lib/Label'; | ||
|
||
import { Field, FieldType } from './models'; | ||
|
||
|
||
interface SchemaFormProps { | ||
fields: Field[]; | ||
layout?: 'vertical' | 'horizontal'; | ||
ns?: string[]; | ||
} | ||
|
||
|
||
interface SchemaFormState { } | ||
|
||
|
||
export default class SchemaForm extends React.PureComponent<SchemaFormProps, SchemaFormState> { | ||
static defaultNamespace = 'SchemaForm'; | ||
|
||
render() { | ||
const { layout } = this.props; | ||
const ns = (this.props.ns || []).concat(['SchemaForm']); | ||
|
||
return ( | ||
<NamespacesConsumer ns={ns}> | ||
{t => ( | ||
<Form className={layout === 'horizontal' ? 'form-horizontal' : ''}> | ||
{this.props.fields.map(this.renderField)} | ||
|
||
<ButtonGroup className="float-md-right"> | ||
<Button color="primary">{t('submit')}</Button> | ||
</ButtonGroup> | ||
</Form> | ||
)} | ||
</NamespacesConsumer> | ||
); | ||
} | ||
|
||
renderField = (field: Field) => { | ||
const { layout } = this.props; | ||
const { name, title, helpText } = field; | ||
|
||
switch (layout) { | ||
case 'horizontal': | ||
return ( | ||
<FormGroup key={name} className="row"> | ||
<Label for={name} className="col-md-3 col-form-label">{title}</Label> | ||
<Col md={9}>{this.renderInput(field)}</Col> | ||
</FormGroup> | ||
); | ||
case 'vertical': | ||
default: | ||
return ( | ||
<FormGroup key={name} className="row"> | ||
<Label for={name}>{title}</Label> | ||
{this.renderInput(field)} | ||
</FormGroup> | ||
); | ||
} | ||
} | ||
|
||
renderInput = (field: Field) => { | ||
switch (field.type) { | ||
case 'Input': | ||
return <Input name={field.name} />; | ||
case 'TextArea': | ||
return <Input type="textarea" name={field.name} rows={field.rows || 10} />; | ||
} | ||
} | ||
} |
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,23 @@ | ||
export type FieldType = 'Input' | 'TextArea'; | ||
|
||
interface BaseField { | ||
type: FieldType; | ||
name: string; | ||
title: string; | ||
helpText?: string; | ||
required?: boolean; | ||
} | ||
|
||
|
||
export interface Input extends BaseField { | ||
type: 'Input'; | ||
} | ||
|
||
|
||
export interface TextArea extends BaseField { | ||
type: 'TextArea'; | ||
rows?: number; | ||
cols?: number; | ||
} | ||
|
||
export type Field = Input | TextArea; |
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
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