簡體   English   中英

Modal 內的滑塊不起作用 - React + Material UI

[英]Slider inside of Modal is not working - React + Material UI

我正在嘗試在 MUI 模態組件中附加自定義滑塊組件。

我的滑塊在故事書上工作得很好,這是預期的行為:

預期行為

但是當我將它添加到 Material UI 模式中時,這是行為:

當前行為

我真的不知道會發生什么......我已經嘗試使用另一個滑塊庫制作我的自定義模式(沒有 MUI)並且它們的行為都相同。

當我嘗試在模態上移動滑塊時收到此警告:

Slider.js:770 [Violation] Added non-passive event listener to a scroll-blocking 'touchstart' event. 
Consider marking the event handler as 'passive' to make the page more responsive. 
See https://www.chromestatus.com/feature/5745543795965952

這是我的滑塊代碼(強調一下,它在模態之外完美運行:

import React from "react";
import {
  styled,
  Grid,
  Slider as MUISlider,
  InputBase,
  Tooltip,
} from "@material-ui/core";

const CustomSlider = styled(MUISlider)(({ theme }) => ({
  color: theme.palette.secondary.light,
  width: 86,
}));

const GasInput = styled(InputBase)(({ theme }) => ({
  color: theme.palette.secondary.light,
  width: 48,
  height: 32,
  border: "1px solid #ECEFF3",
  borderRadius: 4,
  background: "#FAFCFF",
  fontSize: 12,
  boxSizing: "border-box",
  padding: 12,
}));

const SliderContainer = styled(Grid)({
  width: 200,
  height: 20,
  marginTop: -10,
});

const Input = styled(Grid)({
  paddingLeft: 8,
});

export interface SliderProps {
  value: number;
  min: number;
  max: number;
  onChangeValue: (value: number) => void;
}

interface Props {
  children: React.ReactElement;
  open: boolean;
  value: number;
}

function ValueLabelComponent(props: Props) {
  const { children, open, value } = props;

  return (
    <Tooltip open={open} enterTouchDelay={0} placement="top" title={value}>
      {children}
    </Tooltip>
  );
}

export function Slider({ min, max, value, onChangeValue }: SliderProps) {
  const handleSliderChange = (
    _: React.ChangeEvent<unknown>,
    value: number | number[]
  ) => {
    onChangeValue(Number(value));
  };

  const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    onChangeValue(parseInt(event.target.value, 10));
  };

  return (
    <SliderContainer
      container
      direction="row"
      alignItems="center"
      justify="flex-end"
    >
      <Grid item>
        <CustomSlider
          ValueLabelComponent={ValueLabelComponent}
          min={min}
          max={max}
          value={value}
          onChange={handleSliderChange}
        />
      </Grid>
      <Input item>
        <GasInput type="number" value={value} onChange={handleInputChange} />
      </Input>
    </SliderContainer>
  );
}

我使用您的代碼創建了一個示例 一切似乎都按預期工作。 將您的本地代碼與該代碼進行比較。

感謝@jack.benson 的回答,我能夠找出到底發生了什么(非常感謝先生)。

我創建了一個模態組件,它抽象了我將在整個應用程序中使用的模態的主要內容:

import React from "react";
import { Modal as MUIModal, Box, styled } from "@material-ui/core";

interface ModalProps {
  width: number;
  height: number;
  children: React.ReactNode;
  open: boolean;
}

export function Modal({ width, height, children, open }: ModalProps) {
  const ModalContainer = styled(MUIModal)({
    height,
    width,
    margin: "auto",
    borderRadius: 12,
  });

  const ModalBox = styled(Box)({
    height,
    width,
    background: "#FFFFFF",
    borderRadius: 12,
    outline: "none",
  });

  return (
    <ModalContainer open={open}>
      <ModalBox>{children}</ModalBox>
    </ModalContainer>
  );
}

如您所見,我正在使用styled函數來設置我的組件的樣式。 這就是給我帶來問題的原因。 我不知道為什么是這個原因,但是如果我從styled移動到makeStyles它將完美地工作,這是我的模態組件的新代碼:

import React from "react";
import { Modal as MUIModal, Box, makeStyles } from "@material-ui/core";

interface ModalProps {
  width: number;
  height: number;
  children: React.ReactNode;
  open: boolean;
}

const useStyles = ({ height, width }: Partial<ModalProps>) => makeStyles({
  root: {
    height: `${height}px`,
    width: `${width}px`,
    margin: "auto",
    borderRadius: 12,
  },
  box: {
    height: `${height}px`,
    width: `${width}px`,
    background: "#FFFFFF",
    borderRadius: 12,
    outline: 0,
  }
});

export function Modal({ width, height, children, open }: ModalProps) {
  const classes = useStyles({ width, height })()
  return (
    <MUIModal className={classes.root} open={open}>
      <Box className={classes.box}>{children}</Box>
    </MUIModal>
  );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM