简体   繁体   English

如何将按钮添加到 MUI TextField Select 组件选择选项?

[英]How to add a button to MUI TextField Select component selected option?

I have a MUI v5 Textfield select component.我有一个 MUI v5 Textfield select组件。 Each option has a play button , avatar and option text .每个选项都有一个play buttonavataroption text Everything works fine in dropdown list.在下拉列表中一切正常。 But how can I make play button work (do something) if it is selected?但是,如果它被选中,我怎样才能让play button工作(做某事)呢? At the moment if user clicks on it then option list opens.目前,如果用户单击它,则会打开选项列表。

const StyledSelect = ({ ...props }) => {
  return (
    <TextField {...props} select fullWidth>
      {props.children}
    </TextField>
  );
};

export const AudioSelect = () => {
  return (
    <StyledSelect>
      {AUDIO_OPTIONS.map(({ id, name, avatar, voice }) => (
        <MenuItem key={id} value={id}>
          <Stack>
            <PlayAudioButton
              sound={voice}
              key={id}
              onPlay={() => console.log("play button " + id)}
              isPlaying={false}
            />
            <Avatar src={avatar} />
            <Typography>{name}</Typography>
          </Stack>
        </MenuItem>
      ))}
    </StyledSelect>
  );
};

Here's codesandbox: https://codesandbox.io/s/silly-monad-6z3144?file=/src/AudioSelect.tsx这是 codesandbox: https://codesandbox.io/s/silly-monad-6z3144?file=/src/AudioSelect.tsx

Add another propagation stopping in onMouseDown did the trickonMouseDown中添加另一个传播停止就可以了

  return (
    <IconButton
      onMouseDown={(e) => {
        e.stopPropagation();
      }}
      onClick={(e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
        e.stopPropagation();
        onPlay();
      }}
    >
      {isPlaying && !isAudioEnded ? <PauseIcon /> : <PlayArrowRoundedIcon />}
    </IconButton>
  );

编辑 wonderful-silence-6movrx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM