|
1 | 1 | <template>
|
2 |
| - <v-dialog v-model="dialog" max-width="480"> |
3 |
| - <v-form ref="form" v-model="valid" lazy-validation> |
| 2 | + <v-dialog v-model="state.dialog" max-width="480"> |
| 3 | + <v-form ref="form" v-model="state.valid" lazy-validation> |
4 | 4 | <v-card>
|
5 | 5 | <v-card-title primary-title>
|
6 | 6 | <span class="title" v-text="title" />
|
7 | 7 | </v-card-title>
|
8 | 8 | <v-card-text>
|
9 | 9 | <v-select
|
10 |
| - v-model="formInputs.field" |
| 10 | + v-model="state.formInputs.field" |
11 | 11 | :items="fields"
|
12 | 12 | label="Field"
|
13 | 13 | dense
|
14 | 14 | class="pt-3"
|
15 | 15 | />
|
16 | 16 | <v-select
|
17 |
| - v-model="formInputs.condition" |
| 17 | + v-model="state.formInputs.condition" |
18 | 18 | :items="conditions"
|
19 | 19 | label="Condition"
|
20 | 20 | dense
|
21 | 21 | class="pt-3"
|
22 | 22 | />
|
23 | 23 | <v-text-field
|
24 |
| - v-model="formInputs.value" |
| 24 | + v-model="state.formInputs.value" |
25 | 25 | :rules="valueRules"
|
26 | 26 | label="Value"
|
27 | 27 | :placeholder="placeholder"
|
28 | 28 | required
|
29 | 29 | autofocus
|
30 | 30 | />
|
31 | 31 | <v-select
|
32 |
| - v-model="formInputs.action" |
| 32 | + v-model="state.formInputs.action" |
33 | 33 | :items="actions"
|
34 | 34 | label="Action"
|
35 | 35 | dense
|
36 | 36 | class="pt-3"
|
37 | 37 | />
|
38 | 38 | <v-switch
|
39 |
| - v-model="formInputs.active" |
40 |
| - :label="formInputs.active ? 'Active' : 'Inactive'" |
| 39 | + v-model="state.formInputs.active" |
| 40 | + :label="state.formInputs.active ? 'Active' : 'Inactive'" |
41 | 41 | />
|
42 | 42 | </v-card-text>
|
43 | 43 | <v-card-actions>
|
44 | 44 | <v-spacer />
|
45 |
| - <v-btn text @click.native="onClickClose">Cancel</v-btn> |
46 |
| - <v-btn color="primary" text @click.native="onClickSave">Save</v-btn> |
| 45 | + <v-btn text @click.native="handleClickClose">Cancel</v-btn> |
| 46 | + <v-btn color="primary" text @click.native="handleClickSave"> |
| 47 | + Save |
| 48 | + </v-btn> |
47 | 49 | </v-card-actions>
|
48 | 50 | </v-card>
|
49 | 51 | </v-form>
|
50 | 52 | </v-dialog>
|
51 | 53 | </template>
|
52 | 54 |
|
53 | 55 | <script lang="ts">
|
54 |
| -import { Vue, Component, Prop, Watch, Ref } from 'vue-property-decorator' |
| 56 | +import { |
| 57 | + defineComponent, |
| 58 | + computed, |
| 59 | + ref, |
| 60 | + reactive, |
| 61 | + watch, |
| 62 | + SetupContext, |
| 63 | +} from '@vue/composition-api' |
55 | 64 | import { VForm } from 'vuetify/lib'
|
56 | 65 | import Rule from '~/models/rule'
|
57 | 66 |
|
58 |
| -@Component |
59 |
| -export default class RuleDialog extends Vue { |
60 |
| - @Prop({ type: Boolean, required: true }) readonly value!: boolean |
61 |
| - @Prop({ type: Object, default: () => ({}) }) readonly inputs!: object |
62 |
| - @Prop({ type: String, default: 'New Rule' }) readonly title!: string |
63 |
| - @Ref() readonly form!: typeof VForm |
| 67 | +const fields = [ |
| 68 | + { text: 'Author', value: 'author' }, |
| 69 | + { text: 'Message', value: 'message' }, |
| 70 | +] |
| 71 | +const conditions = [ |
| 72 | + { text: 'Contains', value: 'contains' }, |
| 73 | + { text: 'Matches Regular Expression', value: 'matches_regular_expression' }, |
| 74 | +] |
| 75 | +const actions = [ |
| 76 | + { text: 'Mask Message', value: 'mask_message' }, |
| 77 | + { text: 'Hide Completely', value: 'hide_completely' }, |
| 78 | +] |
| 79 | +const valueRules = [(v: string) => !!v || 'Value is required'] |
64 | 80 |
|
65 |
| - fields = [ |
66 |
| - { text: 'Author', value: 'author' }, |
67 |
| - { text: 'Message', value: 'message' }, |
68 |
| - ] |
69 |
| - conditions = [ |
70 |
| - { text: 'Contains', value: 'contains' }, |
71 |
| - { text: 'Matches Regular Expression', value: 'matches_regular_expression' }, |
72 |
| - ] |
73 |
| - actions = [ |
74 |
| - { text: 'Mask Message', value: 'mask_message' }, |
75 |
| - { text: 'Hide Completely', value: 'hide_completely' }, |
76 |
| - ] |
77 |
| - valueRules = [(v: string) => !!v || 'Value is required'] |
78 |
| - valid = false |
79 |
| - dialog = false |
80 |
| - formInputs: Partial<Rule> = {} |
| 81 | +type Props = { |
| 82 | + value: boolean |
| 83 | + title: string |
| 84 | + inputs: Partial<Rule> |
| 85 | +} |
81 | 86 |
|
82 |
| - get placeholder() { |
83 |
| - return this.formInputs.condition === 'contains' ? 'Text' : 'Pattern' |
84 |
| - } |
| 87 | +export default defineComponent({ |
| 88 | + props: { |
| 89 | + value: { |
| 90 | + type: Boolean, |
| 91 | + required: true, |
| 92 | + }, |
| 93 | + title: { |
| 94 | + type: String, |
| 95 | + default: 'New Rule', |
| 96 | + }, |
| 97 | + inputs: { |
| 98 | + type: Object, |
| 99 | + default: () => ({}), |
| 100 | + }, |
| 101 | + }, |
| 102 | + setup(props: Props, context: SetupContext) { |
| 103 | + const state = reactive<{ |
| 104 | + valid: boolean |
| 105 | + dialog: boolean |
| 106 | + formInputs: Partial<Rule> |
| 107 | + }>({ |
| 108 | + valid: false, |
| 109 | + dialog: false, |
| 110 | + formInputs: {}, |
| 111 | + }) |
85 | 112 |
|
86 |
| - @Watch('value') |
87 |
| - onValueChanged(value: boolean) { |
88 |
| - this.dialog = value |
89 |
| - if (value) { |
90 |
| - this.formInputs = { |
91 |
| - active: true, |
92 |
| - field: 'message', |
93 |
| - condition: 'contains', |
94 |
| - action: 'mask_message', |
95 |
| - ...this.inputs, |
96 |
| - } |
| 113 | + const placeholder = computed(() => |
| 114 | + state.formInputs.condition === 'contains' ? 'Text' : 'Pattern' |
| 115 | + ) |
| 116 | +
|
| 117 | + const form = ref<typeof VForm>() |
| 118 | +
|
| 119 | + const handleClickClose = () => { |
| 120 | + context.emit('update:inputs', undefined) |
| 121 | + context.emit('input', false) |
97 | 122 | }
|
98 |
| - } |
99 |
| - @Watch('dialog') |
100 |
| - onDialogChanged(value: boolean) { |
101 |
| - if (!value) { |
102 |
| - this.$emit('update:inputs', null) |
| 123 | + const handleClickSave = () => { |
| 124 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 125 | + if (!(form.value as any).validate()) { |
| 126 | + return |
| 127 | + } |
| 128 | + context.emit('update:inputs', { ...state.formInputs }) |
| 129 | + context.emit('input', false) |
103 | 130 | }
|
104 |
| - this.$emit('input', value) |
105 |
| - } |
106 | 131 |
|
107 |
| - onClickClose() { |
108 |
| - this.$emit('update:inputs', null) |
109 |
| - this.$emit('input', false) |
110 |
| - } |
111 |
| - onClickSave() { |
112 |
| - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
113 |
| - if (!(this.form as any).validate()) { |
114 |
| - return |
| 132 | + watch( |
| 133 | + () => props.value, |
| 134 | + (value) => { |
| 135 | + state.dialog = value |
| 136 | + if (value) { |
| 137 | + state.formInputs = { |
| 138 | + active: true, |
| 139 | + field: 'message', |
| 140 | + condition: 'contains', |
| 141 | + action: 'mask_message', |
| 142 | + ...props.inputs, |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + ) |
| 147 | +
|
| 148 | + return { |
| 149 | + fields, |
| 150 | + conditions, |
| 151 | + actions, |
| 152 | + valueRules, |
| 153 | + state, |
| 154 | + placeholder, |
| 155 | + form, |
| 156 | + handleClickClose, |
| 157 | + handleClickSave, |
115 | 158 | }
|
116 |
| - this.$emit('update:inputs', { ...this.formInputs }) |
117 |
| - this.$emit('input', false) |
118 |
| - } |
119 |
| -} |
| 159 | + }, |
| 160 | +}) |
120 | 161 | </script>
|
0 commit comments