简体   繁体   English

MUI 芯片:onAdd (Material-UI) 等效 function?

[英]MUI Chip: onAdd (Material-UI) equivalent function?

I noticed that "onAdd" property is removed from the updated version of Material-UI, MUI.我注意到“onAdd”属性已从 Material-UI、MUI 的更新版本中删除。 The only property that is a function is "onDelete" and "onClick". function 的唯一属性是“onDelete”和“onClick”。 I want to create new chips depending on user input tags.我想根据用户输入标签创建新芯片。 Is there any equivalent way of doing so?有没有等效的方法?

You can use input fields to trigger the creation of new chips.您可以使用输入字段来触发新筹码的创建。 This jsFiddle demo (click "Run" to start the demo) has a button that creates chips.这个jsFiddle 演示(单击“运行”开始演示)有一个创建芯片的按钮。

The important code in that demo relating to your question is below under index.jsx .该演示中与您的问题相关的重要代码位于index.jsx下方。 The key items are:关键项目是:

  1. createNewChip function, createNewChip function,
  2. chipName TextField, and芯片名称文本字段,和
  3. Create New Chip button.创建新芯片按钮。

The createNewChip function acts as an event listener for the onClick event of the Create New Chip button. createNewChip function 充当Create New Chip按钮的onClick事件的事件侦听器。 createNewChip takes the text value from the chipName TextField and adds it to the list variable which is managed by React.useState . createNewChipchipName TextField 中获取文本值,并将其添加到由React.useState管理的列表变量中。

list is an array of chip data where each element is an object that looks something like this: list是一个芯片数据数组,其中每个元素是一个 object,看起来像这样:

{
    id: '11bf5b37-e0b8-42e0-8dcf-dc8c4aefc000',
    value: 'A Wonderful Chip',
    isValid: true
}

Hopefully, this helps to get you started on a solution.希望这有助于您开始解决问题。

index.jsx索引.jsx


...

export default function ChipMaker() {
    const [ list, setList ] = React.useState( [] );
    const selectedItems = list.filter( ( item ) => item.isValid );
    const selectedLengthIndex = selectedItems.length - 1;

    let listById = {};
    for ( let item of list ) {
        listById[ item.id ] = item;
    }

    ...

    function createNewChip() {
        let chipName = document.getElementById( 'chipName' ).value;
        let newList = list.concat( {
            id: uuidv4(),
            value: chipName,
            isValid: true
        } );

        setList( newList );
    }

    ...

    return (
        <Stack spacing={3} sx={{ width: 500 }}>
            <Autocomplete
                multiple
                id="tags-filled"
                filterSelectedOptions={ true }
                options={ list.map(( item ) => item.id) }
                value={ list.map(( item ) => item.id) }
                freeSolo
                renderTags={( listIds, getTagProps) =>
                    listIds.map(( id, index) => (
                        <Chip
                            key={ index }
                            variant="outlined"
                            label={ listById[ id ].value }
                            sx={ {
                                color: ( theme ) => {
                                    let chipColor = '#fff';
                                    if ( typeof( listById[ id ] ) == 'object' ) {
                                        chipColor = listById[ id ].isValid
                                            ? theme.palette.common.white 
                                            : theme.palette.common.white
                                    }
                                    return chipColor;
                                },

                                backgroundColor: ( theme ) => {
                                    let chipColor = '#fff';
                                    if ( typeof( listById[ id ] ) == 'object' ) {
                                        chipColor = listById[ id ].isValid
                                            ? theme.palette.primary.main 
                                            : theme.palette.error.main
                                    }
                                    return chipColor;
                                },
                            
                                [`& .MuiSvgIcon-root.MuiSvgIcon-fontSizeMedium.MuiChip-deleteIcon.MuiChip-deleteIconMedium.MuiChip-deleteIconColorDefault.MuiChip-deleteIconOutlinedColorDefault`]: {
                                    fill: ( theme ) => theme.palette.grey[200]                
                                }
                            } }
                            {...getTagProps({ index })}
                        />
                    ))
                }
                renderInput={(params) => (
                    <TextField
                        {...params}
                        variant="filled"
                        label="Material-UI Chip Input Test"
                        placeholder="Favorites"
                    />
                )}
                onChange={ validateInput }
            />

            <div>
                { selectedItems.map( ( item, index ) => {
                    let comma = null;
                    if ( selectedLengthIndex != index ) {
                        comma = (<span key={ 'idx'+index }>, </span>);
                    }

                    return (
                        item.isValid
                            ? <span key={ index }>{ item.value }{ comma }</span>
                            : null
                    );
                } ) }
            </div>

            <TextField
                id='chipName'
                name='chipName'
                className={ 'test' }
                type='text'
                label={ 'Chip name' }
                fullWidth={ false }
                variant='standard'
                inputProps={ { maxLength: 20 } }
                helperText={ 'Enter chip name' }
                InputLabelProps={ {
                    'variant': 'standard',
                    'color': 'primary',
                    'disableAnimation': false
                } }
                FormHelperTextProps={ { 'variant': 'standard' } }
                error={ false }
                defaultValue={ '' }
                placeholder={ 'New chip name' }
                color={ 'info' }
            />

            <Button
                variant={ 'contained' }
                onClick={ createNewChip }
                sx={ {
                    width: '200px'    
                } }
            >
                { 'Create new chip' }
            </Button>
        </Stack>
    );
}


/**
 * Inject component into DOM
 */
root.render(
    <ChipMaker />
);

Actually, I've just come to find out myself that MUI has a component called, "Autocomplete" which is the equivalent of making new chips (and adding tags from user input) with the "ChipInput" component from the older material-UI.实际上,我刚刚发现 MUI 有一个名为“自动完成”的组件,这相当于使用旧 material-UI 中的“ChipInput”组件制作新芯片(并从用户输入添加标签)。

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

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