three.cad/src/react/dialog.jsx

74 lines
1.6 KiB
React
Raw Normal View History

2021-04-15 16:36:54 +08:00
import React, { useEffect, useReducer, useRef, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux'
import { MdDone, MdClose } from 'react-icons/md'
import * as Icon from "./icons";
2021-04-17 11:54:01 +08:00
export const Dialog = () => {
const dialog = useSelector(state => state.ui.dialog)
2021-04-15 16:36:54 +08:00
const dispatch = useDispatch()
const ref = useRef()
2021-04-17 11:54:01 +08:00
2021-04-15 16:36:54 +08:00
useEffect(() => {
2021-04-17 11:54:01 +08:00
if (!ref.current) return
2021-04-15 16:36:54 +08:00
ref.current.focus()
2021-04-17 11:54:01 +08:00
}, [dialog])
2021-04-15 16:36:54 +08:00
const extrude = () => {
2021-04-17 11:54:01 +08:00
sc.extrude(dialog.target, ref.current.value)
2021-04-17 07:03:30 +08:00
sc.render()
2021-04-15 16:36:54 +08:00
2021-04-17 11:54:01 +08:00
dispatch({ type: "clear-dialog" })
forceUpdate()
}
2021-04-15 16:36:54 +08:00
const [_, forceUpdate] = useReducer(x => x + 1, 0);
2021-04-17 11:54:01 +08:00
switch (dialog.action) {
case 'extrude':
return <>
<input className='w-1/2' type="number" defaultValue="1" step="0.1" ref={ref} />
<Icon.Flip className="btn w-auto h-full p-2"
onClick={() => ref.current.value *= -1}
/>
<MdDone
className="btn w-auto h-full p-2"
onClick={extrude}
/>
<MdClose className="btn w-auto h-full p-2"
onClick={() => dispatch({ type: "clear-dialog" })}
/>
</>
case 'sketch':
return <>
<MdDone
className="btn w-auto h-full p-2"
onClick={() => {
// dispatch({ type: 'update-descendents', sketch})
sc.activeSketch.deactivate()
sc.render()
forceUpdate()
}}
/>
<MdClose className="btn w-auto h-full p-2"
onClick={() => dispatch({ type: "clear-dialog" })}
/>
</>
default:
return null
}
2021-04-15 16:36:54 +08:00
}
2021-04-17 11:54:01 +08:00