three.cad/src/react/dialog.jsx

66 lines
1.7 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 { GiVerticalFlip } from 'react-icons/gi'
import * as Icon from "./icons";
2021-04-16 08:05:40 +08:00
export const Dialog = ({ dialog, setDialog }) => {
if (!dialog) return null
2021-04-15 16:36:54 +08:00
const dispatch = useDispatch()
const treeEntriesById = useSelector(state => state.treeEntries.byId)
const activeSketchId = useSelector(state => state.treeEntries.activeSketchId)
const ref = useRef()
useEffect(() => {
ref.current.focus()
}, [])
const extrude = () => {
2021-04-17 07:03:30 +08:00
let sketch
2021-04-15 16:36:54 +08:00
if (sc.activeSketch) {
2021-04-17 07:03:30 +08:00
sketch = sc.activeSketch
2021-04-15 16:36:54 +08:00
} else if (sc.selected.length === 1 && sc.selected[0].userData.type == 'sketch') {
2021-04-17 07:03:30 +08:00
sketch = treeEntriesById[sc.selected[0].name]
2021-04-15 16:36:54 +08:00
} else {
console.log('invalid selection')
2021-04-17 07:03:30 +08:00
return
2021-04-15 16:36:54 +08:00
}
2021-04-17 07:03:30 +08:00
setDialog(null)
sc.extrude(sketch, ref.current.value)
sc.render()
forceUpdate()
2021-04-15 16:36:54 +08:00
}
const [_, forceUpdate] = useReducer(x => x + 1, 0);
return <div className='dialog w-40 h-10 flex items-center bg-gray-700'>
2021-04-17 07:03:30 +08:00
{/* return <div className='w-40 h-full flex items-center bg-gray-700'> */}
2021-04-15 16:36:54 +08:00
<input className='w-1/2' type="number" {...useNumField(1)} step="0.1" ref={ref} />
2021-04-16 08:05:40 +08:00
<Icon.Flip className="btn w-auto h-full p-2"
onClick={() => ref.current.value *= -1}
/>
2021-04-15 16:36:54 +08:00
<MdDone
2021-04-16 08:05:40 +08:00
className="btn w-auto h-full p-2"
2021-04-15 16:36:54 +08:00
onClick={extrude}
2021-04-16 08:05:40 +08:00
/>
<MdClose className="btn w-auto h-full p-2"
onClick={() => setDialog(null)}
/>
2021-04-15 16:36:54 +08:00
</div>
}
export const useNumField = (initValue = 0) => {
const [value, setValue] = useState(initValue);
const onChange = e => setValue(e.target.value);
return { value, onChange };
}