Comments (11)
Will look into it @damooo! Thanks for the issue.
from svelte-forms-lib.
For now a solution would be to initialize the default value in the initialValues
object
const DEFAULT_OPTION = 'Mr.';
const {...handlers, etc} = createForm({
initialValues: {
title: DEFAULT_OPTION // initialise your select component with the default option
},
// other props
});
from svelte-forms-lib.
Does this make sense @damooo?
from svelte-forms-lib.
@tjinauyeung Thanks.
But, when we are initializing the form, there is no data. all options are dynamic, will be known only at run time. So default option is not known.
from svelte-forms-lib.
I see what you mean now.
Currently, the select will only update when the handleChange
is called or if the form state is changed directly. The latter is done as follows:
createForm
returns a form
variable. This is an observable representing the form values. You can manipulate this variable and update the form state when the options are fetched:
const { form } = createForm({
initialValues: {
title: ""
},
onSubmit: (values) => alert(values)
});
getDynamicTitles().then(titles => {
$form.title = titles[0] // set the title
});
Let me know if this works!
from svelte-forms-lib.
@tjinauyeung
That still has a problem that, it triggers svelte reactivity over $form, without user's explicit action. In some cases it is unwanted.
from svelte-forms-lib.
And what about initialising the form áfter the dynamic data has been fetched? Is that an option?
I'm open to other suggestions btw, just need to have a bit of a think on this.
from svelte-forms-lib.
In such cases, form
binding is not a store, until it is initialized, and we cannot refer to it using $form anywhere, even after it is initialized. as svelte checks at compile time and raises error.
One solution i can think of is to make createForm
accept an optional parameter formStore:Writable
. If it is passed, it will be used and returned as form
, otherwise new one. This way, even before initialization, store does exists, with what ever value, and we can refer to it in svelte component with '$form` syntax,
from svelte-forms-lib.
hello @tjinauyeung ,
Are you able to get to a way for it? It is very normal case to feed dynamic options to select.
Or is it possible to work around this without side effects?
thanks again for your work.
from svelte-forms-lib.
@damooo does #24 help with dynamically loaded options?
e.g.
- render form, select's default value is
null
/undefined
- request data
- on response, set select's default value using
updateInitialValues
from svelte-forms-lib.
Yup, that's pretty good api. thanks for that.
from svelte-forms-lib.
Related Issues (20)
- OnChange (handleChange func) HOT 2
- Add support for Form Arrays for Helper Components
- $errors first string, then array HOT 1
- Prototype Pollution
- Document best practices for handling form submission
- View 'global' error on validationSchema's yup object
- Uncaught (in promise) TypeError: Cannot read property '$$' of null HOT 3
- Validation with nested objects behaves inconsistently HOT 1
- Is this project maintained? HOT 1
- Export FormState type
- Adding useField() functionality HOT 1
- Where can I find the documentation for v2? HOT 1
- $errors is not returning error messages for simple arrays (yup.array(yup.string().require()) HOT 1
- set errors when using helpers HOT 1
- What is the correct way to update a property in touched store that is array of objects?
- Have form.reset() HOT 2
- Manually modify initialValues HOT 1
- Add alternatives to validationSchema HOT 1
- Vite Warning: Svelte field in package.json but no export condition for svelte. HOT 1
- onSubmit Function Behaves Inappropriate
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.