-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [#52] Implement selectboxes preview component
- Loading branch information
1 parent
99473b0
commit 0a7285d
Showing
5 changed files
with
184 additions
and
29 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
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,10 @@ | ||
// import EditForm from './edit'; | ||
// import validationSchema from './edit-validation'; | ||
import Preview from './preview'; | ||
|
||
export default { | ||
// edit: EditForm, | ||
// editSchema: validationSchema, | ||
preview: Preview, | ||
// defaultValue: '', | ||
}; |
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,63 @@ | ||
import {SelectboxesComponentSchema} from '@open-formulieren/types'; | ||
import {Option} from '@open-formulieren/types/lib/formio/common'; | ||
import {useIntl} from 'react-intl'; | ||
|
||
import {Component, Description} from '@/components/formio'; | ||
import {CheckboxInput} from '@/components/formio/checkbox'; | ||
|
||
import {ComponentPreviewProps} from '../types'; | ||
|
||
// FIXME: this union does not seem to be properly inferred :( | ||
const checkIsManualOptions = ( | ||
component: SelectboxesComponentSchema | ||
): component is SelectboxesComponentSchema & {values: Option[]} => { | ||
return component.openForms.dataSrc === 'manual'; | ||
}; | ||
|
||
/** | ||
* Show a formio selectboxes component preview. | ||
* | ||
* NOTE: for the time being, this is rendered in the default Formio bootstrap style, | ||
* however at some point this should use the components of | ||
* @open-formulieren/formio-renderer instead for a more accurate preview. | ||
*/ | ||
const Preview: React.FC<ComponentPreviewProps<SelectboxesComponentSchema>> = ({component}) => { | ||
const intl = useIntl(); | ||
const {key, label, description, tooltip, validate} = component; | ||
const {required = false} = validate || {}; | ||
const isManualOptions = checkIsManualOptions(component); | ||
const options = isManualOptions | ||
? component.values | ||
: [ | ||
{ | ||
value: 'itemsExpression', | ||
label: intl.formatMessage( | ||
{ | ||
description: 'Selectboxes dummy option for itemsExpression', | ||
defaultMessage: 'Options from expression: <code>{expression}</code>', | ||
}, | ||
{ | ||
expression: JSON.stringify(component.openForms.itemsExpression), | ||
code: chunks => <code>{chunks}</code>, | ||
} | ||
), | ||
}, | ||
]; | ||
return ( | ||
<Component type="selectboxes" field={key} label={label} tooltip={tooltip} required={required}> | ||
<div className="form-radio radio"> | ||
{options.map(({value, label}, index) => ( | ||
<div key={`option-${value}-${index}`} className="form-check"> | ||
<label className="form-check-label"> | ||
<CheckboxInput name={`${key}.${value}`} label={label} /> | ||
</label> | ||
</div> | ||
))} | ||
</div> | ||
|
||
{description && <Description text={description} />} | ||
</Component> | ||
); | ||
}; | ||
|
||
export default Preview; |