|
| 1 | +import { classes, properties, useResource, useTitle } from '@tomic/react'; |
| 2 | +import React, { FormEvent, useCallback, useState } from 'react'; |
| 3 | +import { Button } from '../Button'; |
| 4 | +import { |
| 5 | + Dialog, |
| 6 | + DialogActions, |
| 7 | + DialogContent, |
| 8 | + DialogTitle, |
| 9 | + useDialog, |
| 10 | +} from '../Dialog'; |
| 11 | +import Field from '../forms/Field'; |
| 12 | +import { InputStyled, InputWrapper } from '../forms/InputStyles'; |
| 13 | +import { Base } from './Base'; |
| 14 | +import { useCreateAndNavigate } from './useCreateAndNavigate'; |
| 15 | +import { NewInstanceButtonProps } from './NewInstanceButtonProps'; |
| 16 | + |
| 17 | +export function NewFolderButton({ |
| 18 | + klass, |
| 19 | + subtle, |
| 20 | + icon, |
| 21 | + parent, |
| 22 | + children, |
| 23 | + label, |
| 24 | +}: NewInstanceButtonProps): JSX.Element { |
| 25 | + const resource = useResource(klass); |
| 26 | + const [title] = useTitle(resource); |
| 27 | + const [name, setName] = useState(''); |
| 28 | + |
| 29 | + const [dialogProps, show, hide] = useDialog(); |
| 30 | + |
| 31 | + const createResourceAndNavigate = useCreateAndNavigate(klass, parent); |
| 32 | + |
| 33 | + const onDone = useCallback( |
| 34 | + (e: FormEvent) => { |
| 35 | + e.preventDefault(); |
| 36 | + |
| 37 | + createResourceAndNavigate('Folder', { |
| 38 | + [properties.name]: name, |
| 39 | + [properties.displayStyle]: 'list', |
| 40 | + [properties.isA]: [classes.folder], |
| 41 | + }); |
| 42 | + }, |
| 43 | + [name], |
| 44 | + ); |
| 45 | + |
| 46 | + return ( |
| 47 | + <> |
| 48 | + <Base |
| 49 | + onClick={show} |
| 50 | + title={title} |
| 51 | + icon={icon} |
| 52 | + subtle={subtle} |
| 53 | + label={label} |
| 54 | + > |
| 55 | + {children} |
| 56 | + </Base> |
| 57 | + <Dialog {...dialogProps}> |
| 58 | + <DialogTitle> |
| 59 | + <h1>New Folder</h1> |
| 60 | + </DialogTitle> |
| 61 | + <DialogContent> |
| 62 | + <form onSubmit={onDone}> |
| 63 | + <Field required label='url'> |
| 64 | + <InputWrapper> |
| 65 | + <InputStyled |
| 66 | + placeholder='New Folder' |
| 67 | + value={name} |
| 68 | + autoFocus={true} |
| 69 | + onChange={e => setName(e.target.value)} |
| 70 | + /> |
| 71 | + </InputWrapper> |
| 72 | + </Field> |
| 73 | + </form> |
| 74 | + </DialogContent> |
| 75 | + <DialogActions> |
| 76 | + <Button onClick={hide} subtle> |
| 77 | + Cancel |
| 78 | + </Button> |
| 79 | + <Button onClick={onDone} disabled={name.trim() === ''}> |
| 80 | + Ok |
| 81 | + </Button> |
| 82 | + </DialogActions> |
| 83 | + </Dialog> |
| 84 | + </> |
| 85 | + ); |
| 86 | +} |
0 commit comments