Skip to content
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

Add Rule Identifier to RuleSlot #83

Merged
merged 5 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/styling.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ The `rule` slot allows for customizing markup around each rule component.

The slot receives an object with the shape of the [RuleSlotProps
object](https://github.com/rtucek/vue-query-builder/blob/master/types/index.d.ts#L47).
An exact rule can be identified based on the `ruleCtrl.ruleIdentifier` for dynamic content.

You'll have to use Vue's [Dynamic
Component](https://vuejs.org/v2/guide/components.html#Dynamic-Components) feature for displaying the
Expand Down
1 change: 1 addition & 0 deletions src/QueryBuilderRule.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class QueryBuilderRule extends Vue {
return {
ruleComponent: this.component,
ruleData: this.query.value,
ruleIdentifier: this.query.identifier,
updateRuleData: (ruleData: any) => this.ruleUpdate(ruleData),
};
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface GroupCtrlSlotProps {
export interface RuleSlotProps {
ruleComponent: Component | string,
ruleData: any,
ruleIdentifier: string,
updateRuleData: (newData: any) => void,
}

Expand Down
4 changes: 4 additions & 0 deletions tests/components/Component.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export default class Input extends Vue {
@Prop({
default: null,
}) readonly value!: any;

@Prop({
default: null,
}) readonly identifier!: string;
}
</script>

Expand Down
3 changes: 3 additions & 0 deletions tests/unit/slots.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ describe('Testing slot related features', () => {
<component
:is="props.ruleComponent"
:value="props.ruleData"
:identifier="props.ruleIdentifier"
@input="props.updateRuleData"
class="slot-rule"
/>
Expand All @@ -197,6 +198,8 @@ describe('Testing slot related features', () => {
// Verify rule slot is properly rendered
expect(ruleComponent.is(Component)).toBeTruthy();
expect(ruleComponent.vm.$props.value).toBe('A');
expect(rule.vm.$props.query.identifier).toBe('txt');
expect(ruleComponent.vm.$props.identifier).toBe('txt');
ruleComponent.vm.$emit('input', 'a');
expect((rule.emitted('query-update') as any)[0][0]).toStrictEqual({ identifier: 'txt', value: 'a' });

Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ export interface GroupCtrlSlotProps {
export interface RuleSlotProps {
ruleComponent: Component | string,
ruleData: any,
ruleIdentifier: string,
updateRuleData: (newData: any) => void,
}