three.cad/src/react/dialog.jsx

59 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 { 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 = () => {
if (sc.activeSketch) {
sc.extrude(sc.activeSketch, ref.current.value)
2021-04-16 08:05:40 +08:00
setDialog(null)
2021-04-15 16:36:54 +08:00
} else if (sc.selected.length === 1 && sc.selected[0].userData.type == 'sketch') {
sc.extrude(treeEntriesById[sc.selected[0].name], ref.current.value)
2021-04-16 08:05:40 +08:00
setDialog(null)
2021-04-15 16:36:54 +08:00
} else {
console.log('invalid selection')
}
}
const [_, forceUpdate] = useReducer(x => x + 1, 0);
return <div className='dialog w-40 h-10 flex items-center bg-gray-700'>
<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 };
}