{"id":226636,"date":"2020-11-30T19:13:26","date_gmt":"2020-11-30T11:13:26","guid":{"rendered":"http:\/\/4563.org\/?p=226636"},"modified":"2020-11-30T19:13:26","modified_gmt":"2020-11-30T11:13:26","slug":"%e8%bf%99%e6%98%af%e4%b8%80%e4%b8%aa%e9%9d%a2%e8%af%95%e9%a1%b9%e7%9b%ae%ef%bc%8c%e8%b5%90%e6%95%99%e8%bf%98%e6%9c%89%e5%93%aa%e4%ba%9b%e9%9c%80%e8%a6%81%e4%bc%98%e5%8c%96%e7%9a%84%e5%9c%b0%e6%96%b9","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=226636","title":{"rendered":"\u8fd9\u662f\u4e00\u4e2a\u9762\u8bd5\u9879\u76ee\uff0c\u8d50\u6559\u8fd8\u6709\u54ea\u4e9b\u9700\u8981\u4f18\u5316\u7684\u5730\u65b9\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8fd9\u662f\u4e00\u4e2a\u9762\u8bd5\u9879\u76ee\uff0c\u8d50\u6559\u8fd8\u6709\u54ea\u4e9b\u9700\u8981\u4f18\u5316\u7684\u5730\u65b9\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : tiglapiles <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h1>Table of Contents<\/h1>\n<p> <\/p>\n<ol>\n<li>\u8fd9\u662f\u4e00\u4e2a\u9762\u8bd5\u9879\u76ee\uff0c\u8d50\u6559\u8fd8\u6709\u54ea\u4e9b\u9700\u8981\u4f18\u5316\u7684\u5730\u65b9\uff1f<\/li>\n<li>\u9879\u76ee\u9700\u6c42<\/li>\n<li>\u6211\u7684\u4ee3\u7801<\/li>\n<\/ol>\n<h1>\u8fd9\u662f\u4e00\u4e2a\u9762\u8bd5\u9879\u76ee\uff0c\u8d50\u6559\u8fd8\u6709\u54ea\u4e9b\u9700\u8981\u4f18\u5316\u7684\u5730\u65b9\uff1f<\/h1>\n<p> <\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u516c\u53f8\u7684\u9762\u8bd5\u5c0f\u9879\u76ee\uff0c\u6839\u636e\u6570\u636e\u751f\u6210\u591a\u9009\u6846\uff0c\u8981\u6709\u4e00\u4e2a\u5168\u9009\u7684\u9009\u9879\u76ee\uff0c\u8fd9\u662f\u6211\u7684\u4ee3\u7801\uff0c\u8bf7\u95ee\u8fd8\u6709\u54ea\u4e9b\u9700\u8981\u6539\u8fdb\u7684\u5730\u65b9\u548c\u4e0d\u8db3\uff1f\u56e0\u4e3a\u5bf9\u65b9\u4e0d\u56de\u590d\uff0c\u53ea\u80fd\u6765\u8fd9\u91cc\u6c42\u6559\u3002<\/p>\n<\/p>\n<h1>\u9879\u76ee\u9700\u6c42<\/h1>\n<p> <\/p>\n<ol>\n<li>\u5199\u4e00\u4e2a\u591a\u9009\u6846\u5c0f\u7ec4\u4ef6;<\/li>\n<li>\u9700\u8981\u6709\u5355\u5143\u6d4b\u8bd5<\/li>\n<li>\u6709\u5168\u9009\u9009\u9879\u76ee<\/li>\n<li>\u6837\u5f0f\u7b26\u5408\u9700\u6c42<\/li>\n<li>\u7528 react,typescript,jest \u6280\u672f<\/li>\n<li>\u5168\u7a0b\u4e0d\u80fd\u51fa\u73b0\u4e2d\u6587<\/li>\n<li>\u9700\u8981\u6709\u610f\u4e49\u7684\u6ce8\u91ca<\/li>\n<li>\u4ee3\u7801\u5c3d\u91cf\u4f18\u7f8e<\/li>\n<li>\u53d6\u540d\u4f18\u7f8e<\/li>\n<li>\u6700\u540e\u63d0\u4ea4 PR \u5ba1\u6838<\/li>\n<\/ol>\n<h1>\u6211\u7684\u4ee3\u7801<\/h1>\n<p> <\/p>\n<ul>\n<li>\n<p>\u591a\u9009\u6846\u7ec4\u4ef6 MultiCheck.tsx<\/p>\n<pre><code>    import React, { useState, useEffect, FunctionComponent } from \"react\";     import { fpMap, pipe } from \"..\/utils\";     import \".\/MultiCheck.css\";      export type Option = {     label: string;     value: string;     checked: boolean;     };  \/**  * Notice:  * 1. There should be a special `Select All` option with checkbox to control all passing options  * 2. If columns &gt; 1, the options should be placed from top to bottom in each column  *  * @param {string} label - the label text of this component  * @param {Option[]} options - options  * @param {number} columns - default value is 1  * @param {Function} onChange - when checked options are changed,  *                             they should be passed to outside  *\/ type Props = {   label?: string;   options: Option[];   columns?: number;   onChange?: (options: Option[]) =&gt; void; };  const selectAll = (c = false) =&gt; ({   label: \"Select All\",   value: \"Select All\",   checked: c });  const MultiCheck: FunctionComponent&lt;Props&gt; = (props): JSX.Element =&gt; {   const { label = \"\", columns = 1, onChange = () =&gt; {} } = props;   const [value, setValue] = useState({}); \/\/ for call props.onChange   const [options, setOptions] = useState&lt;Option[]&gt;([     \/* if original options all checked *\/     selectAll(!hasUnchecked(props.options)),     ...props.options   ]);    function hasUnchecked(o: Option[]) {     return o.filter(o =&gt; o.value !== selectAll().value).some(o =&gt; !o.checked);   }    function _handleClick(o: Option): void {     \/* user toggle click checkbox *\/     const convertCheck = fpMap(i =&gt; {       if (i.value === o.value) {         return { ...i, checked: !o.checked };       }       return i;     });     \/* user toggle click SelectAll option *\/     const convertSelectAll = fpMap(i =&gt; {       if (selectAll().value === o.value) {         return { ...i, checked: !o.checked };       }       return i;     });     \/* if all other option checked than SelectAll option checked else unchecked *\/     const isAllChecked = (o: Option[]) =&gt; {       return fpMap(i =&gt; {         if (i.value === selectAll().value) {           return { ...i, checked: !hasUnchecked(o) };         }         return i;       })(o);     };      setOptions(prev =&gt;       pipe(convertCheck, convertSelectAll, isAllChecked)(prev)     );     setValue(o);   }    function _getColumns(n: number): number {     return n &gt; 0 ? Math.ceil(options.length \/ n) : 1;   }    useEffect(() =&gt; {     \/* selectAll option can not be pass outside *\/     onChange(options.filter(i =&gt; i.value !== selectAll().value));   }, [value]);    return (     &lt;div className=\"multi-check-container\"&gt;       &lt;div className=\"multi-check\"&gt;         &lt;div className=\"multi-check-label\"&gt;           &lt;label&gt;{label}&lt;\/label&gt;         &lt;\/div&gt;         &lt;div           className=\"multi-check-items\"           style={{             gridTemplateRows: `repeat(${_getColumns(columns)},auto)`           }}         &gt;           {fpMap(o =&gt; (             &lt;label className=\"multi-check-item\" key={o.value}&gt;               &lt;input                 type=\"checkbox\"                 value={o.value}                 checked={o.checked}                 onChange={() =&gt; _handleClick(o)}               \/&gt;               &lt;span&gt;&lt;\/span&gt;               &lt;div&gt;{o.label}&lt;\/div&gt;             &lt;\/label&gt;           ))(options)}         &lt;\/div&gt;       &lt;\/div&gt;     &lt;\/div&gt;   ); };  export default MultiCheck; <\/code><\/pre>\n<\/li>\n<li>\n<p>\u6d4b\u8bd5\u7528\u4f8b<\/p>\n<pre><code>    import \"@testing-library\/jest-dom\";     import React from \"react\";     import renderer from \"react-test-renderer\";     import { fireEvent, render, screen } from \"@testing-library\/react\";     import { fpMap, pipe } from \"..\/utils\";     import MultiCheck from \".\/MultiCheck\";  describe(\"MultiCheck\", () =&gt; {   describe(\"initialize\", () =&gt; {     it(\"renders correctly\", () =&gt; {       const tree = renderer.create(&lt;MultiCheck options={[]} \/&gt;).toJSON();        expect(tree).toMatchSnapshot();     });      it(\"renders the label if label provided\", () =&gt; {       const label = \"jest test\";       render(&lt;MultiCheck options={[]} label={label} \/&gt;);        expect(screen.queryByLabelText(label)).toBeDefined();     });      it(\"render and click checkbox\", () =&gt; {       const options = [{ label: \"test\", value: \"test\", checked: false }];       render(&lt;MultiCheck options={options} \/&gt;);        expect(screen.queryByLabelText(\/test\/i)).not.toBeChecked();        \/\/ simulate user click checkbox       fireEvent.click(screen.getByLabelText(\/test\/i));        expect(screen.queryByLabelText(\/test\/i)).toBeChecked();     });   });    describe(\"utils\", () =&gt; {     it(\"fpMap\", () =&gt; {       let arr = [1, 2, 3];       let add1 = (o: number) =&gt; o + 1;        expect(fpMap(add1)(arr)).toEqual([2, 3, 4]);     });      it(\"pipe\", () =&gt; {       let arr = [1, 2, 3];       let add1Each = (i: []) =&gt; i.map(o =&gt; o + 1);       let prod2Each = (i: []) =&gt; i.map(o =&gt; o * 2);        expect(pipe(add1Each, prod2Each)(arr)).toEqual([4, 6, 8]);     });   }); }); <\/code><\/pre>\n<\/li>\n<li>\n<p>\u5de5\u5177\u4ee3\u7801 utils.ts<\/p>\n<pre><code>    type fn = (arg: any) =&gt; any;  \/\/ functional programming for map iterator export function fpMap(func: fn): fn {   return function(arr: unknown[]): unknown[] {     let length: number = arr.length || 0;     let i: number = 0;     let result: unknown[] = [];      while (i &lt; length) {       result.push(func(arr[i]));       i++;     }     return result;   }; }  \/\/ functional programming pipe export function pipe(...fns: fn[]): fn {   return function(x: any) {     return fns.reduce((v: any, f: fn) =&gt; f(v), x);   }; } <\/code><\/pre>\n<\/li>\n<\/ul><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>12<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4594110\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : tiglapiles <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5f88\u5b8c\u7f8e\u4e48\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4594111\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : tesguest123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4ea4\u5b8c\u540e\uff0c\u4f60\u4e0d\u9002\u5408\u6211\u4eec\u516c\u53f8 doge \u3002\u767d\u5ad6\u4e00\u6ce2                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4594112\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : tiglapiles <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @tesguest123 \u5bf9\u5440\uff0c\u6240\u4ee5\u6765\u8fd9\u91cc\u95ee\u95ee                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4594113\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : aaronlam <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u662f\u4e2a\u73b0\u573a\u4e0a\u673a\u9898\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4594114\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Chrisssss <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6bd4\u5982 `_getColumns` \u4e0d\u5173\u4e4e\u4e1a\u52a1\u7684\u4e1c\u897f\u53ef\u4ee5\u63d0\u5230 FC \u5916\u9762\u53bb\uff0c\u90e8\u5206 any \u5b9a\u4e49\u4f7f\u7528\u6cdb\u578b\uff0c\u6709\u4e00\u4e9b\u4ee3\u7801\u98ce\u683c\u4e0d\u592a\u597d\uff0c\u6bd4\u5982 `selectAll` \u51fd\u6570\u7684 c \u53c2\u6570\u547d\u540d\uff0c\u5176\u5b9e\u53ef\u4ee5\u7528 `checked`\uff0c\u7136\u540e\u91cc\u9762\u76f4\u63a5`{<br \/> label: &#8220;Select All&#8221;,<br \/> value: &#8220;Select All&#8221;,<br \/> checked<br \/>}` \u5c31\u597d\u3002`\u2018Select All\u2019` \u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u5e38\u91cf\u3002\u8fd9\u4e9b\u5c31\u662f\u6211\u4f5c\u4e3a\u9762\u8bd5\u5b98\u7684\u8bdd\u770b\u4e86\u4e00\u773c\u4f1a\u7ed9\u4f60\u6263\u5206\u7684\u5730\u65b9                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4594115\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Chrisssss <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5176\u5b9e\u8fd8\u6709\u5f88\u591a\u95ee\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4594116\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : buhi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u597d\u597d\u7684 Array#map \u4e0d\u7528, \u9020\u4e86\u4e2a any=&gt;any=&gt;(any[])=&gt;any[], \u4e00\u901a\u4e0b\u6765\u5168\u7ed9\u4f60\u6574 any \u4e86<br \/>\u8fd9\u5c31\u662f\u4f60\u8ba4\u4e3a\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\u5417? \u9047\u89c1\u6211\u540c\u4e8b\u8fd9\u4e48\u505a\u76f4\u63a5\u6253\u6b7b\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4594117\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : tiglapiles <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @aaronlam \u4e0d\u662f\uff0cgithub                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4594118\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : tiglapiles <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @buhi \u5927\u54e5\u8fd9\u80fd\u4e00\u6837\u4e48\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4594119\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : tiglapiles <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Chrisssss \u6ca1\u65f6\u95f4\u6253\u5b57\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4594120\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : tiglapiles <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Chrisssss \u662f\u6709\u4e00\u4e9b\u4e0d\u8db3\uff0c\u4e0d\u8fc7\u8fd9\u662f\u4e00\u9053\u9762\u8bd5\u9898\uff0c\u6211\u4e0d\u4f1a\u82b1\u592a\u591a\u65f6\u95f4\u5728\u4e0a\u9762\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4594121\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yzbythesea <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u903b\u8f91\u633a\u7b80\u5355\u7684\uff0c\u4f46\u662f\u6211\u5c31\u662f\u8bfb\u4e0d\u61c2\u4f60\u7684\u4ee3\u7801\u3002\u3002\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u4e0d\u7528 array \u5b58 option \uff1f\u5355\u72ec\u9020\u4e00\u4e2a selectAll \u7684\u7c7b\u5e72\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u4e0d\u628a selectAll \u5f53\u4f5c\u4e00\u4e2a option \u52a0\u8fdb\u53bb\u5462\uff1f<\/p>\n<p>\u7136\u540e checkbox \u7684\u903b\u8f91\u90fd\u53ef\u4ee5\u4f46\u5199\u51fa function\uff0c\u518d\u5957\u8fdb\u91cc\u9762\uff0c\u4f60\u8fd9\u4e48\u5199\u770b\u7740\u597d\u6655\u3002\u3002\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u4e00\u4e2a\u9762\u8bd5\u9879\u76ee\uff0c\u8d50\u6559\u8fd8\u6709\u54ea\u4e9b\u9700\u8981&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/226636"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=226636"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/226636\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=226636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=226636"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=226636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}