Création du RadioInpu

pull/206/head
mama 2018-02-07 15:09:42 +01:00 committed by Laurent Bossavit
parent bb13843c88
commit e637f08766
3 changed files with 161 additions and 5 deletions

View File

@ -4,7 +4,7 @@ import { FormDecorator } from './FormDecorator'
import { answer, answered } from './userAnswerButtonStyle'
import HoverDecorator from '../HoverDecorator'
import Explicable from './Explicable'
import { pipe, split, reverse, reduce, is } from 'ramda'
import { is } from 'ramda'
import SendButton from './SendButton'
/* Ceci est une saisie de type "radio" : l'utilisateur choisit une réponse dans une liste, ou une liste de listes.
Les données @choices sont un arbre de type:

View File

@ -0,0 +1,143 @@
import React, { Component } from 'react'
import { FormDecorator } from './FormDecorator'
import { answer, answered } from './userAnswerButtonStyle'
import HoverDecorator from '../HoverDecorator'
import Explicable from './Explicable'
import { is } from 'ramda'
import SendButton from './SendButton'
@FormDecorator('radioInput')
export default class Question extends Component {
render() {
let { choices, submit, themeColours, meta: { pristine } } = this.props
let choiceElements = is(Array)(choices)
? this.renderBinaryQuestion()
: this.renderChildren(choices)
return (
<>
{choiceElements}
<SendButton
{...{
disabled: pristine,
themeColours,
error: false,
submit
}}
/>
</>
)
}
renderBinaryQuestion() {
let {
input, // vient de redux-form
submit,
choices,
setFormValue,
themeColours
} = this.props
return (
<ul>
{choices.map(({ value, label }) => (
<RadioLabel
key={value}
{...{ value, label, input, submit, themeColours, setFormValue }}
/>
))}
</ul>
)
}
renderChildren(choices) {
let {
input, // vient de redux-form
submit,
setFormValue,
themeColours
} = this.props,
{ name } = input,
// seront stockées ainsi dans le state :
// [parent object path]: dotted name relative to parent
relativeDottedName = radioDottedName =>
radioDottedName.split(name + ' . ')[1]
return (
<ul>
{choices.canGiveUp && (
<li key="aucun" className="variantLeaf aucun">
<RadioLabel
{...{
value: 'non',
label: 'Aucun',
input,
submit,
themeColours,
dottedName: null,
setFormValue
}}
/>
</li>
)}
{choices.children &&
choices.children.map(
({ name, title, dottedName, children }) =>
children ? (
<li key={name} className="variant">
<div>{title}</div>
{this.renderChildren({ children })}
</li>
) : (
<li key={name} className="variantLeaf">
<RadioLabel
{...{
value: relativeDottedName(dottedName),
label: title,
dottedName,
input,
submit,
themeColours,
setFormValue
}}
/>
</li>
)
)}
</ul>
)
}
}
let RadioLabel = props => (
<Explicable dottedName={props.dottedName}>
<RadioLabelContent {...props} />
</Explicable>
)
@HoverDecorator
class RadioLabelContent extends Component {
click = value => () => {
if (this.props.input.value == value) this.props.submit('dblClick')
}
render() {
let { value, label, input, hover, themeColours } = this.props,
// value = when(is(Object), prop('value'))(choice),
labelStyle = Object.assign(
value === input.value || hover
? answered(themeColours)
: answer(themeColours),
value === '_' ? { fontWeight: 'bold' } : null
)
return (
<label key={value} style={labelStyle} className="radio">
{label}
<input
type="radio"
{...input}
onClick={this.click(value)}
value={value}
checked={value === input.value ? 'checked' : ''}
/>
</label>
)
}
}

View File

@ -23,6 +23,7 @@ import Input from 'Components/conversation/Input'
import Select from 'Components/conversation/select/Select'
import SelectAtmp from 'Components/conversation/select/SelectTauxRisque'
import formValueTypes from 'Components/conversation/formValueTypes'
import RadioInput from '../components/conversation/RadioInput'
import {
findRuleByDottedName,
@ -172,16 +173,28 @@ export let getInputComponent = ({ unfolded }) => (
Else just display the Input component.
*/
if (inversion)
return (
<RadioInput
{...{
...commonProps,
valueType: formValueTypes[rule.format],
suggestions: rule.suggestions,
inversion,
inverted: dottedName !== fieldName,
fieldTitle: findRuleByDottedName(rules, fieldName).title,
fieldName
}}
/>
)
return (
<Input
{...{
...commonProps,
valueType: formValueTypes[rule.format],
suggestions: rule.suggestions,
inversion,
fieldTitle: findRuleByDottedName(rules, fieldName).title,
fieldName,
inverted: dottedName !== fieldName
fieldName
}}
/>
)