Svelte Tutorial for Beginners #21 - Poll Form Component
Key Takeaways
Builds a poll form component in Svelte for adding new polls
Full Transcript
okay then so now we have our tabs sorted and we can switch between contents what I'd like to do first is a form for adding new polls which is going to go right here so to do that we're going to create a brand new component for that form and I'll place it inside the components folder so let's create a new file and call this create Paul form dos belt okay so the first thing I'm gonna do is actually import this now in Apps felt and place it down here where we have content for adding a new poll so let me now just duplicate one of these things and change this and this to create Paul form like so and now down here we're going to output this instead of this paragraph tag and we'll just say create Paul form like so save it now we're not gonna see anything at the minute because that has no content over here and create Paul form so let's create first of all the script tag and then down here we'll do a style tag for later on and then first of all I'd like to flesh out the HTML of this form so we'll create a form tag it doesn't need an action and then inside the form we want to first of all do a few different fields now we're going to have a field for the poll question then a field for answer a or answer one and then a field for answer B or answer two and then finally a button at the bottom now each field I'm gonna place in its own div so we can style it a bit better later on so I'm gonna say div and then form - field and then inside this first form field I'm going to do a label and this is going to be for question and the label will say Paul question okay so now we need an input field below that so we'll say input it's going to be off type text it's gonna have an ID equal to question and then we'll copy this form field because gonna do two more but this time this is going to be for question a or rather answer a so answer - a and I'll change the label text to answer a and then this down here is going to be answer B so answer - B and we need to change the ID here by the way answer a and answer B and the reason we're adding IDs by the way is to link the label and the input because the four generally equals the ID of the input okay so now we need to change this to answer B like so okay so we have this form and we should be able to see that if we go to add new poll we can do well style this later on to make it look a bit better but first of all I want to do a bit of data binding so that we can track what a user types into each one of these input fields now previously when we've tracked this data we've created a separate variable for each input but now I'm going to show you a different way and that's just to create one variable called fields which is equal to an object and the object will have three different properties a property for the question property for answer a and a property for answer B so that now we're storing all of the fields inside one value because all of those values belong together right so then this is going to have a question property which is going to be an empty string to begin with then it's going to have answer a which is going to be an empty string to begin with and then finally answer B empty string to begin with so we have that fields object now we just need to bind to these three properties down in the inputs so first of all we'll bind the value of this one equal to something and in fact I'm just going to copy and paste that in each input field so we don't have to type things out again because we're super lazy or at least I am and then this is going to be bound to fields dot question because the question property is on the fields object right so this one is going to be fields dots answer a and down here we have fields dots answer be awesome so we're binding now the values that user types into the input to these different properties on the fields object so now we're storing those inside here now we also need a button at the bottom so let's do that button this will say add poll now when a user clicks on this button it's gonna fight a submit event on the form so we can listen to that I'm going to say on submit I'm also going to attach an event modifier which is prevent default we saw that before and that prevents the default action when a submit event occurs the default action is to refresh the page we don't want that to happen which is why we're doing this and now we're going to set this equal to some kind of submit handler which is a function we need to create up here so let's now do that I'm going to say Const submit handler is equal to a function and inside that function all we're gonna do is console dot log the fields object right so when we click on this it's just going to log the fields object and the current values of these three properties to the console so let me try this out first of all functionality-wise add new poll inspect and go to the console pod question do you like Marmite yep and na and add poll and we see this fields object now with this question this answer and this answer so we're now tracking those values and storing them in this object and later we can add this object to a data array which we could cycle through over here and output each object right but for now let's just focus on this form and I think I'd like to style it a little bit better so to begin with I'm gonna target the form tag itself this is going to have a width of 400 pixels a margin of 0 and auto so 0 top and bottom auto left and right that centralizes it and also applies margin to the left and right of it equally now we also want to say text - aligned is in the sensor like so now I'm gonna grab each form - field remember that's these things right here each one of these and inside here I'm going to give a margin of 18 pixels top and bottom just so each farm field is spread out a little bit and then it's going to be auto left and right and then down here we also want the input tags which is going to be 100% now that is 100% of 400 pixels remember not the entire screen so down here I also want to say the border radius is going to be 6 pixels that just softens the corners and then after that will style the labels themselves now we want to say the margin is going to be 10 pixels top and bottom auto left and right and we also want to text the line to the left that's to overwrite this thing right here we text a line to the center so that the poll could be in the center the Tsarina button could be in the center but now we're overriding that for the label so the label sits on the left so if I save this and come over here add new poll now this looks a lot better so let's just test this out again ad Paul and we can see everything it still works awesome so now we've created the poll and later on we're going to take this and add it to some data in the app component so then we can cycle through that data and output a template for each poll but for now what I'd like to do is just create maybe a custom button component that could be reusable so that they look a bit better so we'll do that in the next video
Original Description
🐱👤🐱👤 JOIN THE GANG -
https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join
----------------------------------------
🐱💻 🐱💻 My Udemy Courses:
+ Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript
+ Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase
+ D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase
🐱💻 🐱💻 Course Files:
https://github.com/iamshaunjp/svelte-tutorial/tree/lesson-21
🐱💻 🐱💻 Other Related Free Courses:
+ HTML & CSS Crash Course - https://www.youtube.com/playlist?list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G
+ Modern JavaScript - https://www.youtube.com/playlist?list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc
🐱💻 🐱💻 Svelte Docs
https://svelte.dev/tutorial/basics
🐱💻 🐱💻 The Net Ninja Community Boards:
https://community.thenetninja.co.uk/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
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
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: JavaScript Fundamentals
View skill →Related Reads
📰
📰
📰
📰
I built a landing page with Three.js, vanilla JS, and zero frameworks — here's what I learned
Dev.to · Ayush Shekhar
Part 1: Why I Rarely Use useEffect Anymore (and what I use instead)
Dev.to · Alejandro
HTML Canvas Cheat Sheet for Pixel Art & Image Effects
Dev.to · lemon
Draw Pixel Art with Vanilla JS — A Step-by-Step Guide
Dev.to · lemon
🎓
Tutor Explanation
DeepCamp AI