OutlineView icon indicating copy to clipboard operation
OutlineView copied to clipboard

Is there a way to make multiple choices?

Open wogus3602 opened this issue 4 years ago • 6 comments

Currently, only single selection seems to be supported. How about making Selection support Set<Data.Element>?

wogus3602 avatar Mar 06 '22 13:03 wogus3602

Not currently. But, I’m happily accepting PRs 🙂

Sameesunkaria avatar Mar 06 '22 14:03 Sameesunkaria

Currently testing. If it goes well, I'll send a PR!

wogus3602 avatar Mar 06 '22 14:03 wogus3602

@wogus3602 have you made any progress on this? If not, I'd be interested in helping work on it.

I haven't done anything like this, but I think the challenging part for me would be how to allow initializers to use either Binding<Set<Data.Element>> or Binding<Data.Element>, while the OutlineView's internal variable is... what? Binding to a Set with a hard limit to one element if the initializer was the single-selection variety? How did you plan on going about it?

RCCoop avatar Feb 03 '23 14:02 RCCoop

@RCCoop I worked on multi-select as below.

    @Binding var selection: Set<Item>
        
    init(
        selection: Binding<Item?>,
        children childrenKeyPath: ChildrenKeyPath<Item>? = nil,
        content: @escaping ListItemContentType<Item>
    ) {
        self._selection = .init {
            if let sel = selection.wrappedValue {
                return Set([sel])
            }
            
            return Set()
        } set: { newValue in
            selection.wrappedValue = newValue.first
        }
        
        self.content = content
        self.childrenKeyPath = childrenKeyPath
    }
    
    init(
        selection: Binding<Set<Item>>,
        children childrenKeyPath: ChildrenKeyPath<Item>? = nil,
        content: @escaping ListItemContentType<Item>
    ) {
        self._selection = selection
        self.content = content
        self.childrenKeyPath = childrenKeyPath
    }

wogus3602 avatar Feb 07 '23 08:02 wogus3602

@RCCoop The multi-select work is complete, so I'll post a PR soon.

wogus3602 avatar Feb 07 '23 08:02 wogus3602

@wogus3602 Very cool! Thanks for working on that :-)

RCCoop avatar Feb 07 '23 15:02 RCCoop