Skip to content

Commit

Permalink
Add isDefault to new front-end and reorganize files
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed Apr 29, 2024
1 parent bb21d7c commit 2840a82
Show file tree
Hide file tree
Showing 23 changed files with 905 additions and 646 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React, { useState, useEffect } from 'react'
import ReactSelect from 'react-select'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import { isNil } from 'lodash'

const AutocompleteInput = ({ value, options, disabled, isDefault, updateValue }) => {
const selectOptions = options.map(option => ({
value: option.id,
label: option.text
}))

const [inputValue, setInputValue] = useState('')
useEffect(() => {
setInputValue(selectOptions.find((selectOption) => selectOption.value == value.option))
}, [value.id, value.option])

const handleChange = (selectedOption) => {
updateValue(value, {
option: isNil(selectedOption) ? null : selectedOption.value
})
}

const classnames = classNames({
'react-select': true,
'default': isDefault
})

return (
<ReactSelect
classNamePrefix="react-select"
className={classnames}
isClearable={true}
options={selectOptions}
value={inputValue}
onChange={(option) => {
setInputValue(option)
handleChange(option)
}}
isDisabled={disabled}
/>
)
}

AutocompleteInput.propTypes = {
value: PropTypes.object.isRequired,
options: PropTypes.array.isRequired,
disabled: PropTypes.bool,
isDefault: PropTypes.bool,
updateValue: PropTypes.func.isRequired
}

export default AutocompleteInput
Original file line number Diff line number Diff line change
@@ -1,72 +1,44 @@
import React, { useState, useEffect } from 'react'
import React from 'react'
import PropTypes from 'prop-types'
import ReactSelect from 'react-select'
import { isNil } from 'lodash'

import { isDefaultValue } from '../../../../utils/value'

import QuestionAddValue from '../QuestionAddValue'
import QuestionRemoveValue from '../QuestionRemoveValue'

const AutocompleteInput = ({ value, options, disabled, updateValue }) => {
const selectOptions = options.map(option => ({
value: option.id,
label: option.text
}))

const [inputValue, setInputValue] = useState('')
useEffect(() => {
setInputValue(selectOptions.find((selectOption) => selectOption.value == value.option))
}, [value.id, value.option])

const handleChange = (selectedOption) => {
updateValue(value, {
option: isNil(selectedOption) ? null : selectedOption.value
})
}
import DefaultBadge from './common/DefaultBadge'

return (
<ReactSelect
classNamePrefix="react-select"
className="react-select"
isClearable={true}
options={selectOptions}
value={inputValue}
onChange={(option) => {
setInputValue(option)
handleChange(option)
}}
isDisabled={disabled}
/>
)
}

AutocompleteInput.propTypes = {
value: PropTypes.object.isRequired,
options: PropTypes.array.isRequired,
disabled: PropTypes.bool,
updateValue: PropTypes.func.isRequired
}
import AutocompleteInput from './AutocompleteInput'

