Skip to content

Commit

Permalink
feat(radio): init import radio from EDSCandidates
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin Lee committed Aug 24, 2022
1 parent 00ca3b5 commit f56bdc4
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 130 deletions.
File renamed without changes.
20 changes: 20 additions & 0 deletions src/components/Radio/Radio.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { BADGE } from '@geometricpanda/storybook-addon-badges';
import { StoryObj, Meta } from '@storybook/react';
import React from 'react';

import { Radio } from './Radio';

export default {
title: 'Atoms/Forms/Radio',
component: Radio,
args: {
label: 'Option 1',
},
parameters: {
badges: [BADGE.BETA],
},
} as Meta<Args>;

type Args = React.ComponentProps<typeof Radio>;

export const Default: StoryObj<Args> = {};
File renamed without changes.
62 changes: 62 additions & 0 deletions src/components/Radio/Radio.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import clsx from 'clsx';
import React from 'react';
import { useUID } from 'react-uid';
import styles from './Radio.module.css';
import { RadioButton, RadioButtonProps } from '../RadioButton/RadioButton';

export type RadioProps = {
/**
* HTML id attribute. If not passed, this component
* will generate an id to use for accessibility.
*/
id?: string;
/**
* Visible text label for the checkbox.
*/
label?: React.ReactNode;
} & RadioButtonProps;

/**
* ```ts
* import Radio from 'v2/core/EDSCandidates/Radio';
* ```
* ```ts
* import {Label, RadioButton} from 'v2/core/EDSCandidates/Radio';
* ```
*
* Radio button and commonly a visual label.
*
* @example
* <Radio label="Option 1" />
*/
export const Radio = ({
id,
label,
className,
disabled,
...radioProps
}: RadioProps) => {
if (
process.env.NODE_ENV !== 'production' &&
!label &&
!('aria-label' in radioProps)
) {
throw new Error('You must provide a visible label or aria-label');
}
const generatedId = useUID();
const radioId = id || generatedId;

const componentClassName = clsx(styles['radio'], className);

const labelClassName = clsx(
styles['radio__label'],
disabled && styles['radio__label--disabled'],
);

return (
<div className={componentClassName}>
<RadioButton disabled={disabled} id={radioId} {...radioProps} />
<label htmlFor={radioId}>{label}</label>
</div>
);
};
File renamed without changes.
12 changes: 12 additions & 0 deletions src/components/RadioButton/RadioButton.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@import '../../design-tokens/mixins.css';

/*------------------------------------*\
# RADIO BUTTON
\*------------------------------------*/

/**
* 1)
*/
.radio-button {
display: flex;
}
19 changes: 19 additions & 0 deletions src/components/RadioButton/RadioButton.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { BADGE } from '@geometricpanda/storybook-addon-badges';
import { StoryObj, Meta } from '@storybook/react';
import React from 'react';

import { RadioButton } from './RadioButton';

export default {
title: 'PleaseUpdateThisToADifferentFolder/RadioButton',
component: RadioButton,
parameters: {
badges: [BADGE.BETA],
},
} as Meta<Args>;

type Args = React.ComponentProps<typeof RadioButton>;

export const Default: StoryObj<Args> = {
args: {},
};
6 changes: 6 additions & 0 deletions src/components/RadioButton/RadioButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { generateSnapshots } from '@chanzuckerberg/story-utils';
import * as stories from './RadioButton.stories';

describe('<RadioButton />', () => {
generateSnapshots(stories);
});
72 changes: 72 additions & 0 deletions src/components/RadioButton/RadioButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import clsx from 'clsx';
import React from 'react';
import styles from './RadioButton.module.css';

export type RadioButtonProps = {
/**
* Additional classnames passed in for styling
*/
className?: string;
/**
* Label visible only to screen readers for cases where there is no visible label.
* Prefer using `label` over this where possible.
*/
'aria-label'?: string;
/**
* Name for the group the button is in.
* Each button in the group should be given the same name.
*/
name?: string;
/**
* Whether radio button starts checked. Defaults to false.
*/
checked?: boolean;
/**
* Whether radio is disabled.
*/
disabled?: boolean;
/**
* Value assigned to the radio button.
*/
value?: string;
/**
* Callback when radio option is selected.
* Usually requires value prop to identify which radio button has been selected.
*/
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
} & React.InputHTMLAttributes<HTMLInputElement>;

/**
* <RadioButton aria-label="Less preferred, circle part of radio button only" />
*
* BETA: This component is still a work in progress and is subject to change.
*
* ```ts
* import {RadioButton} from "@chanzuckerberg/eds";
* ```
*
* The circle part of the radio button.
* For flexibility regarding styling and label placement.
* Should be associated with a <label>, or less preferred, aria-label.
*
* Style outer circle with "[data-bootstrap-override='radio'].radio"
* and inner circle with "[data-bootstrap-override='radio'].radio::before"
*
* @example
* <div>
* <RadioButton />
* <label>Label to describe the radio button</label>
* </div>
*
* @example
*/
export const RadioButton = ({ className, ...props }: RadioButtonProps) => {
return (
<input
className={clsx(styles.radio, className)}
data-bootstrap-override="radio"
type="radio"
{...props}
/>
);
};
1 change: 1 addition & 0 deletions src/components/RadioButton/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { RadioButton as default } from './RadioButton';
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export { default as PageHeader } from './components/PageHeader';
export { default as PageLevelBanner } from './components/PageLevelBanner';
export { default as Panel } from './components/Panel';
export { default as ProjectCard } from './components/ProjectCard';
export { default as Radio } from './components/Radio';
export { default as RadioButton } from './components/RadioButton';
export { default as Score } from './components/Score';
export { default as Section } from './components/Section';
export { default as StackedBlock } from './components/StackedBlock';
Expand Down
31 changes: 0 additions & 31 deletions src/upcoming-components/Radio/Radio.stories.tsx

This file was deleted.

98 changes: 0 additions & 98 deletions src/upcoming-components/Radio/Radio.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion src/upcoming-components/RadioFieldItem/RadioFieldItem.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import clsx from 'clsx';
import React, { ChangeEventHandler, ReactNode } from 'react';
import { useUID } from 'react-uid';
import Radio from '../Radio';
import { Radio } from '../../components/Radio/Radio';
import styles from '../RadioField/RadioField.module.css';

export interface Props {
Expand Down

0 comments on commit f56bdc4

Please sign in to comment.