Skip to content

Commit

Permalink
[PBNTR-373](Enable Kits Instead of Text for Radio label) (#3612)
Browse files Browse the repository at this point in the history
[PBNTR-373](https://runway.powerhrg.com/backlog_items/PBNTR-373)

This PR adds the ability to have a child wrapped around the `<Radio>`
kit. When this child is clicked. it will check off the radio button!


<img width="1121" alt="Screenshot 2024-08-26 at 11 30 37 AM"
src="https://github.com/user-attachments/assets/e15b0b27-8796-49a2-bf57-a9060bed2b53">



**How to test?** Steps to confirm the desired behavior:
1. Go to Radio kit and test the functionally!

#### Checklist:
- [ ] **LABELS** Add a label: `enhancement`, `bug`, `improvement`, `new
kit`, `deprecated`, or `breaking`. See [Changelog &
Labels](https://github.com/powerhome/playbook/wiki/Changelog-&-Labels)
for details.
- [ ] **DEPLOY** I have added the `milano` label to show I'm ready for a
review.
- [ ] **TESTS** I have added test coverage to my code.
  • Loading branch information
nickamantia authored Sep 6, 2024
1 parent 18795ba commit 7ea0e37
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 50 deletions.
150 changes: 100 additions & 50 deletions playbook/app/pb_kits/playbook/pb_radio/_radio.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
/*eslint-disable react/no-multi-comp, flowtype/space-before-type-colon */

import React, { forwardRef } from 'react'
import React, { forwardRef, isValidElement, useRef } from 'react'
import Body from '../pb_body/_body'
import Flex from '../pb_flex/_flex'
import classnames from 'classnames'
import { buildAriaProps, buildCss, buildDataProps, buildHtmlProps } from '../utilities/props'
import { globalProps, GlobalProps } from '../utilities/globalProps'

type RadioProps = {
aria?: {[key: string]: string},
aria?: { [key: string]: string },
alignment?: string,
checked?: boolean,
children?: React.ReactChild[] | React.ReactChild,
className?: string,
dark?: boolean,
data?: {[key: string]: string},
data?: { [key: string]: string },
disabled?: boolean,
error?: boolean,
htmlOptions?: {[key: string]: string | number | boolean | (() => void)},
htmlOptions?: { [key: string]: string | number | boolean | (() => void) },
id?: string,
label: string,
name?: string,
value?: string,
text?: string,
onChange: (event: React.FormEvent<HTMLInputElement> | null)=>void,
onChange: (event: React.FormEvent<HTMLInputElement> | null) => void,
} & GlobalProps

const Radio = ({
Expand All @@ -31,9 +30,9 @@ const Radio = ({
children,
className,
dark = false,
data = {},
disabled = false,
error = false,
data = {},
htmlOptions = {},
id,
label,
Expand All @@ -42,52 +41,103 @@ const Radio = ({
value = 'radio_text',
onChange = () => { void 0 },
...props
}: RadioProps, ref: any) => {
const ariaProps = buildAriaProps(aria)
const dataProps = buildDataProps(data)
const htmlProps = buildHtmlProps(htmlOptions)
}: RadioProps ) => {
const radioRef = useRef(null);

const ariaProps = buildAriaProps(aria);
const dataProps = buildDataProps(data);
const htmlProps = buildHtmlProps(htmlOptions);
const classes = classnames(
buildCss('pb_radio_kit', alignment ),
dark ? 'dark': null, error ? 'error': null,
buildCss('pb_radio_kit', alignment),
dark ? 'dark' : null,
error ? 'error' : null,
globalProps(props),
className)
className
);

const classesCustom = classnames(
dark ? 'dark' : null,
error ? 'error' : null,
globalProps(props),
className
);

const isCustomChild = children && isValidElement(children) && children.type !== 'input';

const displayRadio = (props: RadioProps & any) => {
if (children)
return (children)
else
return (
<input
disabled={disabled}
id={id}
name={name}
onChange={onChange}
ref={ref}
text={text}
type="radio"
value={value}
{...props}
/>
)}
if (isValidElement(children) && children.type === 'input') {
return children;
} else if (isCustomChild || !children) {
return (
<input
disabled={disabled}
id={id}
name={name}
onChange={onChange}
ref={radioRef}
text={text}
type="radio"
value={value}
{...props}
/>
);
}
};

const handleContainerClick = (event: React.MouseEvent<HTMLDivElement, MouseEvent> | undefined) => {
if (event) {
const target = event.target as HTMLElement;
if (
target.id === 'pb-radio-children-wrapper' ||
target.closest('#pb-radio-children-wrapper')
) {
radioRef.current?.click();
}
}
};

return (
<label
{...ariaProps}
{...dataProps}
{...htmlProps}
className={classes}
htmlFor={id}
>
<>{displayRadio(props)}</>
<span className="pb_radio_button" />
<Body
dark={dark}
status={error ? 'negative' : null}
text={label}
variant={null}
/>
</label>
)
}
isCustomChild ? (
<Flex
{...ariaProps}
{...dataProps}
{...htmlProps}
align='center'
className={classesCustom}
cursor='pointer'
htmlFor={id}
htmlOptions={{
onClick: ((event: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
handleContainerClick(event);
}) as unknown as () => void
}}
id="radio-container"
>
<label className={buildCss('pb_radio_kit', alignment)}>
<>{displayRadio(props)}</>
<span className="pb_radio_button" />
</label>
<div id="pb-radio-children-wrapper"> {children} </div>
</Flex>
) : (
<label
{...ariaProps}
{...dataProps}
{...htmlProps}
className={classes}
htmlFor={id}
>
<>{displayRadio(props)}</>
<span className="pb_radio_button" />
<Body
dark={dark}
status={error ? 'negative' : null}
text={label}
variant={null}
/>
</label>
)
);
};

export default forwardRef(Radio)
export default forwardRef(Radio);
56 changes: 56 additions & 0 deletions playbook/app/pb_kits/playbook/pb_radio/docs/_radio_children.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react'
import Radio from '../_radio'
import Select from '../../pb_select/_select'
import Typeahead from '../../pb_typeahead/_typeahead'
import Title from '../../pb_title/_title'

const RadioChildren = (props) => {


const options = [
{ label: 'Orange', value: 'Orange' },
{ label: 'Red', value: 'Red' },
{ label: 'Green', value: 'Green' },
{ label: 'Blue', value: 'Blue' },
]

return (
<div>
<Radio
label="Select"
name="Group1"
tabIndex={0}
value="Select"
{...props}
>
<Select
minWidth="xs"
options={options}
/>
</Radio>
<Radio
label="Typeahead"
name="Group1"
tabIndex={0}
value="Typeahead"
{...props}
>
<Typeahead
minWidth="xs"
options={options}
/>
</Radio>
<br />
<Radio
defaultChecked={false}
label="Typography"
name="Group1"
value="Typography"
{...props}
>
<Title text="Custom Typography" />
</Radio>
</div>
)
}
export default RadioChildren
1 change: 1 addition & 0 deletions playbook/app/pb_kits/playbook/pb_radio/docs/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ examples:
- radio_error: With Error
- radio_alignment: Alignment
- radio_disabled: Disabled
- radio_children: Children

swift:
- radio_default_swift: Default
Expand Down
1 change: 1 addition & 0 deletions playbook/app/pb_kits/playbook/pb_radio/docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { default as RadioCustom } from './_radio_custom.jsx'
export { default as RadioError } from './_radio_error.jsx'
export { default as RadioAlignment } from './_radio_alignment.jsx'
export { default as RadioDisabled } from './_radio_disabled.jsx'
export { default as RadioChildren } from './_radio_children.jsx'

0 comments on commit 7ea0e37

Please sign in to comment.