material-you-react/src/primitive-components/radio/radio.tsx

55 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-02-01 00:58:19 +03:00
'use client';
2024-02-02 13:39:02 +03:00
import { bool, string } from 'prop-types';
2024-02-01 15:23:59 +03:00
import { RadioProps } from './radio.types';
2024-02-01 00:58:19 +03:00
import { RippleArea } from '../ripple/ripple-area';
2024-02-01 15:23:59 +03:00
import { forwardRef, useRef, useState } from 'react';
2024-02-01 00:58:19 +03:00
import useRippleEffect from '../ripple/hooks/useRippleEffect';
import { CheckBoxLayout } from '../checkbox-layout/check-box-layout';
/**
* Radio component
** description
*/
export const Radio = forwardRef<HTMLInputElement, RadioProps>(
({ centralRipple, ...props }, ref) => {
2024-02-01 15:23:59 +03:00
const [isActive, setIsActive] = useState<boolean>(false),
ripplesRef = useRef(null),
events = useRippleEffect(ripplesRef, setIsActive);
2024-02-01 00:58:19 +03:00
2024-02-01 15:23:59 +03:00
const classes =
`m3 m3-radio ${isActive === true ? 'visible' : ''}`.trimEnd();
2024-02-01 00:58:19 +03:00
2024-02-01 15:23:59 +03:00
return (
<div {...events} className={classes}>
<CheckBoxLayout {...props} ref={ref} type={'radio'} />
<span className={'m3 m3-radio-state-layer'} />
<svg height={'20px'} viewBox={'0 0 20 20'} width={'20px'}>
<circle
className={'m3-radio-outline'}
cx={'50%'}
cy={'50%'}
/>
<circle
className={'m3-radio-state'}
cx={'50%'}
cy={'50%'}
/>
</svg>
<RippleArea
callback={setIsActive}
central={centralRipple}
className={'m3-checkbox-ripple-layer'}
ref={ripplesRef}
/>
</div>
2024-02-01 15:23:59 +03:00
);
},
);
Radio.propTypes = {
children: string,
centralRipple: bool,
};