-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAspectConfigModal.tsx
More file actions
97 lines (90 loc) · 2.84 KB
/
Copy pathAspectConfigModal.tsx
File metadata and controls
97 lines (90 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { type Aspect, ToggleGroup } from '@h5web/lib';
import { useState } from 'react';
import type { PropsWithChildren } from 'react';
import { MdAspectRatio } from 'react-icons/md';
import LabelledInput from './LabelledInput';
import type { IIconType } from './Modal';
import Modal from './Modal';
import styles from './Modal.module.css';
import { getAspectType, isValidPositiveNumber } from './utils';
/**
* Props for the `AspectConfigModal` component.
*/
interface AspectConfigModalProps {
/** The current value of the aspect */
aspect: Aspect;
/** The function to update aspect state */
setAspect: (value: Aspect) => void;
/** If true, hide toggle */
hideToggle?: boolean;
/** The children to render inside the modal (optional) */
}
/**
* Render the configuration options for the aspect ratio.
* @param {AspectConfigModalProps} props - The component props.
* @returns {JSX.Element} {Modal} The rendered component.
*/
function AspectConfigModal(props: PropsWithChildren<AspectConfigModalProps>) {
const { aspect: initAspect, setAspect, children } = props;
const [aspectType, setAspectType] = useState('number');
const [aspectRatio, setAspectRatio] = useState<number>(2.0);
const initType = getAspectType(initAspect);
if (initType != aspectType) {
console.log('Set initial type', props);
setAspectType(initType);
if (initType === 'number') {
setAspectRatio(initAspect as number);
}
}
function handleAspectTypeChange(val: string) {
setAspectType(val);
if (val === 'number') {
setAspect(aspectRatio);
} else {
setAspect(val as Aspect);
}
}
return Modal({
title: 'Aspect ratio',
icon: MdAspectRatio as IIconType,
hideToggle: props.hideToggle,
children: (
<>
<div className={styles.aspect}>
<LabelledInput<number>
key="0"
disabled={aspectType !== 'number'}
label="aspect ratio"
input={aspectRatio}
isValid={(v) => isValidPositiveNumber(v, 10)}
inputAttribs={{
name: 'digits',
pattern: '^\\d+|\\d+.\\d*$',
size: 3,
}}
updateValue={(v) => {
setAspectRatio(v);
setAspect(v);
}}
submitLabel="update ratio"
/>
</div>
<div className={styles.aspect}>
<ToggleGroup
role="radiogroup"
ariaLabel="aspect"
value={aspectType}
onChange={handleAspectTypeChange}
>
<ToggleGroup.Btn label="number" value="number" />
<ToggleGroup.Btn label="auto" value="auto" />
<ToggleGroup.Btn label="equal" value="equal" />
</ToggleGroup>
{children}
</div>
</>
),
});
}
export type { AspectConfigModalProps };
export default AspectConfigModal;