|
| 1 | +constReact=window.React; |
| 2 | + |
| 3 | +const{useState}=React; |
| 4 | + |
| 5 | +asyncfunctiondefer(timeoutMS){ |
| 6 | +returnnewPromise(resolve=>{ |
| 7 | +setTimeout(resolve,timeoutMS); |
| 8 | +}); |
| 9 | +} |
| 10 | + |
| 11 | +exportdefaultfunctionFormActions(){ |
| 12 | +const[textValue,setTextValue]=useState('0'); |
| 13 | +const[radioValue,setRadioValue]=useState('two'); |
| 14 | +const[checkboxValue,setCheckboxValue]=useState([false,true,true]); |
| 15 | +const[selectValue,setSelectValue]=useState('three'); |
| 16 | + |
| 17 | +return( |
| 18 | +<form |
| 19 | +action={async()=>{ |
| 20 | +awaitdefer(500); |
| 21 | +}} |
| 22 | +onReset={()=>{ |
| 23 | +setTextValue('0'); |
| 24 | +setRadioValue('two'); |
| 25 | +setCheckboxValue([false,true,true]); |
| 26 | +setSelectValue('three'); |
| 27 | +}}> |
| 28 | +<divstyle={{display: 'flex'}}> |
| 29 | +<fieldsetstyle={{flexBasis: 0}}> |
| 30 | +<legend>type="text"</legend> |
| 31 | +<input |
| 32 | +type="text" |
| 33 | +name="text" |
| 34 | +value={textValue} |
| 35 | +onChange={event=>setTextValue(event.currentTarget.value)} |
| 36 | +/> |
| 37 | +</fieldset> |
| 38 | +<fieldsetstyle={{flexBasis: 0}}> |
| 39 | +<legend>type="radio"</legend> |
| 40 | +<input |
| 41 | +type="radio" |
| 42 | +name="radio" |
| 43 | +value="one" |
| 44 | +checked={radioValue==='one'} |
| 45 | +onChange={()=>setRadioValue('one')} |
| 46 | +/> |
| 47 | +<input |
| 48 | +type="radio" |
| 49 | +name="radio" |
| 50 | +value="two" |
| 51 | +checked={radioValue==='two'} |
| 52 | +onChange={()=>setRadioValue('two')} |
| 53 | +/> |
| 54 | +<input |
| 55 | +type="radio" |
| 56 | +name="radio" |
| 57 | +value="three" |
| 58 | +checked={radioValue==='three'} |
| 59 | +onChange={()=>setRadioValue('three')} |
| 60 | +/> |
| 61 | +</fieldset> |
| 62 | +<fieldsetstyle={{flexBasis: 0}}> |
| 63 | +<legend>type="checkbox"</legend> |
| 64 | +<input |
| 65 | +type="checkbox" |
| 66 | +name="checkbox" |
| 67 | +value="one" |
| 68 | +checked={checkboxValue[0]} |
| 69 | +onChange={event=>{ |
| 70 | +constchecked=event.currentTarget.checked; |
| 71 | +setCheckboxValue(pending=>[checked,pending[1],pending[2]]); |
| 72 | +}} |
| 73 | +/> |
| 74 | +<input |
| 75 | +type="checkbox" |
| 76 | +name="checkbox" |
| 77 | +value="two" |
| 78 | +checked={checkboxValue[1]} |
| 79 | +onChange={event=>{ |
| 80 | +constchecked=event.currentTarget.checked; |
| 81 | +setCheckboxValue(pending=>[pending[0],checked,pending[2]]); |
| 82 | +}} |
| 83 | +/> |
| 84 | +<input |
| 85 | +type="checkbox" |
| 86 | +name="checkbox" |
| 87 | +value="three" |
| 88 | +checked={checkboxValue[2]} |
| 89 | +onChange={event=>{ |
| 90 | +constchecked=event.currentTarget.checked; |
| 91 | +setCheckboxValue(pending=>[pending[0],pending[1],checked]); |
| 92 | +}} |
| 93 | +/> |
| 94 | +</fieldset> |
| 95 | +<fieldsetstyle={{flexBasis: 0}}> |
| 96 | +<legend>select</legend> |
| 97 | +<select |
| 98 | +name="select" |
| 99 | +value={selectValue} |
| 100 | +onChange={event=>setSelectValue(event.currentTarget.value)}> |
| 101 | +<optionvalue="one">one</option> |
| 102 | +<optionvalue="two">two</option> |
| 103 | +<optionvalue="three">three</option> |
| 104 | +</select> |
| 105 | +</fieldset> |
| 106 | +</div> |
| 107 | +<div> |
| 108 | +<inputtype="reset"/> |
| 109 | +<inputtype="submit"/> |
| 110 | +</div> |
| 111 | +</form> |
| 112 | +); |
| 113 | +} |
0 commit comments