const AutocompleteWidget = ({ question, values, currentSet, disabled, createValue, updateValue, deleteValue }) => {
return (
<div className="interview-collection">
{
values.map((value, valueIndex) => (
<div key={valueIndex} className="interview-input">
<div className="interview-input-options">
{
(question.is_collection || values.length > 1) && (
<QuestionRemoveValue value={value} deleteValue={deleteValue} />
)
}
values.map((value, valueIndex) => {
const isDefault = isDefaultValue(question, value)

return (
<div key={valueIndex} className="interview-input">
<div className="interview-input-options">
{
isDefault && <DefaultBadge />
}
{
(question.is_collection || values.length > 1) && (
<QuestionRemoveValue value={value} deleteValue={deleteValue} />
)
}
</div>
<AutocompleteInput
value={value}
options={question.options}
disabled={disabled}
isDefault={isDefault}
updateValue={updateValue}
/>
</div>
<AutocompleteInput
value={value}
options={question.options}
disabled={disabled}
updateValue={updateValue}
/>
</div>
))
)
})
}
{
question.is_collection && (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import React from 'react'
import PropTypes from 'prop-types'
import { useDebouncedCallback } from 'use-debounce'
import { isEmpty, isNil } from 'lodash'

import AdditionalTextInput from './common/AdditionalTextInput'
import AdditionalTextareaInput from './common/AdditionalTextareaInput'

const CheckboxInput = ({ value, option, disabled, onCreate, onUpdate, onDelete }) => {

const checked = !isNil(value)

const handleChange = () => {
if (checked) {
onDelete(value)
} else {
onCreate(option)
}
}

const handleAdditionalValueChange = useDebouncedCallback((value, option, additionalValue) => {
if (checked) {
onUpdate(value, { text: additionalValue, option: option.id })
} else {
onCreate(option, additionalValue)
}
}, 500)

return (
<div className="checkbox">
<label>
<input
type="checkbox"
checked={checked}
disabled={disabled}
onChange={() => handleChange()}
/>
<span>{option.text}</span>
{
isEmpty(option.additional_input) && (
<span className="text-muted">{option.help}</span>
)
}
{
option.additional_input == 'text' && (
<>
<span>:</span>
{' '}
<AdditionalTextInput value={value} option={option} disabled={disabled} onChange={handleAdditionalValueChange} />
{' '}
<span className="text-muted">{option.help}</span>
</>
)
}
{
option.additional_input == 'textarea' && (
<>
<span>:</span>
{' '}
<AdditionalTextareaInput value={value} option={option} disabled={disabled} onChange={handleAdditionalValueChange} />
{' '}
<div className="text-muted">{option.help}</div>
</>
)
}
</label>
</div>
)
}

CheckboxInput.propTypes = {
value: PropTypes.object,
option: PropTypes.object,
disabled: PropTypes.bool,
onCreate: PropTypes.func.isRequired,
onUpdate: PropTypes.func.isRequired,
onDelete: PropTypes.func.isRequired
}

export default CheckboxInput
Original file line number Diff line number Diff line change
@@ -1,81 +1,8 @@
import React from 'react'
import PropTypes from 'prop-types'
import { useDebouncedCallback } from 'use-debounce'
import { isEmpty, isNil, maxBy } from 'lodash'
import { isNil, maxBy } from 'lodash'

import AdditionalTextInput from './common/AdditionalTextInput'
import AdditionalTextareaInput from './common/AdditionalTextareaInput'

const CheckboxInput = ({ value, option, disabled, onCreate, onUpdate, onDelete }) => {

const checked = !isNil(value)

const handleChange = () => {
if (checked) {
onDelete(value)
} else {
onCreate(option)
}
}

const handleAdditionalValueChange = useDebouncedCallback((value, option, additionalValue) => {
if (checked) {
onUpdate(value, { text: additionalValue, option: option.id })
} else {
onCreate(option, additionalValue)
}
}, 500)

return (
<div className="checkbox">
<label>
<input
type="checkbox"
checked={checked}
disabled={disabled}
onChange={() => handleChange()}
/>
<span>{option.text}</span>
{
isEmpty(option.additional_input) && (
<span className="text-muted">{option.help}</span>
)
}
{
option.additional_input == 'text' && (
<>
<span>:</span>
{' '}
<AdditionalTextInput value={value} option={option} disabled={disabled} onChange={handleAdditionalValueChange} />
{' '}
<span className="text-muted">{option.help}</span>
</>
)
}
{
option.additional_input == 'textarea' && (
<>
<span>:</span>
{' '}
<AdditionalTextareaInput value={value} option={option} disabled={disabled} onChange={handleAdditionalValueChange} />
{' '}
<div className="text-muted">{option.help}</div>
</>
)
}
</label>
</div>
)
}

CheckboxInput.propTypes = {
value: PropTypes.object,
option: PropTypes.object,
disabled: PropTypes.bool,
onCreate: PropTypes.func.isRequired,
onUpdate: PropTypes.func.isRequired,
onDelete: PropTypes.func.isRequired
}
import CheckboxInput from './CheckboxInput'

const CheckboxWidget = ({ question, values, currentSet, disabled, createValue, updateValue, deleteValue }) => {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React from 'react'
import PropTypes from 'prop-types'
import DatePicker from 'react-datepicker'
import classNames from 'classnames'
import { enGB, de, it, es, fr } from 'date-fns/locale'

import lang from 'rdmo/core/assets/js/utils/lang'

const DateInput = ({ value, disabled, isDefault, updateValue }) => {

const getLocale = () => {
switch (lang) {
case 'de':
return de
case 'it':
return it
case 'es':
return es
case 'fr':
return fr
default:
return enGB
}
}

const getDateFormat = () => {
switch (lang) {
case 'de':
return 'dd.MM.yyyy'
case 'it':
return 'dd/MM/yyyy'
case 'es':
return 'dd/MM/yyyy'
case 'fr':
return 'dd/MM/yyyy'
default:
return 'dd/MM/yyyy'
}
}

const handleChange = (date) => {
const text = date.toISOString().slice(0,10)
updateValue(value, { text })
}

const classnames = classNames({
'form-control': true,
'date-control': true,
'default': isDefault
})

return (
<DatePicker
className={classnames}
selected={value.text}
onChange={(date) => handleChange(date)}
locale={getLocale()}
dateFormat={getDateFormat()}
disabled={disabled}
/>
)
}

DateInput.propTypes = {
value: PropTypes.object.isRequired,
disabled: PropTypes.bool,
isDefault: PropTypes.bool,
updateValue: PropTypes.func.isRequired
}

export default DateInput
Loading

0 comments on commit 2840a82

Please sign in to comment.