import _ from "lodash" import React from "react" import { Choice, Item } from "../formDesign" import * as formUtils from "../formUtils" import { InnerFormContext } from "../formContext" import LargeRadio from "../LargeRadio" export interface LikertAnswerComponentProps { choices: Choice[] items: Choice[] onAnswerChange: any /** See answer format */ answer: any data: any formCtx: InnerFormContext } export default class LikertAnswerComponent extends React.Component { focus() { // Nothing to focus return null } handleValueChange = (choice: Choice, item: any) => { let newValue if (this.props.answer.value != null) { newValue = _.clone(this.props.answer.value) } else { newValue = {} } if (newValue[item.id] === choice.id) { delete newValue[item.id] } else { newValue[item.id] = choice.id } return this.props.onAnswerChange(_.extend({}, this.props.answer, { value: newValue })) } renderChoice(item: Choice, choice: Choice) { let value const id = `${item.id}:${choice.id}` if (this.props.answer.value != null) { value = this.props.answer.value[item.id] } else { value = null } return ( {/* id is used for testing */} {formUtils.localizeString(choice.label, this.props.formCtx.locale)} ) } // IN CASE WE DECIDE TO DISPLAY THE CHOICES AT THE TOP (INSTEAD OF REPEATING THEM) //renderChoiceLabel: (choice) -> // R 'td', key: "label#{choice.id}", // formUtils.localizeString(choice.label, @context.locale) renderItem(item: Choice) { return ( {formUtils.localizeString(item.label, this.props.formCtx.locale)} {item.hint ? (
{formUtils.localizeString(item.hint, this.props.formCtx.locale)}
) : undefined} {_.map(this.props.choices, (choice) => this.renderChoice(item, choice))} ) } render() { return ( {/* IN CASE WE DECIDE TO DISPLAY THE CHOICES AT THE TOP (INSTEAD OF REPEATING THEM) */} {/*R 'thead', null,*/} {/* R 'tr', null,*/} {/* R('td'),*/} {/* _.map @props.choices, (choice) =>*/} {/* @renderChoiceLabel(choice)*/} {_.map(this.props.items, (item) => this.renderItem(item))}
) } }