-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathslds-publisher.vue
118 lines (90 loc) · 2.88 KB
/
slds-publisher.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<template>
<div :class="publisherClassNames">
<!-- Label -->
<label v-if="label" class="slds-publisher__toggle-visibility slds-m-bottom_small">
{{ label }}
</label>
<!-- Input -->
<textarea
ref="input"
class="slds-publisher__input slds-textarea slds-text-longform"
:value="modelValue"
v-bind="inputAttributes"
@focus="handleFocus"
@input="handleInput"
@keyup="handleKeyUp"
/>
<div class="slds-publisher__actions slds-grid slds-grid_align-spread">
<slds-grid class="slds-publisher__toggle-visibility">
<slot name="actions"/>
</slds-grid>
<!-- Button -->
<slds-button
brand
:label="buttonLabel"
:show-spinner="showSpinner"
@click="handleClickShare"
/>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue"
import SldsButton from "../slds-button/slds-button.vue"
import { EVENTS, KEYS } from "../../constants"
import SldsGrid from "../../components/slds-grid/slds-grid.vue"
export default defineComponent({
name: "SldsPublisher",
components: { SldsGrid, SldsButton },
props: {
buttonLabel: { type: String, default: "Share" },
label: String,
modelValue: String,
showSpinner: Boolean,
},
data() {
return {
isActive: false,
}
},
computed: {
inputAttributes(): Record<string, unknown> {
const attributes: Record<string, unknown> = {}
for (const attribute in this.$attrs) {
if (!attribute.startsWith("data-") && attribute !== "class") {
attributes[attribute] = this.$attrs[attribute]
}
}
return attributes
},
publisherClassNames(): string {
let classNames = "slds-publisher"
if (this.isActive) classNames += " slds-is-active"
return classNames
},
},
methods: {
handleClickShare(): void {
if (this.isActive) {
this.$emit(EVENTS.POST)
}
else {
const input = this.$refs.input as HTMLInputElement
input.focus()
this.isActive = true
}
},
handleFocus(): void {
if (!this.isActive) this.isActive = true
},
handleInput(event: Event): void {
const target = event.target as HTMLInputElement
this.$emit(EVENTS.UPDATE_MODEL_VALUE, target.value)
},
handleKeyUp(event: Event): void {
if (!(event instanceof KeyboardEvent) || event.key !== KEYS.ENTER) return
event.stopPropagation()
},
},
})
</script>