import * as React from "react"; import { useState } from "react"; import Switch from "@material-ui/core/Switch"; import FormControlLabel from "@material-ui/core/FormControlLabel"; import RemoveIcon from "@material-ui/icons/Remove"; import AddIcon from "@material-ui/icons/Add"; import { isNumber } from "@sc/plugins/utilities"; import { ButtonSliderGroup } from "../../groups"; import { LineHeightProps } from "./types"; const filterLineHeight = (value) => { return value.replace("pt", ""); }; /** * Creates a Button Slider Group to Change the Components LineHeight */ const LineHeight: React.FC = ({ isMax, isAuto, minValue = 6, maxValue = 72, showMaxCheck, showAutoCheck, autoCheckValue = "normal", style, onChange, label = "Line Height", __testSliderValue = 111, }) => { const value: any = style.lineHeight; const [lineHeight, setLineHeight] = useState(value); const [initialValue, setInitialValue] = useState(value); const [maxIsChecked, setMaxIsChecked] = useState(isMax); const [autoIsChecked, setAutoIsChecked] = useState(isAuto); const handleChange = ( command: string, newValue: number | React.ReactText, skipUncheck: boolean = false ) => { // console.log({ command, newValue }); let value = newValue; // max & min enforcement if (newValue > maxValue && isNumber(newValue)) value = maxValue; if (newValue < minValue && isNumber(newValue)) value = minValue; // button commands switch (command) { case "GROW": value = Number(value.toString().replace("pt", "")) + 1; break; case "SHRINK": value = Number(value.toString().replace("pt", "")) - 1; break; } setLineHeight(value as number); // Should immediately uncheck the AutoCheck and MaxCheck checkbox when the button is clicked or slider is changed if (!skipUncheck) { setMaxIsChecked(false); setAutoIsChecked(false); } if (newValue === maxValue) setMaxIsChecked(true); const returnNumber = value.toString().replace("pt", ""); const lineHeight = isNumber(returnNumber) ? `${returnNumber}pt` : returnNumber; onChange({ lineHeight }); }; const handleMaxCheck = ( event: React.ChangeEvent, checked: boolean ) => { // Should set the value to the maxCheckValue prop's value when the [Max] checkbox is checked setMaxIsChecked(checked); if (checked) { setInitialValue(lineHeight); handleChange(null, maxValue, true); } else { handleChange(null, initialValue); } }; const handleAutoCheck = ( event: React.ChangeEvent, checked: boolean ) => { // Should set the value to the autoCheckValue prop's value when the [Auto] checkbox is checked setAutoIsChecked(checked); if (checked) { setInitialValue(lineHeight); handleChange(null, autoCheckValue, true); } else { handleChange(null, initialValue); } }; return (
, }} button2={{ command: "GROW", icon: , }} __testSliderValue={__testSliderValue} > {showMaxCheck && ( } label="Max" /> )} {showAutoCheck && ( } label="Auto Size" /> )}
); }; export default LineHeight;