2020-06-10 13:03:00 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2020-06-10 13:03:00 +01:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2020-06-10 13:49:52 +01:00
|
|
|
|
2024-09-09 14:57:16 +01:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2020-06-10 13:03:00 +01:00
|
|
|
*/
|
|
|
|
|
|
2024-11-13 20:39:17 +05:30
|
|
|
import React, { Ref } from "react";
|
2020-06-10 13:03:00 +01:00
|
|
|
import classnames from "classnames";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2020-06-10 13:03:00 +01:00
|
|
|
interface IProps extends React.InputHTMLAttributes<HTMLInputElement> {
|
2024-11-13 20:39:17 +05:30
|
|
|
inputRef?: Ref<HTMLInputElement>;
|
2020-07-03 19:27:45 +01:00
|
|
|
outlined?: boolean;
|
2021-07-16 22:27:31 +01:00
|
|
|
// If true (default), the children will be contained within a <label> element
|
2021-07-16 22:35:53 +01:00
|
|
|
// If false, they'll be in a div. Putting interactive components that have labels
|
|
|
|
|
// themselves in labels can cause strange bugs like https://github.com/vector-im/element-web/issues/18031
|
2021-07-16 22:27:31 +01:00
|
|
|
childrenInLabel?: boolean;
|
2020-06-10 13:03:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IState {}
|
|
|
|
|
|
|
|
|
|
export default class StyledRadioButton extends React.PureComponent<IProps, IState> {
|
|
|
|
|
public static readonly defaultProps = {
|
|
|
|
|
className: "",
|
2021-07-16 22:27:31 +01:00
|
|
|
childrenInLabel: true,
|
2020-06-18 14:32:43 +01:00
|
|
|
};
|
2020-06-10 13:03:00 +01:00
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2021-12-17 11:08:56 -06:00
|
|
|
const { children, className, disabled, outlined, childrenInLabel, inputRef, ...otherProps } = this.props;
|
2020-06-11 12:27:09 +01:00
|
|
|
const _className = classnames("mx_StyledRadioButton", className, {
|
2021-11-04 12:57:38 +00:00
|
|
|
mx_StyledRadioButton_disabled: disabled,
|
|
|
|
|
mx_StyledRadioButton_enabled: !disabled,
|
|
|
|
|
mx_StyledRadioButton_checked: this.props.checked,
|
|
|
|
|
mx_StyledRadioButton_outlined: outlined,
|
2020-06-11 12:27:09 +01:00
|
|
|
});
|
2021-07-16 22:27:31 +01:00
|
|
|
|
|
|
|
|
const radioButton = (
|
|
|
|
|
<React.Fragment>
|
2021-12-17 11:08:56 -06:00
|
|
|
<input
|
|
|
|
|
// Pass through the ref - used for keyboard shortcut access to some buttons
|
|
|
|
|
ref={inputRef}
|
|
|
|
|
type="radio"
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
{...otherProps}
|
|
|
|
|
/>
|
2021-07-19 22:43:11 +01:00
|
|
|
{/* Used to render the radio button circle */}
|
2020-07-03 19:27:45 +01:00
|
|
|
<div>
|
|
|
|
|
<div />
|
|
|
|
|
</div>
|
2021-07-16 22:27:31 +01:00
|
|
|
</React.Fragment>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (childrenInLabel) {
|
|
|
|
|
return (
|
|
|
|
|
<label className={_className}>
|
2021-07-19 22:43:11 +01:00
|
|
|
{radioButton}
|
2021-11-04 12:57:38 +00:00
|
|
|
<div className="mx_StyledRadioButton_content">{children}</div>
|
|
|
|
|
<div className="mx_StyledRadioButton_spacer" />
|
2021-07-16 22:27:31 +01:00
|
|
|
</label>
|
2022-12-12 12:24:14 +01:00
|
|
|
);
|
2021-11-04 12:57:38 +00:00
|
|
|
} else {
|
|
|
|
|
return (
|
|
|
|
|
<div className={_className}>
|
|
|
|
|
<label className="mx_StyledRadioButton_innerLabel">{radioButton}</label>
|
|
|
|
|
<div className="mx_StyledRadioButton_content">{children}</div>
|
|
|
|
|
<div className="mx_StyledRadioButton_spacer" />
|
2021-07-16 22:27:31 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-06-10 13:03:00 +01:00
|
|
|
}
|
|
|
|
|
}
|