-
-
Notifications
You must be signed in to change notification settings - Fork 28
implementing combobox widget 🔥 #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ObjectDirective } from '@vue/runtime-core'; | ||
import { VNComboBox } from '../../widgets/ComboBox/VNComboBox'; | ||
|
||
type ModelDirective<T> = ObjectDirective<T & { _assign: Function }> | ||
|
||
export const vModelComboBox: ModelDirective<VNComboBox> = { | ||
beforeMount: (el, { value }, vnode) => { | ||
el.setCurrentIndex(value); | ||
// eslint-disable-next-line no-param-reassign, no-underscore-dangle | ||
el._assign = vnode.props!['onUpdate:modelValue'] as Function; | ||
el.addEventListener('currentIndexChanged', (indexValue) => { | ||
// eslint-disable-next-line no-underscore-dangle | ||
el._assign(indexValue); | ||
}); | ||
}, | ||
beforeUpdate: (el, { value, oldValue }) => { | ||
if (value === oldValue) { | ||
return; | ||
} | ||
el.setCurrentIndex(value); | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { | ||
QComboBox, NodeWidget, QSize, QVariant, SizeAdjustPolicy, InsertPolicy, QIcon, | ||
} from '@nodegui/nodegui'; | ||
import { VNWidget } from 'widgets/config'; | ||
import { ViewProps, viewPropsSetters } from '../View/VNView'; | ||
import { PropSetters, Prop } from '../../renderer/patchProp'; | ||
|
||
/** | ||
* The ComboBox component provides the ability to add and manipulate native combobox widgets. | ||
* It is based on [NodeGui's QComboBox](https://docs.nodegui.org/docs/api/generated/classes/qcombobox/). | ||
* | ||
* ## Usage | ||
* | ||
* ```html | ||
* <template> | ||
* <vn-view> | ||
* <vn-text>Choose a color</vn-text> | ||
* <vn-combobox :items="data" v-model="index" /> | ||
* <vn-text>You have picked: {{data[index].text}}</vn-text> | ||
* </vn-view> | ||
* </template> | ||
* | ||
* <script> | ||
* import { ref } from 'vue'; | ||
* | ||
* export default { | ||
* setup() { | ||
* const index = ref(0); | ||
* const data = [ | ||
* {text: "Red"}, | ||
* {text: "Yellow"}, | ||
* {text: "Blue"}, | ||
* {text: "Green"}, | ||
* ]; | ||
* | ||
* return { | ||
* index, | ||
* data, | ||
* } | ||
* } | ||
* }; | ||
* </script> | ||
* | ||
* ``` | ||
* | ||
* ## What it looks like? | ||
* | ||
*  | ||
* | ||
* ## Props and styling | ||
* | ||
* You can find all the props `vn-combobox` accepts listed below. | ||
* Apart from this, you can take a look at the [styling](/docs/guides/3-styling) | ||
* and [event handling](/docs/guides/5-handle-events) docs | ||
*/ | ||
|
||
export interface ComboBoxProps extends ViewProps { | ||
items?: ComboBoxItem[]; | ||
count?: number; | ||
iconSize?: QSize; | ||
frame?: boolean; | ||
currentIndex?: number; | ||
currentData?: QVariant; | ||
currentText?: string; | ||
duplicatesEnabled?: boolean; | ||
editable?: boolean; | ||
insertPolicy?: InsertPolicy; | ||
maxCount?: number; | ||
maxVisibleItems?: number; | ||
minimumContentsLength?: number; | ||
modelColumn?: number; | ||
sizeAdjustPolicy?: SizeAdjustPolicy; | ||
} | ||
|
||
type ComboBoxItem = { | ||
text: string; | ||
icon?: QIcon; | ||
userData?: QVariant; | ||
}; | ||
|
||
const comboBoxPropsSetters: PropSetters<VNComboBox, ComboBoxProps> = { | ||
...viewPropsSetters, | ||
items(widget: VNComboBox, _, items: ComboBoxItem[]) { | ||
widget.clear(); | ||
items.forEach((item) => { | ||
widget.addItem(item.icon, item.text, item.userData); | ||
}); | ||
}, | ||
count(widget: VNComboBox, _, count: number) { | ||
widget.setProperty('count', count); | ||
}, | ||
iconSize(widget: VNComboBox, _, iconSize: QSize) { | ||
widget.setProperty('iconSize', iconSize.native); | ||
}, | ||
frame(widget: VNComboBox, _, frame: boolean) { | ||
widget.setProperty('frame', frame); | ||
}, | ||
currentIndex(widget: VNComboBox, _, currentIndex: number) { | ||
widget.setCurrentIndex(currentIndex); | ||
}, | ||
currentData(widget: VNComboBox, _, currentData: QVariant) { | ||
widget.setProperty('currentData', currentData.native); | ||
}, | ||
currentText(widget: VNComboBox, _, currentText: string) { | ||
widget.setCurrentText(currentText); | ||
}, | ||
editable(widget: VNComboBox, _, editable: boolean) { | ||
widget.setEditable(editable); | ||
}, | ||
duplicatesEnabled(widget: VNComboBox, _, duplicatesEnabled: boolean) { | ||
widget.setProperty('duplicatesEnabled', duplicatesEnabled); | ||
}, | ||
insertPolicy(widget: VNComboBox, _, insertPolicy: InsertPolicy) { | ||
widget.setProperty('insertPolicy', insertPolicy); | ||
}, | ||
maxCount(widget: VNComboBox, _, maxCount: number) { | ||
widget.setProperty('maxCount', maxCount); | ||
}, | ||
maxVisibleItems(widget: VNComboBox, _, maxVisibleItems: number) { | ||
widget.setMaxVisibleItems(maxVisibleItems); | ||
}, | ||
minimumContentsLength(widget: VNComboBox, _, minimumContentsLength: number) { | ||
widget.setProperty('minimumContentsLength', minimumContentsLength); | ||
}, | ||
modelColumn(widget: VNComboBox, _, modelColumn: number) { | ||
widget.setProperty('modelColumn', modelColumn); | ||
}, | ||
sizeAdjustPolicy(widget: VNComboBox, _, sizeAdjustPolicy: SizeAdjustPolicy) { | ||
widget.setSizeAdjustPolicy(sizeAdjustPolicy); | ||
}, | ||
}; | ||
|
||
/** @internal */ | ||
export class VNComboBox extends QComboBox implements VNWidget<ComboBoxProps> { | ||
patchProp( | ||
key: keyof ComboBoxProps, | ||
prevValue: Prop<ComboBoxProps, typeof key>, | ||
nextValue: Prop<ComboBoxProps, typeof key>, | ||
) { | ||
const propSetter = comboBoxPropsSetters[key]; | ||
if (propSetter !== undefined) { propSetter(this, prevValue as never, nextValue as never); } | ||
} | ||
|
||
insertChild() { | ||
throw new Error('Cannot add child to ComboBox elements'); | ||
} | ||
|
||
getNextSibling(): NodeWidget<any> | null { | ||
throw new Error('ComboBox elements cannot have children'); | ||
} | ||
|
||
insertBefore() { | ||
throw new Error('Cannot add child to ComboBox elements'); | ||
} | ||
|
||
removeChild() { | ||
throw new Error('Cannot remove/add child to ComboBox elements'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { WidgetConfig } from '../config'; | ||
import { VNComboBox, ComboBoxProps } from './VNComboBox'; | ||
|
||
class ComboBoxConfig implements WidgetConfig<ComboBoxProps> { | ||
parentNode: any; | ||
|
||
createElement() { | ||
return new VNComboBox(); | ||
} | ||
} | ||
|
||
export default ComboBoxConfig; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Used the currentIndex value for implementing v-model – would this be okay?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, index feels safer than the text event.