Advertisement
Pandaaaa906

Untitled

Jan 7th, 2022
1,014
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {useState} from 'react'
  2. import produce from 'immer'
  3.  
  4. const filterField=(value)=>(item)=>{
  5.     return item.name?.includes(value)
  6. }
  7.  
  8. function useShutterForm({fields=[], filterFunc=filterField}){
  9.     const [values, setValues] = useState({
  10.         leftFields: fields.map((item, index)=>({...item, checked: false, index})),
  11.         rightFields: [],
  12.         leftKeyword: '',
  13.         rightKeyword: '',
  14.     })
  15.     const leftFields = values.leftFields
  16.     const rightFields = values.rightFields
  17.     const leftKeyword = values.leftKeyword
  18.     const rightKeyword = values.rightKeyword
  19.     const filteredLFields = values.leftFields.filter(filterFunc(values.leftKeyword))
  20.     const filteredRFields = values.rightFields.filter(filterFunc(values.rightKeyword))
  21.  
  22.     const reset=()=>{
  23.         setValues(produce(preState=>{
  24.             preState.leftFields = fields.map(item=>({...item, checked: false}))
  25.             preState.rightFields = []
  26.         }))
  27.     }
  28.  
  29.     const handleLKeywordChange = ({target: {value}})=>{
  30.         setValues(produce(preState=>preState.leftKeyword = value))
  31.     }
  32.  
  33.     const handleRKeywordChange = ({target: {value}})=>{
  34.         setValues(produce(preState=>preState.rightKeyword = value))
  35.     }
  36.  
  37.     const toggleLField = (index)=>()=>{
  38.         setValues(produce(preState=> {
  39.             preState.leftFields[index].checked = !values.leftFields[index].checked
  40.         }))
  41.     }
  42.  
  43.     const toggleLFields = ()=>{
  44.         // not implemented
  45.     }
  46.  
  47.     const transferL2R = ()=>{
  48.         setValues(produce(preState=>{
  49.             const toRFields = values.leftFields.filter(item=>item.checked)
  50.             preState.leftFields = values.leftFields.filter(item=>!item.checked)
  51.             preState.rightFields = [...values.rightFields, ...toRFields]
  52.         }))
  53.     }
  54.  
  55.     return {
  56.         leftFields,
  57.         rightFields,
  58.         leftKeyword,
  59.         rightKeyword,
  60.         filteredLFields,
  61.         filteredRFields,
  62.         setValues,
  63.  
  64.         reset,
  65.         transferL2R,
  66.  
  67.         toggleLField,
  68.         handleLKeywordChange,
  69.  
  70.         handleRKeywordChange,
  71.     }
  72. }
  73.  
  74.  
  75. const fields = [
  76.     {name: '产品中文名称', field: 'cn_name'},
  77.     {name: '产品英文名称', field: 'en_name'},
  78. ]
  79.  
  80. function ShutterForm(){
  81.     const form = useShutterForm({fields})
  82.  
  83.     return (
  84.         <div>
  85.             <div className={'leftPanel'}>
  86.                 <div>
  87.                     <input value={form.leftKeyword} onChange={form.handleLKeywordChange}/>
  88.                 </div>
  89.                 <div>
  90.                     {form.filteredLFields.map((item)=>(
  91.                         <div key={item.index}>
  92.                             <checkbox checked={item.checked} onChange={form.toggleLField(item.index)}/>
  93.                             <input id={item.index} name={item.field}/>
  94.                         </div>
  95.                     ))}
  96.                 </div>
  97.             </div>
  98.             <div className={'rightPanel'}>
  99.                 <div>
  100.                     <input value={form.rightKeyword} onChange={form.handleRKeywordChange}/>
  101.                 </div>
  102.                 <div>
  103.  
  104.                 </div>
  105.             </div>
  106.         </div>
  107.     )
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement