UNPKG

@coveord/plasma-mantine

Version:

A Plasma flavoured Mantine theme

41 lines (40 loc) 1.04 kB
import { useUncontrolled } from '@mantine/hooks'; /** * Manage a list of items in a controlled fashion, to be used with inputs */ export const useControlledList = ({ defaultValue, value, onChange })=>{ const [values, handleChange] = useUncontrolled({ value, defaultValue, finalValue: [], onChange }); const remove = (index)=>{ const newValues = values.filter((_, i)=>i !== index); handleChange?.(newValues); }; const append = (item)=>{ const newValues = [ ...values, item ]; handleChange?.(newValues); }; const reorder = ({ from, to })=>{ const newValues = [ ...values ]; const item = values[from]; newValues.splice(from, 1); newValues.splice(to, 0, item); handleChange?.(newValues); }; return [ values, { remove, append, reorder } ]; }; //# sourceMappingURL=useControlledList.js.map