Skip to content

Commit 07e9afc

Browse files
refactor(vue3): migrate FileEntryName to script setup ts
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 1f7c0cb commit 07e9afc

1 file changed

Lines changed: 77 additions & 87 deletions

File tree

src/views/FilesList/FileEntry/FileEntryName.vue

Lines changed: 77 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -39,106 +39,96 @@
3939
</component>
4040
</template>
4141

42-
<script>
42+
<script setup lang="ts">
4343
4444
import { t } from '@nextcloud/l10n'
45+
import { computed, nextTick, ref, watch } from 'vue'
4546
4647
import NcTextField from '@nextcloud/vue/components/NcTextField'
4748
48-
export default {
49+
defineOptions({
4950
name: 'FileEntryName',
51+
})
5052
51-
components: {
52-
NcTextField,
53-
},
54-
props: {
55-
/**
56-
* The filename without extension
57-
*/
58-
basename: {
59-
type: String,
60-
required: true,
61-
},
62-
/**
63-
* The extension of the filename
64-
*/
65-
extension: {
66-
type: String,
67-
required: true,
68-
},
53+
type RenameInputRef = {
54+
$el?: {
55+
querySelector: (selector: string) => HTMLInputElement | null
56+
}
57+
}
58+
59+
const props = defineProps<{
60+
basename: string
61+
extension: string
62+
}>()
63+
64+
const emit = defineEmits<{
65+
rename: [name: string]
66+
'update:basename': [name: string]
67+
renaming: [value: boolean]
68+
}>()
69+
70+
const renameInput = ref<RenameInputRef | null>(null)
71+
const isRenaming = ref(false)
72+
const newName = ref('')
73+
74+
const linkTo = computed(() => ({
75+
is: 'button',
76+
params: {
77+
'aria-label': props.basename,
78+
title: props.basename,
79+
tabindex: '0',
6980
},
81+
}))
7082
71-
emits: ['rename', 'update:basename', 'renaming'],
83+
watch(() => props.basename, (newValue) => {
84+
if (!isRenaming.value) {
85+
newName.value = newValue
86+
}
87+
})
88+
89+
async function startRenaming() {
90+
isRenaming.value = true
91+
emit('renaming', true)
92+
newName.value = props.basename
93+
await nextTick()
94+
const input = renameInput.value?.$el?.querySelector('input')
95+
if (input) {
96+
input.focus()
97+
input.setSelectionRange(0, props.basename.length)
98+
}
99+
}
72100
73-
setup() {
74-
return {
75-
t,
76-
}
77-
},
101+
function stopRenaming() {
102+
isRenaming.value = false
103+
emit('renaming', false)
104+
newName.value = ''
105+
}
78106
79-
data() {
80-
return {
81-
isRenaming: false,
82-
newName: '',
83-
}
84-
},
107+
async function onRename() {
108+
const trimmedName = newName.value.trim()
85109
86-
computed: {
87-
linkTo() {
88-
return {
89-
is: 'button',
90-
params: {
91-
'aria-label': this.basename,
92-
title: this.basename,
93-
tabindex: '0',
94-
},
95-
}
96-
},
97-
},
98-
watch: {
99-
basename(newVal) {
100-
if (!this.isRenaming) {
101-
this.newName = newVal
102-
}
103-
},
104-
},
105-
methods: {
106-
startRenaming() {
107-
this.isRenaming = true
108-
this.$emit('renaming', true)
109-
this.newName = this.basename
110-
this.$nextTick(() => {
111-
const input = this.$refs.renameInput?.$el?.querySelector('input')
112-
if (input) {
113-
input.focus()
114-
input.setSelectionRange(0, this.basename.length)
115-
}
116-
})
117-
},
118-
119-
stopRenaming() {
120-
this.isRenaming = false
121-
this.$emit('renaming', false)
122-
this.newName = ''
123-
},
124-
125-
async onRename() {
126-
const trimmedName = this.newName.trim()
127-
128-
if (!trimmedName || trimmedName.length < 1) {
129-
this.stopRenaming()
130-
return
131-
}
132-
133-
if (trimmedName === this.basename) {
134-
this.stopRenaming()
135-
return
136-
}
137-
138-
this.$emit('rename', trimmedName)
139-
},
140-
},
110+
if (!trimmedName || trimmedName.length < 1) {
111+
stopRenaming()
112+
return
113+
}
114+
115+
if (trimmedName === props.basename) {
116+
stopRenaming()
117+
return
118+
}
119+
120+
emit('rename', trimmedName)
141121
}
122+
123+
defineExpose({
124+
isRenaming,
125+
newName,
126+
linkTo,
127+
startRenaming,
128+
stopRenaming,
129+
onRename,
130+
renameInput,
131+
})
142132
</script>
143133

144134
<style scoped lang="scss">

0 commit comments

Comments
 (0)