Your Vue.js component looks well-structured and leverages reusable components effectively. Below, I’ll provide a comprehensive review, highlighting potential issues, suggestions for improvements, and best practices to enhance your component.
1. Handle Missing Event Handlers and Form State
Issue:
- In your
<StyledInput> components for "Mobile Number", "From", and "To" dates, you have @input-change="handleChange" but the handleChange method is not defined in your script.
- The radio buttons for "Did you obtain the degree or diploma?" lack
v-model bindings, meaning their state isn't being tracked.
- Form submission handling is absent; the form doesn’t have a method to process or validate the input data.
Solution:
- Define the necessary event handlers and manage the form state using
ref or reactive from Vue.
- Add
v-model bindings to the radio buttons.
- Implement a form submission handler to process the data.
Updated Script:
<script lang="ts" setup>
import { computed, ref } from 'vue'
import { Autocomplete, createListResource } from 'frappe-ui'
import Button from '../../components/button.vue';
import Heading from '../../components/heading.vue';
import StyledIcon from '../../components/styledIcon.vue';
import StyledInput from '../../components/styledInput.vue';
// Reactive form state
const form = ref({
mobile: '',
country: '',
diploma: '',
availableFrom: '',
availableTo: '',
})
// Country Autocomplete
const location = createListResource({
type: 'list',
doctype: 'Location',
fields: ["location_name", "parent_location", "location_type"],
cache: 'location',
auto: true,
})
const optionCountry = computed(() => {
return location.data.filter(loc => loc.location_type === 'Country').map(loc => ({
label: loc.location_name,
value: loc.location_name
}))
})
const handleCountryChange = (value) => {
form.value.country = value.value
}
// Generic input change handler
const handleChange = (event) => {
const { name, value } = event.target
form.value[name] = value
}
// Form submission handler
const handleSubmit = (event) => {
event.preventDefault()
// Add your form validation and submission logic here
console.log('Form Submitted:', form.value)
}
</script>
Updated Template:
<template>
<div class="pt-6">
<div class="flex gap-2 mb-2">
<div class="w-7 lg:w-9 shrink-0">
<StyledIcon />
</div>
<Heading level="secondary" class="text-black" tag="h2">
Here you can follow the progress of your report and communicate with Taakd
</Heading>
</div>
<form @submit="handleSubmit">
<div class="mt-3">
<h3 class="text-lg font-medium mb-3 text-black">Contact details</h3>
<div class="lg:grid lg:grid-cols-2 lg:gap-3 lg:w-1/2">
<div class="py-3">
<StyledInput
id="mobile"
name="mobile"
labelText="Mobile Number"
isMandatory="true"
infoText="mobile number"
inputType="text"
v-model="form.mobile"
/>
<small class="block text-[10px] font-normal">Supporting text</small>
</div>
<div class="py-3">
<Autocomplete
placeholder="Country"
:options="optionCountry"
v-model="form.country"
@change="handleCountryChange"
/>
</div>
</div>
<div class="py-3 text-sm">
<span class="text-sm block mb-3">Did you obtain the degree or diploma? *</span>
<div class="flex flex-col gap-2">
<div>
<input
type="radio"
name="diploma"
id="yes"
value="yes"
v-model="form.diploma"
/>
<label for="yes">
Yes, I obtained
</label>
</div>
<div>
<input
type="radio"
name="diploma"
id="no"
value="no"
v-model="form.diploma"
/>
<label for="no">
No, I did not obtain
</label>
</div>
</div>
</div>
<div class="py-3">
<span class="text-sm font-medium block mb-2">Available Time</span>
<div class="grid grid-cols-2 gap-3 text-sm lg:w-1/2">
<div class="w-full">
<StyledInput
id="from"
name="availableFrom"
labelText="From"
isMandatory="true"
infoText="From"
inputType="date"
v-model="form.availableFrom"
/>
</div>
<div class="w-full">
<StyledInput
id="to"
name="availableTo"
labelText="To"
isMandatory="true"
infoText="To"
inputType="date"
v-model="form.availableTo"
/>
</div>
</div>
</div>
<div class="pt-5 flex w-full justify-center">
<Button type="submit" level="other">
Save
</Button>
</div>
</div>
</form>
</div>
</template>
2. Improve Accessibility
Issue:
- Labels are correctly associated with inputs using the
for and id attributes, which is excellent for accessibility. However, ensuring all interactive elements are accessible via keyboard and screen readers is crucial.
Suggestions:
- Ensure
StyledInput and other custom components properly forward the necessary ARIA attributes.
- For the
Autocomplete component, verify that it’s accessible (e.g., supports keyboard navigation).
3. Validation
Issue:
- There's no form validation to ensure that mandatory fields are filled out correctly.
Solution:
- Implement validation either through Vue's reactive system or by leveraging a validation library like Vuelidate or Yup with VeeValidate.
Basic Validation Example:
<script lang="ts" setup>
// ...previous imports and code
const errors = ref({
mobile: '',
country: '',
diploma: '',
availableFrom: '',
availableTo: '',
})
const validateForm = () => {
let isValid = true
// Reset errors
Object.keys(errors.value).forEach(key => errors.value[key] = '')
if (!form.value.mobile) {
errors.value.mobile = 'Mobile number is required.'
isValid = false
}
if (!form.value.country) {
errors.value.country = 'Country is required.'
isValid = false
}
if (!form.value.diploma) {
errors.value.diploma = 'This field is required.'
isValid = false
}
if (!form.value.availableFrom) {
errors.value.availableFrom = 'Start date is required.'
isValid = false
}
if (!form.value.availableTo) {
errors.value.availableTo = 'End date is required.'
isValid = false
}
return isValid
}
const handleSubmit = (event) => {
event.preventDefault()
if (validateForm()) {
// Submit form
console.log('Form Submitted:', form.value)
} else {
console.log('Form validation failed:', errors.value)
}
}
</script>
Updated Template with Error Messages:
<template>
<!-- ...previous template code -->
<form @submit="handleSubmit">
<!-- ...previous form fields -->
<div class="py-3">
<StyledInput
:errorMessage="errors.mobile"
<!-- other props -->
/>
<!-- Similarly, pass error messages to other inputs -->
</div>
<!-- ...rest of the form -->
</form>
</template>
4. Enhance UX with Disabled Submit Button
Suggestion:
- Disable the submit button until the form is valid to prevent users from submitting incomplete information.
Implementation:
<script lang="ts" setup>
// ...previous imports and code
const isFormValid = computed(() => {
return (
form.value.mobile &&
form.value.country &&
form.value.diploma &&
form.value.availableFrom &&
form.value.availableTo
)
})
</script>
<template>
<!-- ...previous template code -->
<Button type="submit" level="other" :disabled="!isFormValid">
Save
</Button>
<!-- ...rest of the form -->
</template>
5. Optimize Imports
Issue:
- You have separate import statements from
'frappe-ui' which can be consolidated.
Solution:
- Combine imports from the same module into a single statement for cleaner code.
Updated Imports:
import { computed, ref } from 'vue'
import { Autocomplete, createListResource } from 'frappe-ui'
import Button from '../../components/button.vue'
import Heading from '../../components/heading.vue'
import StyledIcon from '../../components/styledIcon.vue'
import StyledInput from '../../components/styledInput.vue'
6. Consistent Naming Conventions
Suggestion:
- Ensure that naming conventions are consistent for better readability and maintenance. For example, use
camelCase for JavaScript variables and kebab-case for component names in templates as per Vue conventions.
Example:
valueCountry and selectedCountry could be renamed to countryValue and selectedCountry respectively for clarity.
7. Handle Asynchronous Data Loading
Issue:
- If the
Location data is fetched asynchronously, there might be a loading state or error handling missing.
Solution:
- Implement loading indicators or error messages to enhance user experience.
Example:
<script lang="ts" setup>
// ...previous imports and code
const isLoading = location.isFetching
const error = location.error
</script>
<template>
<!-- ...previous template code -->
<div class="py-3">
<Autocomplete
placeholder="Country"
:options="optionCountry"
v-model="form.country"
@change="handleCountryChange"
:disabled="isLoading"
/>
<span v-if="isLoading">Loading countries...</span>
<span v-if="error" class="text-red-500">Failed to load countries.</span>
</div>
<!-- ...rest of the form -->
</template>
8. Improve Styling and Responsiveness
Suggestions:
- Ensure that all form elements are responsive across different screen sizes.
- Utilize utility-first CSS effectively to maintain a consistent design.
9. Accessibility Enhancements
Suggestions:
- Add
aria-required="true" to mandatory fields.
- Ensure sufficient color contrast for text and interactive elements.
- Provide clear focus indicators for keyboard navigation.
Example:
<StyledInput
id="mobile"
name="mobile"
labelText="Mobile Number"
isMandatory="true"
infoText="mobile number"
inputType="text"
v-model="form.mobile"
aria-required="true"
/>
10. Security Considerations
Suggestions:
- Sanitize and validate all user inputs on both client and server sides to protect against malicious inputs.
- Implement proper error handling to avoid exposing sensitive information.
Final Revised Component
Here's the consolidated revised component incorporating the above suggestions:
<template>
<div class="pt-6">
<div class="flex gap-2 mb-2">
<div class="w-7 lg:w-9 shrink-0">
<StyledIcon />
</div>
<Heading level="secondary" class="text-black" tag="h2">
Here you can follow the progress of your report and communicate with Taakd
</Heading>
</div>
<form @submit="handleSubmit">
<div class="mt-3">
<h3 class="text-lg font-medium mb-3 text-black">Contact details</h3>
<div class="lg:grid lg:grid-cols-2 lg:gap-3 lg:w-1/2">
<div class="py-3">
<StyledInput
id="mobile"
name="mobile"
labelText="Mobile Number"
isMandatory="true"
infoText="mobile number"
inputType="text"
v-model="form.mobile"
:errorMessage="errors.mobile"
aria-required="true"
/>
<small class="block text-[10px] font-normal">Supporting text</small>
</div>
<div class="py-3">
<Autocomplete
placeholder="Country"
:options="optionCountry"
v-model="form.country"
@change="handleCountryChange"
:disabled="isLoading"
:errorMessage="errors.country"
/>
<span v-if="isLoading" class="text-sm">Loading countries...</span>
<span v-if="error" class="text-red-500 text-sm">Failed to load countries.</span>
</div>
</div>
<div class="py-3 text-sm">
<span class="text-sm block mb-3">Did you obtain the degree or diploma? *</span>
<div class="flex flex-col gap-2">
<div>
<input
type="radio"
name="diploma"
id="yes"
value="yes"
v-model="form.diploma"
aria-required="true"
/>
<label for="yes">Yes, I obtained</label>
</div>
<div>
<input
type="radio"
name="diploma"
id="no"
value="no"
v-model="form.diploma"
aria-required="true"
/>
<label for="no">No, I did not obtain</label>
</div>
</div>
<span v-if="errors.diploma" class="text-red-500 text-xs">{{ errors.diploma }}</span>
</div>
<div class="py-3">
<span class="text-sm font-medium block mb-2">Available Time</span>
<div class="grid grid-cols-2 gap-3 text-sm lg:w-1/2">
<div class="w-full">
<StyledInput
id="from"
name="availableFrom"
labelText="From"
isMandatory="true"
infoText="From"
inputType="date"
v-model="form.availableFrom"
:errorMessage="errors.availableFrom"
aria-required="true"
/>
</div>
<div class="w-full">
<StyledInput
id="to"
name="availableTo"
labelText="To"
isMandatory="true"
infoText="To"
inputType="date"
v-model="form.availableTo"
:errorMessage="errors.availableTo"
aria-required="true"
/>
</div>
</div>
</div>
<div class="pt-5 flex w-full justify-center">
<Button type="submit" level="other" :disabled="!isFormValid">
Save
</Button>
</div>
</div>
</form>
</div>
</template>
<script lang="ts" setup>
import { computed, ref } from 'vue'
import { Autocomplete, createListResource } from 'frappe-ui'
import Button from '../../components/button.vue'
import Heading from '../../components/heading.vue'
import StyledIcon from '../../components/styledIcon.vue'
import StyledInput from '../../components/styledInput.vue'
// Reactive form state
const form = ref({
mobile: '',
country: '',
diploma: '',
availableFrom: '',
availableTo: '',
})
// Error messages
const errors = ref({
mobile: '',
country: '',
diploma: '',
availableFrom: '',
availableTo: '',
})
// Country Autocomplete
const location = createListResource({
type: 'list',
doctype: 'Location',
fields: ['location_name', 'parent_location', 'location_type'],
cache: 'location',
auto: true,
})
const optionCountry = computed(() => {
return location.data
.filter((loc) => loc.location_type === 'Country')
.map((loc) => ({
label: loc.location_name,
value: loc.location_name,
}))
})
const handleCountryChange = (value) => {
form.value.country = value.value
}
// Form submission handler
const handleSubmit = (event) => {
event.preventDefault()
if (validateForm()) {
// Submit form
console.log('Form Submitted:', form.value)
// Reset form if needed
// form.value = { mobile: '', country: '', diploma: '', availableFrom: '', availableTo: '' }
} else {
console.log('Form validation failed:', errors.value)
}
}
// Form validation
const validateForm = () => {
let isValid = true
// Reset errors
Object.keys(errors.value).forEach((key) => (errors.value[key] = ''))
if (!form.value.mobile) {
errors.value.mobile = 'Mobile number is required.'
isValid = false
} else if (!/^\+?\d{10,15}$/.test(form.value.mobile)) {
errors.value.mobile = 'Please enter a valid mobile number.'
isValid = false
}
if (!form.value.country) {
errors.value.country = 'Country is required.'
isValid = false
}
if (!form.value.diploma) {
errors.value.diploma = 'This field is required.'
isValid = false
}
if (!form.value.availableFrom) {
errors.value.availableFrom = 'Start date is required.'
isValid = false
}
if (!form.value.availableTo) {
errors.value.availableTo = 'End date is required.'
isValid = false
} else if (form.value.availableFrom && form.value.availableTo < form.value.availableFrom) {
errors.value.availableTo = 'End date cannot be before start date.'
isValid = false
}
return isValid
}
// Computed property to enable/disable submit button
const isFormValid = computed(() => {
return (
form.value.mobile &&
form.value.country &&
form.value.diploma &&
form.value.availableFrom &&
form.value.availableTo &&
// Additional validation can be added here
!errors.value.mobile &&
!errors.value.country &&
!errors.value.diploma &&
!errors.value.availableFrom &&
!errors.value.availableTo
)
})
// Loading and error states
const isLoading = location.isFetching
const error = location.error
</script>
<style scoped>
/* Add any component-specific styles here */
</style>
Summary of Improvements
- Form State Management: Implemented a reactive form state using
ref and v-model bindings for controlled inputs.
- Event Handlers: Defined
handleChange for generic input handling and handleSubmit for form submission.
- Validation: Added basic validation logic to ensure required fields are filled and follow expected formats.
- UX Enhancements: Disabled the submit button until the form is valid and provided feedback for loading states.
- Accessibility: Included ARIA attributes and ensured labels are properly linked to inputs.
- Code Cleanliness: Consolidated imports and maintained consistent naming conventions.
- Error Handling: Displayed error messages for both form validation and data fetching failures.
Implementing these changes will make your component more robust, user-friendly, and maintainable. Depending on your specific requirements, you might want to further enhance validation, handle more edge cases, or integrate with a backend service for form submission.