These draggable rows are to be used for groups of settings, with similar controls being used, like the switch.
() => {const [options, setOptions] = React.useState([{ id: '1', label: 'Label', text: 'optional text 1', isToggleSelected: true, isDisabled: false },{ id: '2', label: 'Label', text: 'optional text 2', isToggleSelected: false, isDisabled: false },{ id: '3', label: 'Label', text: 'optional text 3', isToggleSelected: true, isDisabled: true },{ id: '4', label: 'Label', text: 'optional text 4', isToggleSelected: false, isDisabled: false },{ id: '5', label: 'Label', text: 'optional text 5', isToggleSelected: false, isDisabled: false },]);const setSelectedLists = () => {return options.filter(item => item.isToggleSelected).map(item => item.id);};const handleChangeValue = (selected, orderedList) => {return [selected, orderedList];};const handleInitialLoad = (selected, orderedList) => {return [selected, orderedList];};return (<DraggableRowsvariant="settings"onChangeValue={handleChangeValue}onInitialLoad={handleInitialLoad}options={options}setOptions={setOptions}primaryTextKey="label"secondaryTextKey="text"rowId="id"setSelectedLists={setSelectedLists}><div style={{ marginLeft: 'auto', display: 'flex', alignItems: 'center' }}><Button variant="text" onClick={() => alert('Button clicked!')}>Click Me</Buton></div></DraggableRows>);};
These draggable rows can be filled with text or other components.
() => {const [options, setOptions] = React.useState([{id: '1',label: 'Label',text: 'optional text 1',tagName: 'Success',tagVariant: 'success',isDisabled: false,},{id: '2',label: 'Label',text: 'optional text 2',tagName: 'Info',tagVariant: 'info',isDisabled: false,},{id: '3',label: 'Label',text: 'optional text 3',tagName: 'Danger',tagVariant: 'error',isDisabled: true,},]);const [state, setState] = React.useState(false);const handleChange = (e) => {setState((state) => (state ? false : e.currentTarget));};const updateOrder = (list) => {const optionIdMap = {};options.forEach((option) => {optionIdMap[option.id] = option;});const updatedOptions = list.map((id) => optionIdMap[id]);setOptions(updatedOptions);};return (<DraggableRows variant="flexible" updateOrder={updateOrder}>{options.map((item, index) => (<Boxid={item.id} // id is compulsory for draggable rows to work drag and dropkey={item.id}isDisabled={item.isDisabled}sx={{display: 'flex',alignItems: 'center',width: '100%',}}><Boxsx={{marginRight: '8px',whiteSpace: 'nowrap',overflow: 'hidden',textOverflow: 'ellipsis',}}><Typography>{item.label}</Typography></div><Boxsx={{flex: '0 1 20%',minWidth: '20%',marginRight: '24px',whiteSpace: 'nowrap',overflow: 'hidden',textOverflow: 'ellipsis',}}><Tagvariant={item.tagVariant}onClick={handleChange}open={Boolean(state)}hasMenu={item.tagVariant === 'info'}>{item.tagName}</Tag><MenuanchorEl={state}open={Boolean(state)}onClose={handleChange}anchorOrigin={{ horizontal: 'left', vertical: 'bottom' }}transformOrigin={{ horizontal: 'left', vertical: 'top' }}><MenuItem onClick={handleChange}>Item 1</MenuItem><MenuItem onClick={handleChange}>Item 2</MenuItem><MenuItem onClick={handleChange}>Item 3</MenuItem></Menu></div><Boxsx={{flex: '1 1 auto',whiteSpace: 'nowrap',overflow: 'hidden',textOverflow: 'ellipsis',}}><Typography> {item.text}</Typography></div><Boxsx={{flex: '0 0 auto',marginLeft: 'auto',display: 'flex',alignItems: 'center',}}><Switch /></div></div>))}</DraggableRows>);};
Prop | Type | Default | Description |
---|---|---|---|
variant | string | Can be either settings with toggle and primary secondary text or flexible can have user provided props. | |
rowId | string | Key in each option object representing the unique identifier. | |
secondaryTextKey | string | Key in each option object representing the text content. | |
primaryTextKey | string | Key in each option object representing the label content. | |
onChangeValue | function(selected: string[], orderedList: Character[]) | Callback function triggered when selected items change. | |
onInitialLoad | function(selected: string[], orderedList: Character[]) | Callback function triggered on initial load with selected items. | |
options | array | List of options to be displayed and managed in the drag and drop. | |
setOptions | function | Setter function to update the options list. | |
setSelectedLists | function | function for getting the id of the toggle selected items |