2024-02-01 00:58:19 +03:00
|
|
|
'use client';
|
|
|
|
|
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';
|
2024-02-02 00:12:58 +03:00
|
|
|
import { bool, oneOf, string } from 'prop-types';
|
|
|
|
import { LabelPlacement } from '../checkbox-layout/checkbox-layout.types';
|
2024-02-01 00:58:19 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Radio component
|
|
|
|
** description
|
|
|
|
*/
|
|
|
|
|
2024-02-02 00:12:58 +03:00
|
|
|
export const Radio = forwardRef<HTMLInputElement, RadioProps & LabelPlacement>(
|
|
|
|
({ centralRipple, children, labelPlacement = 'right', ...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 =
|
2024-02-01 23:30:13 +03:00
|
|
|
`m3 m3-radio ${isActive === true ? 'visible' : ''}`.trimEnd();
|
2024-02-01 00:58:19 +03:00
|
|
|
|
2024-02-01 15:23:59 +03:00
|
|
|
return (
|
2024-02-01 23:30:13 +03:00
|
|
|
<div {...events} className={classes}>
|
2024-02-02 00:12:58 +03:00
|
|
|
{children && labelPlacement === 'left' && (
|
|
|
|
<label htmlFor={props.id}>{children}</label>
|
|
|
|
)}
|
|
|
|
<span>
|
|
|
|
<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}
|
2024-02-01 23:30:13 +03:00
|
|
|
/>
|
2024-02-02 00:12:58 +03:00
|
|
|
</span>
|
|
|
|
{children && labelPlacement === 'right' && (
|
|
|
|
<label htmlFor={props.id}>{children}</label>
|
|
|
|
)}
|
2024-02-01 23:30:13 +03:00
|
|
|
</div>
|
2024-02-01 15:23:59 +03:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2024-02-02 00:12:58 +03:00
|
|
|
|
|
|
|
Radio.propTypes = {
|
|
|
|
children: string,
|
|
|
|
centralRipple: bool,
|
|
|
|
labelPlacement: oneOf(['left', 'right']),
|
|
|
|
};
|