{"id":406662,"date":"2024-06-29T19:04:32","date_gmt":"2024-06-29T19:04:32","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=406662"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=406662","title":{"rendered":"<span>\u0414\u0435\u043b\u0430\u0435\u043c \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 RadioGroup \u0432 99 \u0441\u0442\u0440\u043e\u043a \u0434\u043b\u044f React<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0438\u0448\u0435\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u044b\u0439 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 RadioGroup \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0434\u043b\u044f React \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u043f\u0430\u0440\u043e\u0447\u043a\u0443 unit \u0442\u0435\u0441\u0442\u043e\u0432 \u043d\u0430 Jest.<\/p>\n<h3>\u041f\u043b\u0430\u043d \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439<\/h3>\n<p>\u041e\u0431\u0449\u0438\u0439 \u043f\u043b\u0430\u043d \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 6 \u044d\u0442\u0430\u043f\u043e\u0432:<\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u043d\u044f\u0442\u044c, \u0447\u0442\u043e \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 Option<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 RadioGroup<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0451 \u0432 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0435 \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c<\/p>\n<\/li>\n<li>\n<p>\u0421\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u0432\u0432\u043e\u0434\u0430 \u0441 \u043a\u043b\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u044b<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043a\u0440\u044b\u0442\u044c \u0442\u0435\u0441\u0442\u0430\u043c\u0438<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u043e\u0435\u0445\u0430\u043b\u0438!<\/p>\n<h3>\u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h3>\n<p>\u041d\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u0430\u044f \u0440\u0430\u0434\u0438\u043e \u0433\u0440\u0443\u043f\u043f\u0430 \u0434\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432. \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u0430\u044f \u0444\u043e\u0440\u043c\u0430 \u0438 \u0432 \u043d\u0435\u0439 \u043d\u0443\u0436\u043d\u0430 &#171;\u0432\u044b\u0431\u0438\u0440\u0430\u043b\u043a\u0430&#187; \u043f\u0435\u0440\u0438\u043e\u0434\u0430, \u0434\u043b\u044f \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0438 \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0438\/\u043a\u043e\u043d\u0442\u0435\u043d\u0442\u0430 \u0437\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u043f\u0435\u0440\u0438\u043e\u0434 \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<p>\u0421\u0434\u0435\u043b\u0430\u0435\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0432 \u0432\u0438\u0434\u0435 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u043b\u0430\u0448\u043a\u0438, \u0441 \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432 \u0432\u0438\u0434\u0435 \u043a\u043d\u043e\u043f\u043e\u043a. \u0412 \u0446\u0435\u043b\u043e\u043c \u043d\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c ui \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u0442\u0430\u043a, \u043a\u0430\u043a \u0432\u0430\u043c \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c\u0441\u044f. Feel free to edit, \u043a\u0430\u043a \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0441\u044f.<\/p>\n<p>\u041f\u043e \u0438\u0442\u043e\u0433\u0443 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442. \u0414\u0435\u043c\u043e: <a href=\"https:\/\/codesandbox.io\/s\/robzare-custom-radio-jte0kj\" rel=\"noopener noreferrer nofollow\">codesandbox.custom-radio<\/a><\/p>\n<p>PS: \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0444\u043e\u0440\u043c\u0430\u043c\u0438 \u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0435\u0439. \u0420\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u043e\u0447\u0435\u043d\u044c \u043c\u043d\u043e\u0433\u043e, \u0441\u0442\u043e\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0433\u0443\u0433\u043b\u0438\u0442\u044c). \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u0434\u0438\u043d \u0438\u0437 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u044f \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/habr.com\/ru\/articles\/732690\/\" rel=\"noopener noreferrer nofollow\">\u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0444\u043e\u0440\u043c \u0431\u0435\u0437 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439<\/a>.<\/p>\n<p>\u041f\u043e\u0435\u0445\u0430\u043b\u0438!<\/p>\n<h3>\u041f\u0438\u0448\u0435\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 Option<\/h3>\n<h4>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b<\/h4>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043d\u0430\u0448\u0435\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0432\u044b\u0431\u043e\u0440\u0430. \u041e\u043d \u0431\u0443\u0434\u0435\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u0435\u043d \u0438 \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c 2 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430:<\/p>\n<pre><code class=\"typescript\"> type OptionType = {   value: string;   title: string; }; <\/code><\/pre>\n<p>\u0421\u0430\u043c \u0436\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 Options \u0434\u043e\u043b\u0436\u0435\u043d \u0443\u043c\u0435\u0442\u044c \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0435\u0449\u0435\u0439:<\/p>\n<ul>\n<li>\n<p><strong>\u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c<\/strong> \u043e\u0434\u0438\u043d \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0432\u044b\u0431\u043e\u0440\u0430<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e\u043c\u0435\u0447\u0430\u0442\u044c <strong>\u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0439<\/strong> \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u043c \u043e\u0442 \u0434\u0440\u0443\u0433\u0438\u0445<\/p>\n<\/li>\n<li>\n<p>\u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c <strong>onChange<\/strong> \u043f\u0440\u0438 \u0432\u044b\u0431\u043e\u0440\u0435 \u043a\u043b\u0438\u043a\u0435 \u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0435 \u043d\u0430 typescript \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 Option \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"typescript\">type OptionProps = {   value: OptionType['value'];   title: OptionType['title'];   selected: OptionType['value'];   groupName: string;   onChange?: (value: string) => void; }; <\/code><\/pre>\n<h4>\u0412\u0435\u0440\u0441\u0442\u043a\u0430<\/h4>\n<p>\u0414\u043b\u044f \u0441\u0442\u0438\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c css modules \u0434\u043b\u044f \u0441\u0442\u0438\u043b\u0438\u0437\u0430\u0446\u0438\u0438 (\u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043b\u0435\u0436\u0438\u0442 react-create-app \u0441 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u043c ts, \u0442\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 css modules \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438).<\/p>\n<p>\u041d\u0430\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0438\u043b\u0438 \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c:<\/p>\n<pre><code class=\"typescript\">  import Styles from '.\/index.module.css';   ...   &lt;div className={Styles.group}>...&lt;\/div> <\/code><\/pre>\n<p>\u0421\u0430\u043c \u0436\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e:<\/p>\n<pre><code class=\"typescript\">const Option = (props: OptionProps) => {     const {       value,       title,       selected,       groupName,       onChange     } = props;    const handleChange = () => onChange?.(value);    const inputId = `${groupName}_radio_item_with_value__${value}`;   const isChecked = value === selected;    return (     &lt;div       className={Styles.item}       key={value}       data-checked={isChecked}     >       &lt;input         className={Styles.input}         type=\"radio\"         name={groupName}         id={inputId}         value={value}         onChange={handleChange}       \/>       &lt;label className={Styles.label} htmlFor={inputId}>         {title}       &lt;\/label>     &lt;\/div>   ); }; <\/code><\/pre>\n<p>\u041f\u0440\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 data-checked \u0432 <strong>true<\/strong> \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435 &#171;\u043f\u0440\u043e\u043c\u0435\u0447\u0430\u0442\u044c \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u043c \u043e\u0442 \u0434\u0440\u0443\u0433\u0438\u0445&#187;. \u0417\u0430\u0442\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u043c title \u0438 \u0432\u0435\u0448\u0430\u0435\u043c handleChange \u043d\u0430 onChange \u043d\u0430\u0448\u0435\u0433\u043e \u0438\u043d\u043f\u0443\u0442\u0430.<\/p>\n<h3>\u041f\u0438\u0448\u0435\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 RadioGroup<\/h3>\n<h4>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b<\/h4>\n<p>\u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 RadioGroup \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a options, \u043a\u043e\u043b\u043b\u0431\u044d\u043a onChange \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430. \u041d\u0443 \u0438 \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u0438\u043c\u0435\u043d\u043d\u043e <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/input\/radio#defining_a_radio_group\" rel=\"noopener noreferrer nofollow\">Radio group<\/a>, \u0430 \u043d\u0435 \u0447\u0442\u043e-\u0442\u043e \u0434\u0440\u0443\u0433\u043e\u0435, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u043c\u044f \u044d\u0442\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u044b.<\/p>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 4\u0445 \u043f\u0440\u043e\u043f\u0441\u043e\u0432:<\/p>\n<pre><code class=\"typescript\">type RadioGroupProps = {   name: string;   options: OptionType[];   selected: OptionType['value'];   onChange?: (value: string) => void; }; <\/code><\/pre>\n<h4>\u0412\u0451\u0440\u0441\u0442\u043a\u0430<\/h4>\n<p>\u0412 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u043e\u0442\u0440\u0435\u043d\u0434\u0435\u0440\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a option \u0438 \u043e\u0431\u044a\u044f\u0432\u0438\u0442\u044c handleChange \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430. \u041f\u043b\u044e\u0441 \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0431\u0435\u0440\u043d\u0451\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0432 <a href=\"https:\/\/react.dev\/reference\/react\/memo\" rel=\"noopener noreferrer nofollow\">React.memo<\/a>.<\/p>\n<pre><code class=\"typescript\">const RadioGroup = (props: RadioGroupProps) => {   const { name, options, selected, onChange } = props;    const handleChange = (value: string) => onChange?.(value);    return (     &lt;div className={Styles.group}>       {options.map(({ value, title }) => (         &lt;Option           key={value}           groupName={name}           value={value}           title={title}           selected={selected}           onChange={handleChange}         \/>       ))}     &lt;\/div>   ); };  export default React.memo(RadioGroup); <\/code><\/pre>\n<h3>\u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0451 \u0432 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0435 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c<\/h3>\n<pre><code class=\"typescript\">import { useState } from \"react\";  import options from \".\/components\/radio\/options.json\"; import Radio from \".\/components\/radio\";  import \".\/styles.css\";  export default function App() {   const [period, setPeriod] = useState(\"\");    const handlePeriodChange = (val: string) => {     setPeriod(val);   };    return (     &lt;div className=\"App\">       &lt;h1>Custom RadioGroup component example&lt;\/h1>       &lt;h3>\u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043f\u0435\u0440\u0438\u043e\u0434&lt;\/h3>       &lt;div className=\"Radio\">         &lt;Radio           selected={period}           name=\"radio\"           onChange={handlePeriodChange}           options={options}         \/>       &lt;\/div>     &lt;\/div>   ); } <\/code><\/pre>\n<h3>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0432\u0432\u043e\u0434\u0430 \u0441 \u043a\u043b\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u044b<\/h3>\n<p>\u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 RadioGroup \u0441 \u043a\u043b\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u044b, \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0430\u0448 Option \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442. \u0410 \u0438\u043c\u0435\u043d\u043d\u043e:<\/p>\n<ul>\n<li>\n<p>\u0432 <strong>Option<\/strong> \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043b\u0443\u0448\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u043d\u0430\u0436\u0430\u0442\u0438\u044f, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043b\u0438 \u043d\u0430\u0448 option \u0432 \u0444\u043e\u043a\u0443\u0441\u0435 \u0438\u043b\u0438 \u043d\u0435\u0442. \u0415\u0441\u043b\u0438 option \u0432 \u0444\u043e\u043a\u0443\u0441\u0435, \u0442\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a <strong>onClick<\/strong><\/p>\n<\/li>\n<li>\n<p>\u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u043a\u043e\u043b\u0434\u043e\u0432\u0430\u0442\u044c \u0441 tabindex.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u043a\u0438:<\/p>\n<pre><code class=\"typescript\">import { useEffect, useRef } from 'react';  const Option = (props: OptionProps) => {   const optionRef = useRef&lt;HTMLDivElement>(null);   ...   useEffect(() => {     const option = optionRef.current;      if (!option) return;      const handleEnterKeyDown = (event: KeyboardEvent) => {        if ((document.activeElement === option) &amp;&amp; event.key === 'Enter') {         onChange?.(value);       }     }      option.addEventListener('keydown', handleEnterKeyDown);      return () => {       option.removeEventListener('keydown', handleEnterKeyDown);     };   }, [value, onChange]);    return (     &lt;div       className={Styles.item}       { \/* rest props *\/ }       ref={optionRef}       tabIndex={0}     >       &lt;input className={Styles.input} { \/* rest props *\/ } tabIndex={-1} \/>       ...     &lt;\/div>   ); } <\/code><\/pre>\n<p>\u041c\u044b \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u043c input \u0438\u0437 \u043e\u0431\u0445\u043e\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043a\u043b\u0430\u0432\u0438\u0448\u0438 tab, \u043f\u0440\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044f tabindex \u0432 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0418 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0432 \u044d\u0442\u043e\u0442 \u043e\u0431\u0445\u043e\u0434 div \u043e\u0431\u0451\u0440\u0442\u043a\u0443 \u0432\u0441\u0435\u0433\u043e \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0433\u043e option.<\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430 \u043f\u0440\u0438 \u0444\u043e\u043a\u0443\u0441\u0435 \u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u041f\u043e\u0442\u043e\u043c \u043c\u043e\u0436\u0435\u043c \u0447\u0435\u0440\u0435\u0437 css \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0441\u0435\u0432\u0434\u043e\u043a\u043b\u0430\u0441\u0441\u043e\u0432 focus-visible.<\/p>\n<p>activeElement \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432 \u0441\u0435\u0431\u0435 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0444\u043e\u043a\u0443\u0441\u0435. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043d\u0430 MDN: <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/activeElement\" rel=\"noopener noreferrer nofollow\">document.activeElement<\/a>.<\/p>\n<p>\u0415\u0441\u0442\u044c \u0442\u043e\u043d\u043a\u043e\u0441\u0442\u0438 \u0432 \u0440\u0430\u0437\u043d\u0438\u0446\u0435 focus \u0438 focus-visible, \u043f\u0440\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/doka.guide\/css\/focus-visible\/\" rel=\"noopener noreferrer nofollow\">Doka:focus-visible<\/a><\/p>\n<h3>\u041f\u0438\u0448\u0435\u043c \u043f\u0430\u0440\u0443 unit \u0442\u0435\u0441\u0442\u043e\u0432<\/h3>\n<p>\u041f\u0435\u0440\u0435\u0434 \u043d\u0430\u0447\u0430\u043b\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442 <strong>data-testid<\/strong> \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e Option, \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u043e \u043f\u0440\u043e\u0449\u0435 \u0438\u0441\u043a\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0442\u0435\u0441\u0442\u0430\u0445.<\/p>\n<pre><code class=\"typescript\">const Option = (props: OptionProps) => {   ...   const inputId = `${groupName}_radio_item_with_value__${value}`;    return (     &lt;div className={Styles.item} { \/* rest props *\/ } data-testid={inputId}>...&lt;\/div>   ); }; <\/code><\/pre>\n<p>\u041f\u0440\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0442\u0435\u0441\u0442\u0430 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u043c\u043e\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u0440\u043e <a href=\"https:\/\/habr.com\/ru\/articles\/734980\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044e \u0432 React \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438<\/a> \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 <strong>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0442\u0435\u0441\u0442\u0430<\/strong>.<\/p>\n<p>\u0412\u0441\u0451 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0442\u0435\u0441\u0442\u043e\u0432 \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438 <a href=\"https:\/\/create-react-app.dev\/docs\/running-tests\/\" rel=\"noopener noreferrer nofollow\">create-react-app<\/a>.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u043c\u0438\u043d\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u0430\u0440\u043e\u0447\u043a\u0443 \u043c\u0438\u043d\u0438 \u0442\u0435\u0441\u0442\u043e\u0432. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442 <strong>data-checked<\/strong> \u043f\u0440\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0432\u044b\u0431\u043e\u0440\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <strong>onChange<\/strong>:<\/p>\n<pre><code class=\"javascript\">import '@testing-library\/jest-dom'; import { render, screen, fireEvent } from '@testing-library\/react';  import RadioGroup from '.\/index'; import options from '.\/options.json';  describe('React component: RadioGroup', () => {   it('\u0414\u043e\u043b\u0436\u0435\u043d \u043f\u0440\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0430\u0442\u0440\u0438\u0431\u0443\u0442 [data-checked=\"true\"] \u043d\u0430 option, \u0435\u0441\u043b\u0438 \u0431\u044b\u043b\u043e \u0432\u044b\u0431\u0440\u0430\u043d\u043e \u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435', async () => {     render(       &lt;RadioGroup         selected={options[2].value}         name=\"id\"         onChange={jest.fn()}         options={options}       \/>     );      const radioItem = screen.getByTestId(`radio_item_with_value__${options[2].value}`);     expect(radioItem).toHaveAttribute('data-checked', 'true');   });    it('\u0414\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \"onChange\" \u043f\u0440\u0438 \u043a\u043b\u0438\u043a\u0435 \u043d\u0430 option', async () => {     const handleChange = jest.fn();      render(       &lt;RadioGroup         selected={options[2].value}         name=\"id\"         onChange={handleChange}         options={options}       \/>     );      const label = screen.getByLabelText(options[2].title);     fireEvent.click(label);      expect(handleChange).toHaveBeenCalledTimes(1);   }); }); <\/code><\/pre>\n<p>PS:<br \/> \u041f\u0440\u043e \u0444\u0440\u043e\u043d\u0442\u043e\u0432\u044b\u0435 \u0442\u0435\u0441\u0442\u044b \u0435\u0441\u0442\u044c <strong>\u043e\u0442\u043b\u0438\u0447\u043d\u0430\u044f<\/strong> \u0441\u0442\u0430\u0442\u044c\u044f \u0438\u0437 \u0431\u043b\u043e\u0433\u0430 Samokat.tech <a href=\"https:\/\/habr.com\/ru\/companies\/samokat_tech\/articles\/704342\/\" rel=\"noopener noreferrer nofollow\">\u041a\u0430\u043a \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0434<\/a>.<\/p>\n<h3>\u0418\u0442\u043e\u0433\u043e<\/h3>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0447\u0442\u0435\u043d\u0438\u0435 \u0438 \u0443\u0434\u0430\u0447\u0438 \u0432 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0432\u0430\u0448\u0438\u0445 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432)<\/p>\n<p>PS: \u0421\u0441\u044b\u043b\u043a\u0438 \u0438\u0437 \u0441\u0442\u0430\u0442\u044c\u0438:<\/p>\n<ul>\n<li>\n<p>\u041a\u043e\u0434 \u0438 \u0434\u0435\u043c\u043e: <a href=\"https:\/\/codesandbox.io\/s\/robzare-custom-radio-jte0kj\" rel=\"noopener noreferrer nofollow\">codesandbox.custom-radio<\/a><\/p>\n<\/li>\n<li>\n<p>\u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 <\/p>\n<ul>\n<li>\n<p>\u043f\u0440\u043e <a href=\"https:\/\/habr.com\/ru\/articles\/734980\/\" rel=\"noopener noreferrer nofollow\">\u041f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044e \u0432 React \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438<\/a><\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e <a href=\"https:\/\/habr.com\/ru\/articles\/735224\/\" rel=\"noopener noreferrer nofollow\">\u041a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 select \u0434\u043b\u044f React<\/a><\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e <a href=\"https:\/\/habr.com\/ru\/articles\/732690\/\" rel=\"noopener noreferrer nofollow\">\u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0444\u043e\u0440\u043c \u0431\u0435\u0437 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439<\/a><\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e \u0442\u043e, <a href=\"https:\/\/habr.com\/ru\/companies\/samokat_tech\/articles\/704342\/\" rel=\"noopener noreferrer nofollow\">\u041a\u0430\u043a \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0434<\/a><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e React <\/p>\n<ul>\n<li>\n<p>\u043f\u0440\u043e <a href=\"https:\/\/create-react-app.dev\/docs\/running-tests\/\" rel=\"noopener noreferrer nofollow\">create-react-app<\/a><\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e <a href=\"https:\/\/react.dev\/reference\/react\/memo\" rel=\"noopener noreferrer nofollow\">React.memo<\/a><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e \u0432\u0451\u0440\u0441\u0442\u043a\u0443 <\/p>\n<ul>\n<li>\n<p>\u043f\u0440\u043e <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/input\/radio#defining_a_radio_group\" rel=\"noopener noreferrer nofollow\">Radio group<\/a> \u043d\u0430 MDN<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/activeElement\" rel=\"noopener noreferrer nofollow\">document.activeElement<\/a><\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e \u0440\u0430\u0437\u043d\u0438\u0446\u0443 <a href=\"https:\/\/doka.guide\/css\/focus-visible\/\" rel=\"noopener noreferrer nofollow\">:focus vs :focus-visible<\/a><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/735724\/\"> https:\/\/habr.com\/ru\/articles\/735724\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0438\u0448\u0435\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u044b\u0439 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 RadioGroup \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0434\u043b\u044f React \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u043f\u0430\u0440\u043e\u0447\u043a\u0443 unit \u0442\u0435\u0441\u0442\u043e\u0432 \u043d\u0430 Jest.<\/p>\n<h3>\u041f\u043b\u0430\u043d \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439<\/h3>\n<p>\u041e\u0431\u0449\u0438\u0439 \u043f\u043b\u0430\u043d \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 6 \u044d\u0442\u0430\u043f\u043e\u0432:<\/p>\n<ol>\n<li>\n<p>\u041f\u043e\u043d\u044f\u0442\u044c, \u0447\u0442\u043e \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 Option<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 RadioGroup<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0451 \u0432 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0435 \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c<\/p>\n<\/li>\n<li>\n<p>\u0421\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u0432\u0432\u043e\u0434\u0430 \u0441 \u043a\u043b\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u044b<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043a\u0440\u044b\u0442\u044c \u0442\u0435\u0441\u0442\u0430\u043c\u0438<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u043e\u0435\u0445\u0430\u043b\u0438!<\/p>\n<h3>\u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h3>\n<p>\u041d\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u0430\u044f \u0440\u0430\u0434\u0438\u043e \u0433\u0440\u0443\u043f\u043f\u0430 \u0434\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432. \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u0430\u044f \u0444\u043e\u0440\u043c\u0430 \u0438 \u0432 \u043d\u0435\u0439 \u043d\u0443\u0436\u043d\u0430 &#171;\u0432\u044b\u0431\u0438\u0440\u0430\u043b\u043a\u0430&#187; \u043f\u0435\u0440\u0438\u043e\u0434\u0430, \u0434\u043b\u044f \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0438 \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0438\/\u043a\u043e\u043d\u0442\u0435\u043d\u0442\u0430 \u0437\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u043f\u0435\u0440\u0438\u043e\u0434 \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<p>\u0421\u0434\u0435\u043b\u0430\u0435\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0432 \u0432\u0438\u0434\u0435 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u043b\u0430\u0448\u043a\u0438, \u0441 \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432 \u0432\u0438\u0434\u0435 \u043a\u043d\u043e\u043f\u043e\u043a. \u0412 \u0446\u0435\u043b\u043e\u043c \u043d\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c ui \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u0442\u0430\u043a, \u043a\u0430\u043a \u0432\u0430\u043c \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c\u0441\u044f. Feel free to edit, \u043a\u0430\u043a \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0441\u044f.<\/p>\n<p>\u041f\u043e \u0438\u0442\u043e\u0433\u0443 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442. \u0414\u0435\u043c\u043e: <a href=\"https:\/\/codesandbox.io\/s\/robzare-custom-radio-jte0kj\" rel=\"noopener noreferrer nofollow\">codesandbox.custom-radio<\/a><\/p>\n<p>PS: \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0444\u043e\u0440\u043c\u0430\u043c\u0438 \u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0435\u0439. \u0420\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u043e\u0447\u0435\u043d\u044c \u043c\u043d\u043e\u0433\u043e, \u0441\u0442\u043e\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0433\u0443\u0433\u043b\u0438\u0442\u044c). \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u0434\u0438\u043d \u0438\u0437 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u044f \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/habr.com\/ru\/articles\/732690\/\" rel=\"noopener noreferrer nofollow\">\u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0444\u043e\u0440\u043c \u0431\u0435\u0437 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439<\/a>.<\/p>\n<p>\u041f\u043e\u0435\u0445\u0430\u043b\u0438!<\/p>\n<h3>\u041f\u0438\u0448\u0435\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 Option<\/h3>\n<h4>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b<\/h4>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043d\u0430\u0448\u0435\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0432\u044b\u0431\u043e\u0440\u0430. \u041e\u043d \u0431\u0443\u0434\u0435\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u0435\u043d \u0438 \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c 2 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430:<\/p>\n<pre><code class=\"typescript\"> type OptionType = {   value: string;   title: string; }; <\/code><\/pre>\n<p>\u0421\u0430\u043c \u0436\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 Options \u0434\u043e\u043b\u0436\u0435\u043d \u0443\u043c\u0435\u0442\u044c \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0435\u0449\u0435\u0439:<\/p>\n<ul>\n<li>\n<p><strong>\u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c<\/strong> \u043e\u0434\u0438\u043d \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0432\u044b\u0431\u043e\u0440\u0430<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e\u043c\u0435\u0447\u0430\u0442\u044c <strong>\u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0439<\/strong> \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u043c \u043e\u0442 \u0434\u0440\u0443\u0433\u0438\u0445<\/p>\n<\/li>\n<li>\n<p>\u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c <strong>onChange<\/strong> \u043f\u0440\u0438 \u0432\u044b\u0431\u043e\u0440\u0435 \u043a\u043b\u0438\u043a\u0435 \u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0435 \u043d\u0430 typescript \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 Option \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"typescript\">type OptionProps = {   value: OptionType['value'];   title: OptionType['title'];   selected: OptionType['value'];   groupName: string;   onChange?: (value: string) => void; }; <\/code><\/pre>\n<h4>\u0412\u0435\u0440\u0441\u0442\u043a\u0430<\/h4>\n<p>\u0414\u043b\u044f \u0441\u0442\u0438\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c css modules \u0434\u043b\u044f \u0441\u0442\u0438\u043b\u0438\u0437\u0430\u0446\u0438\u0438 (\u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043b\u0435\u0436\u0438\u0442 react-create-app \u0441 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u043c ts, \u0442\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 css modules \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438).<\/p>\n<p>\u041d\u0430\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0438\u043b\u0438 \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c:<\/p>\n<pre><code class=\"typescript\">  import Styles from '.\/index.module.css';   ...   &lt;div className={Styles.group}>...&lt;\/div> <\/code><\/pre>\n<p>\u0421\u0430\u043c \u0436\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e:<\/p>\n<pre><code class=\"typescript\">const Option = (props: OptionProps) => {     const {       value,       title,       selected,       groupName,       onChange     } = props;    const handleChange = () => onChange?.(value);    const inputId = `${groupName}_radio_item_with_value__${value}`;   const isChecked = value === selected;    return (     &lt;div       className={Styles.item}       key={value}       data-checked={isChecked}     >       &lt;input         className={Styles.input}         type=\"radio\"         name={groupName}         id={inputId}         value={value}         onChange={handleChange}       \/>       &lt;label className={Styles.label} htmlFor={inputId}>         {title}       &lt;\/label>     &lt;\/div>   ); }; <\/code><\/pre>\n<p>\u041f\u0440\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 data-checked \u0432 <strong>true<\/strong> \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435 &#171;\u043f\u0440\u043e\u043c\u0435\u0447\u0430\u0442\u044c \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u043c \u043e\u0442 \u0434\u0440\u0443\u0433\u0438\u0445&#187;. \u0417\u0430\u0442\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u043c title \u0438 \u0432\u0435\u0448\u0430\u0435\u043c handleChange \u043d\u0430 onChange \u043d\u0430\u0448\u0435\u0433\u043e \u0438\u043d\u043f\u0443\u0442\u0430.<\/p>\n<h3>\u041f\u0438\u0448\u0435\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 RadioGroup<\/h3>\n<h4>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b<\/h4>\n<p>\u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 RadioGroup \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a options, \u043a\u043e\u043b\u043b\u0431\u044d\u043a onChange \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430. \u041d\u0443 \u0438 \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u0438\u043c\u0435\u043d\u043d\u043e <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/input\/radio#defining_a_radio_group\" rel=\"noopener noreferrer nofollow\">Radio group<\/a>, \u0430 \u043d\u0435 \u0447\u0442\u043e-\u0442\u043e \u0434\u0440\u0443\u0433\u043e\u0435, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u043c\u044f \u044d\u0442\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u044b.<\/p>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 4\u0445 \u043f\u0440\u043e\u043f\u0441\u043e\u0432:<\/p>\n<pre><code class=\"typescript\">type RadioGroupProps = {   name: string;   options: OptionType[];   selected: OptionType['value'];   onChange?: (value: string) => void; }; <\/code><\/pre>\n<h4>\u0412\u0451\u0440\u0441\u0442\u043a\u0430<\/h4>\n<p>\u0412 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u043e\u0442\u0440\u0435\u043d\u0434\u0435\u0440\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a option \u0438 \u043e\u0431\u044a\u044f\u0432\u0438\u0442\u044c handleChange \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430. \u041f\u043b\u044e\u0441 \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0431\u0435\u0440\u043d\u0451\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0432 <a href=\"https:\/\/react.dev\/reference\/react\/memo\" rel=\"noopener noreferrer nofollow\">React.memo<\/a>.<\/p>\n<pre><code class=\"typescript\">const RadioGroup = (props: RadioGroupProps) => {   const { name, options, selected, onChange } = props;    const handleChange = (value: string) => onChange?.(value);    return (     &lt;div className={Styles.group}>       {options.map(({ value, title }) => (         &lt;Option           key={value}           groupName={name}           value={value}           title={title}           selected={selected}           onChange={handleChange}         \/>       ))}     &lt;\/div>   ); };  export default React.memo(RadioGroup); <\/code><\/pre>\n<h3>\u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0451 \u0432 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0435 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c<\/h3>\n<pre><code class=\"typescript\">import { useState } from \"react\";  import options from \".\/components\/radio\/options.json\"; import Radio from \".\/components\/radio\";  import \".\/styles.css\";  export default function App() {   const [period, setPeriod] = useState(\"\");    const handlePeriodChange = (val: string) => {     setPeriod(val);   };    return (     &lt;div className=\"App\">       &lt;h1>Custom RadioGroup component example&lt;\/h1>       &lt;h3>\u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043f\u0435\u0440\u0438\u043e\u0434&lt;\/h3>       &lt;div className=\"Radio\">         &lt;Radio           selected={period}           name=\"radio\"           onChange={handlePeriodChange}           options={options}         \/>       &lt;\/div>     &lt;\/div>   ); } <\/code><\/pre>\n<h3>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0432\u0432\u043e\u0434\u0430 \u0441 \u043a\u043b\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u044b<\/h3>\n<p>\u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 RadioGroup \u0441 \u043a\u043b\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u044b, \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0430\u0448 Option \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442. \u0410 \u0438\u043c\u0435\u043d\u043d\u043e:<\/p>\n<ul>\n<li>\n<p>\u0432 <strong>Option<\/strong> \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043b\u0443\u0448\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u043d\u0430\u0436\u0430\u0442\u0438\u044f, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043b\u0438 \u043d\u0430\u0448 option \u0432 \u0444\u043e\u043a\u0443\u0441\u0435 \u0438\u043b\u0438 \u043d\u0435\u0442. \u0415\u0441\u043b\u0438 option \u0432 \u0444\u043e\u043a\u0443\u0441\u0435, \u0442\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a <strong>onClick<\/strong><\/p>\n<\/li>\n<li>\n<p>\u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u043a\u043e\u043b\u0434\u043e\u0432\u0430\u0442\u044c \u0441 tabindex.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u043a\u0438:<\/p>\n<pre><code class=\"typescript\">import { useEffect, useRef } from 'react';  const Option = (props: OptionProps) => {   const optionRef = useRef&lt;HTMLDivElement>(null);   ...   useEffect(() => {     const option = optionRef.current;      if (!option) return;      const handleEnterKeyDown = (event: KeyboardEvent) => {        if ((document.activeElement === option) &amp;&amp; event.key === 'Enter') {         onChange?.(value);       }     }      option.addEventListener('keydown', handleEnterKeyDown);      return () => {       option.removeEventListener('keydown', handleEnterKeyDown);     };   }, [value, onChange]);    return (     &lt;div       className={Styles.item}       { \/* rest props *\/ }       ref={optionRef}       tabIndex={0}     >       &lt;input className={Styles.input} { \/* rest props *\/ } tabIndex={-1} \/>       ...     &lt;\/div>   ); } <\/code><\/pre>\n<p>\u041c\u044b \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u043c input \u0438\u0437 \u043e\u0431\u0445\u043e\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043a\u043b\u0430\u0432\u0438\u0448\u0438 tab, \u043f\u0440\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044f tabindex \u0432 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0418 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0432 \u044d\u0442\u043e\u0442 \u043e\u0431\u0445\u043e\u0434 div \u043e\u0431\u0451\u0440\u0442\u043a\u0443 \u0432\u0441\u0435\u0433\u043e \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0433\u043e option.<\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430 \u043f\u0440\u0438 \u0444\u043e\u043a\u0443\u0441\u0435 \u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430. \u041f\u043e\u0442\u043e\u043c \u043c\u043e\u0436\u0435\u043c \u0447\u0435\u0440\u0435\u0437 css \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0441\u0435\u0432\u0434\u043e\u043a\u043b\u0430\u0441\u0441\u043e\u0432 focus-visible.<\/p>\n<p>activeElement \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432 \u0441\u0435\u0431\u0435 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0444\u043e\u043a\u0443\u0441\u0435. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043d\u0430 MDN: <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/activeElement\" rel=\"noopener noreferrer nofollow\">document.activeElement<\/a>.<\/p>\n<p>\u0415\u0441\u0442\u044c \u0442\u043e\u043d\u043a\u043e\u0441\u0442\u0438 \u0432 \u0440\u0430\u0437\u043d\u0438\u0446\u0435 focus \u0438 focus-visible, \u043f\u0440\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/doka.guide\/css\/focus-visible\/\" rel=\"noopener noreferrer nofollow\">Doka:focus-visible<\/a><\/p>\n<h3>\u041f\u0438\u0448\u0435\u043c \u043f\u0430\u0440\u0443 unit \u0442\u0435\u0441\u0442\u043e\u0432<\/h3>\n<p>\u041f\u0435\u0440\u0435\u0434 \u043d\u0430\u0447\u0430\u043b\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442 <strong>data-testid<\/strong> \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e Option, \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u043e \u043f\u0440\u043e\u0449\u0435 \u0438\u0441\u043a\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0442\u0435\u0441\u0442\u0430\u0445.<\/p>\n<pre><code class=\"typescript\">const Option = (props: OptionProps) => {   ...   const inputId = `${groupName}_radio_item_with_value__${value}`;    return (     &lt;div className={Styles.item} { \/* rest props *\/ } data-testid={inputId}>...&lt;\/div>   ); }; <\/code><\/pre>\n<p>\u041f\u0440\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0442\u0435\u0441\u0442\u0430 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u043c\u043e\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u0440\u043e <a href=\"https:\/\/habr.com\/ru\/articles\/734980\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044e \u0432 React \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438<\/a> \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 <strong>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0442\u0435\u0441\u0442\u0430<\/strong>.<\/p>\n<p>\u0412\u0441\u0451 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0442\u0435\u0441\u0442\u043e\u0432 \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438 <a href=\"https:\/\/create-react-app.dev\/docs\/running-tests\/\" rel=\"noopener noreferrer nofollow\">create-react-app<\/a>.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u043c\u0438\u043d\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u0430\u0440\u043e\u0447\u043a\u0443 \u043c\u0438\u043d\u0438 \u0442\u0435\u0441\u0442\u043e\u0432. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442 <strong>data-checked<\/strong> \u043f\u0440\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0432\u044b\u0431\u043e\u0440\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <strong>onChange<\/strong>:<\/p>\n<pre><code class=\"javascript\">import '@testing-library\/jest-dom'; import { render, screen, fireEvent } from '@testing-library\/react';  import RadioGroup from '.\/index'; import options from '.\/options.json';  describe('React component: RadioGroup', () => {   it('\u0414\u043e\u043b\u0436\u0435\u043d \u043f\u0440\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0430\u0442\u0440\u0438\u0431\u0443\u0442 [data-checked=\"true\"] \u043d\u0430 option, \u0435\u0441\u043b\u0438 \u0431\u044b\u043b\u043e \u0432\u044b\u0431\u0440\u0430\u043d\u043e \u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435', async () => {     render(       &lt;RadioGroup         selected={options[2].value}         name=\"id\"         onChange={jest.fn()}         options={options}       \/>     );      const radioItem = screen.getByTestId(`radio_item_with_value__${options[2].value}`);     expect(radioItem).toHaveAttribute('data-checked', 'true');   });    it('\u0414\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \"onChange\" \u043f\u0440\u0438 \u043a\u043b\u0438\u043a\u0435 \u043d\u0430 option', async () => {     const handleChange = jest.fn();      render(       &lt;RadioGroup         selected={options[2].value}         name=\"id\"         onChange={handleChange}         options={options}       \/>     );      const label = screen.getByLabelText(options[2].title);     fireEvent.click(label);      expect(handleChange).toHaveBeenCalledTimes(1);   }); }); <\/code><\/pre>\n<p>PS:<br \/> \u041f\u0440\u043e \u0444\u0440\u043e\u043d\u0442\u043e\u0432\u044b\u0435 \u0442\u0435\u0441\u0442\u044b \u0435\u0441\u0442\u044c <strong>\u043e\u0442\u043b\u0438\u0447\u043d\u0430\u044f<\/strong> \u0441\u0442\u0430\u0442\u044c\u044f \u0438\u0437 \u0431\u043b\u043e\u0433\u0430 Samokat.tech <a href=\"https:\/\/habr.com\/ru\/companies\/samokat_tech\/articles\/704342\/\" rel=\"noopener noreferrer nofollow\">\u041a\u0430\u043a \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0434<\/a>.<\/p>\n<h3>\u0418\u0442\u043e\u0433\u043e<\/h3>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0447\u0442\u0435\u043d\u0438\u0435 \u0438 \u0443\u0434\u0430\u0447\u0438 \u0432 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0432\u0430\u0448\u0438\u0445 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432)<\/p>\n<p>PS: \u0421\u0441\u044b\u043b\u043a\u0438 \u0438\u0437 \u0441\u0442\u0430\u0442\u044c\u0438:<\/p>\n<ul>\n<li>\n<p>\u041a\u043e\u0434 \u0438 \u0434\u0435\u043c\u043e: <a href=\"https:\/\/codesandbox.io\/s\/robzare-custom-radio-jte0kj\" rel=\"noopener noreferrer nofollow\">codesandbox.custom-radio<\/a><\/p>\n<\/li>\n<li>\n<p>\u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 <\/p>\n<ul>\n<li>\n<p>\u043f\u0440\u043e <a href=\"https:\/\/habr.com\/ru\/articles\/734980\/\" rel=\"noopener noreferrer nofollow\">\u041f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044e \u0432 React \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438<\/a><\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e <a href=\"https:\/\/habr.com\/ru\/articles\/735224\/\" rel=\"noopener noreferrer nofollow\">\u041a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 select \u0434\u043b\u044f React<\/a><\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e <a href=\"https:\/\/habr.com\/ru\/articles\/732690\/\" rel=\"noopener noreferrer nofollow\">\u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0444\u043e\u0440\u043c \u0431\u0435\u0437 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439<\/a><\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e \u0442\u043e, <a href=\"https:\/\/habr.com\/ru\/companies\/samokat_tech\/articles\/704342\/\" rel=\"noopener noreferrer nofollow\">\u041a\u0430\u043a \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0434<\/a><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e React <\/p>\n<ul>\n<li>\n<p>\u043f\u0440\u043e <a href=\"https:\/\/create-react-app.dev\/docs\/running-tests\/\" rel=\"noopener noreferrer nofollow\">create-react-app<\/a><\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e <a href=\"https:\/\/react.dev\/reference\/react\/memo\" rel=\"noopener noreferrer nofollow\">React.memo<\/a><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e \u0432\u0451\u0440\u0441\u0442\u043a\u0443 <\/p>\n<ul>\n<li>\n<p>\u043f\u0440\u043e <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/input\/radio#defining_a_radio_group\" rel=\"noopener noreferrer nofollow\">Radio group<\/a> \u043d\u0430 MDN<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/activeElement\" rel=\"noopener noreferrer nofollow\">document.activeElement<\/a><\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e \u0440\u0430\u0437\u043d\u0438\u0446\u0443 <a href=\"https:\/\/doka.guide\/css\/focus-visible\/\" rel=\"noopener noreferrer nofollow\">:focus vs :focus-visible<\/a><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/735724\/\"> https:\/\/habr.com\/ru\/articles\/735724\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-406662","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/406662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=406662"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/406662\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=406662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=406662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=406662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}