-
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 a minimal SelectBoxes formio component
This component is to be used in the builder preview pane and to set the defaultValue for the SelectBoxes inside the builder edit form.
- Loading branch information
1 parent
99473b0
commit b22b7ab
Showing
3 changed files
with
136 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import {expect} from '@storybook/jest'; | ||
import {Meta, StoryObj} from '@storybook/react'; | ||
import {within} from '@storybook/testing-library'; | ||
|
||
import {withFormik} from '@/sb-decorators'; | ||
|
||
import SelectBoxes from './selectboxes'; | ||
|
||
export default { | ||
title: 'Formio/Components/SelectBoxes', | ||
component: SelectBoxes, | ||
decorators: [withFormik], | ||
parameters: { | ||
modal: {noModal: true}, | ||
formik: {initialValues: {'my-selectboxes': {}}}, | ||
}, | ||
args: { | ||
name: 'my-selectboxes', | ||
label: '', | ||
tooltip: '', | ||
options: [ | ||
{value: 'a', label: 'A'}, | ||
{value: 'b', label: 'B'}, | ||
], | ||
}, | ||
} as Meta<typeof SelectBoxes>; | ||
|
||
type Story = StoryObj<typeof SelectBoxes>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
label: 'Labeled selectboxes', | ||
}, | ||
}; | ||
|
||
export const WithInitialChecked: Story = { | ||
parameters: { | ||
formik: { | ||
initialValues: { | ||
'my-selectboxes': { | ||
b: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
args: { | ||
label: 'Labeled selectboxes', | ||
}, | ||
|
||
play: async ({canvasElement}) => { | ||
const canvas = within(canvasElement); | ||
|
||
await expect(canvas.getByLabelText('A')).not.toBeChecked(); | ||
await expect(canvas.getByLabelText('B')).toBeChecked(); | ||
}, | ||
}; | ||
|
||
export const WithToolTip: Story = { | ||
args: { | ||
label: 'With tooltip', | ||
tooltip: 'Hiya!', | ||
}, | ||
}; | ||
|
||
export const WithDescription: Story = { | ||
args: { | ||
label: 'With description', | ||
description: 'A description', | ||
}, | ||
}; | ||
|
||
export const WithErrors: Story = { | ||
args: { | ||
label: 'With errors', | ||
}, | ||
|
||
parameters: { | ||
formik: { | ||
initialValues: {'my-selectboxes': false}, | ||
initialErrors: {'my-selectboxes': 'Example error', 'other-field': 'Other error'}, | ||
}, | ||
}, | ||
|
||
play: async ({canvasElement}) => { | ||
const canvas = within(canvasElement); | ||
await expect(canvas.queryByText('Other error')).not.toBeInTheDocument(); | ||
await expect(canvas.queryByText('Example error')).toBeInTheDocument(); | ||
}, | ||
}; |
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,46 @@ | ||
import {ReactNode} from 'react'; | ||
|
||
import {CheckboxInput} from './checkbox'; | ||
import Component from './component'; | ||
import Description from './description'; | ||
|
||
export interface Option { | ||
value: string; | ||
label: ReactNode; | ||
} | ||
|
||
export interface SelectBoxesProps { | ||
name: string; | ||
options: Option[]; | ||
label?: React.ReactNode; | ||
required?: boolean; | ||
tooltip?: string; | ||
description?: string; | ||
} | ||
|
||
export const SelectBoxes: React.FC<SelectBoxesProps> = ({ | ||
name, | ||
options, | ||
label, | ||
required = false, | ||
tooltip = '', | ||
description = '', | ||
}) => { | ||
return ( | ||
<Component type="selectboxes" field={name} 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={`${name}.${value}`} label={label} /> | ||
</label> | ||
</div> | ||
))} | ||
</div> | ||
|
||
{description && <Description text={description} />} | ||
</Component> | ||
); | ||
}; | ||
|
||
export default SelectBoxes; |