Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Car 9534: new component "radioButtonGroupWithTitle" #573

Merged
merged 7 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions .storybook/stories/radioButtonGroupWithTitle.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import React, { FC } from "react"

import { action } from "@storybook/addon-actions"

import { StoryProps } from "./storyProps"
import RadioButtonGroupWithTitle, { RadioButtonGroupProps } from "../../src/components/radioButtonGroupWithTitle"

const onChange = () => action("onChange")

export default {
title: "Radio Button Group with Title",
component: RadioButtonGroupWithTitle,
args: {
storyName: "",
title: "Radio Group",
radioInputs:
[
{
name: "radioButtonGroupWithTitle",
value: "A",
checked: false,
onChange: (e) => {
onChange()(e)
},
renderLabel:() => "option 1",
disabled: true,
error: "test"
},
{
name: "radioButtonGroupWithTitle",
value: "B",
checked: false,
onChange: (e) => {
onChange()(e)
},
renderLabel:() => "option 2"
},
{
name: "radioButtonGroupWithTitle",
value: "C",
checked: false,
onChange: (e) => {
onChange()(e)
},
renderLabel:() => "option 3"
}
]
},
argTypes: {
storyName: {
table: {
disable: true,
},
},
},
}

interface Props extends StoryProps<JSX.Element>, RadioButtonGroupProps {}

const Template: FC<Props> = (args) => {
return (
<>
<div className="text-2xl mb-20">{args.storyName}</div>
<RadioButtonGroupWithTitle
{...args}
/>
</>

)
}

export const Standard = Template.bind({})
Standard.args = {
storyName: "Standard",
}

export const Disabled = Template.bind({})
Disabled.args = {
storyName: "Disabled",
disabled: true,
}

export const Required = Template.bind({})
Required.args = {
storyName: "Required",
required: true,
}

export const WithErrorMessage = Template.bind({})
WithErrorMessage.args = {
storyName: "With error message",
error: "Error message",
}
47 changes: 47 additions & 0 deletions src/components/radioButtonGroupWithTitle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React, { FC, ReactElement } from "react"

import RadioButton, { RadioButtonProps } from "./radioButton"
import WithValidationError from "./fieldHelpers/withValidationError"
import Label from "./fieldHelpers/label"

type RadioInputProps = Omit<RadioButtonProps, "error" | "disabled">

interface Props {
title: string
required?: boolean
disabled?: boolean
radioInputs: RadioInputProps[]
error?: string
}

const RadioButtonGroup: FC<Props> = ({
title,
required = false,
disabled = false,
radioInputs,
error,
}): ReactElement => {
const renderInputs = (hasError) => (
<div className="w-12/12">
<Label fieldName={title} required={required} />
<div className="flex">
{radioInputs.map((radioInput, index) => (
<RadioButton
key={index}
{...radioInput}
disabled={disabled}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason why it's not part of the radioInput object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's because I think we should be able to set it up globally to disable every RadioButtons at once

error={hasError}
/>
))}
</div>
</div>
)
return (
<WithValidationError error={error}>
{(hasError) => renderInputs(hasError)}
</WithValidationError>
)
}

export default RadioButtonGroup
export { Props as RadioButtonGroupProps }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need this export somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we need it for the stories

2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Slider from "./components/slider"
import Select from "./components/select"
import RangeSelect from "./components/rangeSelect"
import RangeInput from "./components/rangeInput"
import RadioButtonGroupWithTitle from "./components/radioButtonGroupWithTitle"
import RadioButton from "./components/radioButton"
import PreviewCard from "./components/previewCard/index"
import Pill from "./components/pill"
Expand Down Expand Up @@ -68,6 +69,7 @@ export {
Select,
Checkbox,
RadioButton,
RadioButtonGroupWithTitle,
WithLabel,
WithValidationError,
useModal,
Expand Down