123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- /*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- import styled from '@emotion/styled';
- import classNames from 'classnames';
- import React from 'react';
- import tw from 'twin.macro';
- import { themeBorder, themeColor } from '../helpers/theme';
-
- type AllowedRadioButtonAttributes = Pick<
- React.InputHTMLAttributes<HTMLInputElement>,
- 'aria-label' | 'autoFocus' | 'id' | 'name' | 'style' | 'title' | 'type'
- >;
-
- interface Props extends AllowedRadioButtonAttributes {
- checked: boolean;
- children?: React.ReactNode;
- className?: string;
- disabled?: boolean;
- onCheck: (value: string) => void;
- value: string;
- }
-
- export default function RadioButton({
- checked,
- children,
- className,
- disabled,
- onCheck,
- value,
- ...htmlProps
- }: Props) {
- const handleChange = () => {
- if (!disabled) {
- onCheck(value);
- }
- };
-
- return (
- <label className={classNames('sw-flex sw-items-center', className)}>
- <RadioButtonStyled
- aria-disabled={disabled}
- checked={checked}
- disabled={disabled}
- onChange={handleChange}
- type="radio"
- value={value}
- {...htmlProps}
- />
- {children}
- </label>
- );
- }
-
- export const RadioButtonStyled = styled.input`
- appearance: none; //disables native style
- border: ${themeBorder('default', 'radioBorder')};
-
- ${tw`sw-w-4 sw-min-w-4 sw-h-4 sw-min-h-4`}
- ${tw`sw-p-1 sw-mr-2`}
- ${tw`sw-inline-block`}
- ${tw`sw-box-border`}
- ${tw`sw-rounded-pill`}
-
- &:hover {
- background: ${themeColor('radioHover')};
- }
-
- &:focus,
- &:focus-visible {
- background: ${themeColor('radioHover')};
- border: ${themeBorder('default', 'radioFocusBorder')};
- outline: ${themeBorder('focus', 'radioFocusOutline')};
- }
-
- &:focus:checked,
- &:focus-visible:checked,
- &:hover:checked,
- &:checked {
- // Color cannot be used with multiple backgrounds, only image is allowed
- background-image: linear-gradient(to right, ${themeColor('radio')}, ${themeColor('radio')}),
- linear-gradient(to right, ${themeColor('radioChecked')}, ${themeColor('radioChecked')});
- background-clip: content-box, padding-box;
- border: ${themeBorder('default', 'radioBorder')};
- }
-
- &:disabled {
- background: ${themeColor('radioDisabledBackground')};
- border: ${themeBorder('default', 'radioDisabledBorder')};
- background-clip: unset;
-
- ${tw`sw-cursor-not-allowed`}
-
- &:checked {
- background-image: linear-gradient(
- to right,
- ${themeColor('radioDisabled')},
- ${themeColor('radioDisabled')}
- ),
- linear-gradient(
- to right,
- ${themeColor('radioDisabledBackground')},
- ${themeColor('radioDisabledBackground')}
- );
- background-clip: content-box, padding-box;
- border: ${themeBorder('default', 'radioDisabledBorder')};
- }
- }
- `;
|