Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add types to RadioTile #15984

Merged
merged 3 commits into from Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions .all-contributorsrc
Expand Up @@ -1434,6 +1434,15 @@
"contributions": [
"doc"
]
},
{
"login": "cuppajoey",
"name": "Joseph Schultz",
"avatar_url": "https://avatars.githubusercontent.com/u/14837881?v=4",
"profile": "https://cuppajoey.com/",
"contributions": [
"code"
]
}
],
"commitConvention": "none"
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -278,6 +278,7 @@ check out our [Contributing Guide](/.github/CONTRIBUTING.md) and our
<tr>
<td align="center"><a href="https://github.com/ggdawson"><img src="https://avatars.githubusercontent.com/u/37080130?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Garrett Dawson</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=ggdawson" title="Code">💻</a> <a href="https://github.com/carbon-design-system/carbon/commits?author=ggdawson" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/dedanade"><img src="https://avatars.githubusercontent.com/u/66811981?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Daniel Adebonojo</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=dedanade" title="Documentation">📖</a></td>
<td align="center"><a href="https://cuppajoey.com/"><img src="https://avatars.githubusercontent.com/u/14837881?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Joseph Schultz</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=cuppajoey" title="Code">💻</a></td>
</tr>
</table>

Expand Down
Expand Up @@ -21,12 +21,73 @@ import { noopFn } from '../../internal/noopFn';
import { Text } from '../Text';
import { useFeatureFlag } from '../FeatureFlags';

export interface RadioTileProps {
/**
* Specify whether the `RadioTile` should be checked.
*/
checked?: boolean;

/**
* The `RadioTile` content.
*/
children?: React.ReactNode;

/**
* Provide an optional `className` to be applied to the underlying `<label>`.
*/
className?: string;

/**
* Specify whether the `RadioTile` should be disabled.
*/
disabled?: boolean;

/**
* Provide a unique id for the underlying `<input>`.
*/
id?: string;

/**
* `true` to use the light version. For use on $ui-01 backgrounds only.
cuppajoey marked this conversation as resolved.
Show resolved Hide resolved
* Don't use this to make tile background color the same as the container background color.
*
* @deprecated This prop is no longer needed and has been deprecated in v11 in favor of the new Layer component. It will be removed in the next major release.
*/
light?: boolean;

/**
* Provide a `name` for the underlying `<input>`.
*/
name?: string;

/**
* Provide an optional `onChange` hook that is called each time the value of
* the underlying `<input>` changes.
*/
onChange?: (
value: string | number,
name: string | undefined,
event:
| React.ChangeEvent<HTMLInputElement>
| React.KeyboardEvent<HTMLInputElement>
) => void;

/**
* Specify the tab index of the underlying `<input>`.
*/
tabIndex?: number;

/**
* Specify the value of the underlying `<input>`.
*/
value: string | number;
}

const RadioTile = React.forwardRef(function RadioTile(
{
children,
className: customClassName,
disabled,
// eslint-disable-next-line no-unused-vars
light,
checked,
name,
Expand All @@ -35,8 +96,8 @@ const RadioTile = React.forwardRef(function RadioTile(
onChange = noopFn,
tabIndex = 0,
...rest
},
ref
}: RadioTileProps,
ref: React.Ref<HTMLInputElement>
) {
const prefix = usePrefix();
const inputId = useFallbackId(id);
Expand All @@ -63,11 +124,15 @@ const RadioTile = React.forwardRef(function RadioTile(
}
}

function handleOnChange(evt) {
function handleOnChange(
evt:
| React.ChangeEvent<HTMLInputElement>
| React.KeyboardEvent<HTMLInputElement>
) {
onChange(value, name, evt);
}

function handleOnKeyDown(evt) {
function handleOnKeyDown(evt: React.KeyboardEvent<HTMLInputElement>) {
if (matches(evt, [keys.Enter, keys.Space])) {
evt.preventDefault();
onChange(value, name, evt);
Expand All @@ -82,9 +147,9 @@ const RadioTile = React.forwardRef(function RadioTile(
disabled={disabled}
id={inputId}
name={name}
onChange={!disabled ? handleOnChange : null}
onKeyDown={!disabled ? handleOnKeyDown : null}
tabIndex={!disabled ? tabIndex : null}
onChange={!disabled ? handleOnChange : undefined}
onKeyDown={!disabled ? handleOnKeyDown : undefined}
tabIndex={!disabled ? tabIndex : undefined}
type="radio"
value={value}
ref={ref}
Expand All @@ -99,27 +164,27 @@ const RadioTile = React.forwardRef(function RadioTile(

RadioTile.propTypes = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I took the liberty of updated these prop descriptions to better match the writing style of other components (e.g., TileGroup, Select, etc..). If you feel this is out of scope for this PR, I don't mind reverting these changes.

/**
* `true` if this tile should be selected.
* Specify whether the `RadioTile` should be checked.
*/
checked: PropTypes.bool,

/**
* The tile content.
* The `RadioTile` content.
*/
children: PropTypes.node,

/**
* The CSS class names.
* Provide an optional `className` to be applied to the underlying `<label>`.
*/
className: PropTypes.string,

/**
* Specify whether the RadioTile should be disabled
* Specify whether the `RadioTile` should be disabled.
*/
disabled: PropTypes.bool,

/**
* The ID of the `<input>`.
* Provide a unique id for the underlying `<input>`.
*/
id: PropTypes.string,

Expand All @@ -134,22 +199,23 @@ RadioTile.propTypes = {
),

/**
* The `name` of the `<input>`.
* Provide a `name` for the underlying `<input>`.
*/
name: PropTypes.string,

/**
* The handler of the massaged `change` event on the `<input>`.
* Provide an optional `onChange` hook that is called each time the value of
* the underlying `<input>` changes.
*/
onChange: PropTypes.func,

/**
* Specify the tab index of the wrapper element
* Specify the tab index of the underlying `<input>`.
*/
tabIndex: PropTypes.number,

/**
* The `value` of the `<input>`.
* Specify the value of the underlying `<input>`.
*/
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
};
Expand Down
Expand Up @@ -5,7 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/

import RadioTile from './RadioTile';
import RadioTile, { RadioTileProps } from './RadioTile';

export default RadioTile;
export { RadioTile };

export type { RadioTileProps };
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I noticed that many of the recently added types don't export their interface. I'm not sure if there is a good reason not to, but I decided to include it.

This matches the original implementation from DefinitelyTyped and the current export for Checkbox