You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-desktop/ts/components/basic/SessionRadio.tsx

85 lines
1.8 KiB
TypeScript

import React, { ChangeEvent } from 'react';
import styled, { CSSProperties } from 'styled-components';
import { Flex } from '../basic/Flex';
// tslint:disable: react-unused-props-and-state
5 years ago
type Props = {
5 years ago
label: string;
value: string;
5 years ago
active: boolean;
inputName?: string;
onClick?: (value: string) => void;
};
const StyledInput = styled.input`
opacity: 0;
position: absolute;
cursor: pointer;
width: calc(var(--filled-size) + var(--outline-offset));
height: calc(var(--filled-size) + var(--outline-offset));
:checked + label:before,
:hover + label:before {
background: var(--color-accent);
}
`;
const StyledLabel = styled.label`
cursor: pointer;
:before {
content: '';
display: inline-block;
margin-inline-end: var(--filled-size);
border-radius: 100%;
transition: var(--default-duration);
padding: calc(var(--filled-size) / 2);
outline-offset: 2px;
outline: var(--color-text) solid 1px;
border: none;
margin-top: var(--filled-size);
:hover {
background: var(--color-accent);
}
}
`;
5 years ago
export const SessionRadio = (props: Props) => {
const { label, inputName, value, active, onClick } = props;
function clickHandler(e: ChangeEvent<any>) {
if (onClick) {
// let something else catch the event if our click handler is not set
e.stopPropagation();
onClick?.(value);
}
5 years ago
}
return (
<Flex
container={true}
padding="0 0 5px"
style={
{
'--filled-size': '15px',
} as CSSProperties
}
>
<StyledInput
type="radio"
name={inputName || ''}
value={value}
aria-checked={active}
checked={active}
onChange={clickHandler}
/>
<StyledLabel role="button" onClick={clickHandler}>
{label}
</StyledLabel>
</Flex>
);
};