Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加应用信息 #1

Merged
merged 3 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
[workspace]
members = ["core", "macro", "."]
resolver = "2"
exclude = ["examples"]

[workspace.package]
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
license-file = "LICENSE"
authors = ["w-sodalite@hotmail.com"]
readme = "README.md"
repository = "https://github.com/w-sodalite/leptos-controls.git"

[workspace]
members = ["core", "macro", "."]
resolver = "2"
[workspace.metadata.release]
registry = "crates-io"
shared-version = true
consolidate-commits = true
pre-release-commit-message = "Release {{version}}"
tag-name = "v{{version}}"
allow-branch = ["master"]

[package]
name = "leptos-controls"
Expand All @@ -19,4 +33,4 @@ leptos-controls-macro = { path = "macro" }
leptos = { version = "0.6" }

[features]
thaw = ["leptos-controls-core/thaw"]
thaw = ["leptos-controls-core/thaw"]
53 changes: 2 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,59 +1,10 @@
# Leptos Controls

在[Leptos](https://github.com/leptos-rs/leptos)中对结构体进行整体控制的工具包。
> 在[Leptos](https://github.com/leptos-rs/leptos)中对结构体进行整体控制的工具包。

## Examples

```rust

use leptos::*;
use leptos_controls::Controls;

#[derive(Default, Controls)]
pub struct LoginArgs {
#[field(label = "账号", validate = "is_not_empty", message = "账号不能为空")]
account: String,

#[field(label = "密码", validate = "is_not_empty", message = "密码不能为空")]
password: String,
}

fn is_not_empty(text: &str) -> bool {
!text.is_empty()
}

fn example() {
let args = LoginArgs {
account: "admin".to_string(),
password: "123456".to_string(),
};
// create controls
let controls = LoginArgsControls::new(args);

// reactive type (RwSignal)
let account = controls.account;
let password =controls.password;

// get field label
let label = controls.account.label();

// get field required
let account_required = controls.account.required();

// get controls validate errors
let errors = controls.validate();

// set signal field default value
controls.account.set_default();

// set all field default value
controls.set_default();

// get snapshot args
let args = controls.snapshot();
}

```
[examples](./examples/login.rs)

## License

Expand Down
54 changes: 54 additions & 0 deletions examples/login.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use leptos::*;
use leptos_controls::Controls;

fn is_not_empty(text: &str) -> bool {
!text.is_empty()
}

#[derive(Debug, Default, Controls)]
pub struct LoginArgs {
#[field(label = "账号", validate = "is_not_empty", message = "账号不能为空")]
account: String,

#[field(label = "密码")]
password: String,
}

fn main() {
let args = LoginArgs::default();
let controls = LoginArgsControls::new(args);
let errors = controls.validate();
println!("{:?}", errors);
// ["账号不能为空"]

controls.account.set("admin".to_string());
controls.account.set("123456".to_string());
let errors = controls.validate();
println!("{:?}", errors);
// []

let account = controls.account.label();
println!("{}", account);
// 账号

let password = controls.password.label();
println!("{}", password);
// 密码

let required = controls.account.required();
println!("{}", required);
// true

let required = controls.password.required();
println!("{}", required);
// false

let args = controls.snapshot();
println!("{:?}", args);
// LoginArgs {account: "admin", password: "123456"}

// set default
controls.account.set_default();
controls.password.set_default();
controls.set_default();
}
45 changes: 0 additions & 45 deletions tests/login.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1 @@
use leptos::*;
use leptos_controls::Controls;

#[derive(Default, Controls)]
pub struct LoginArgs {
#[field(label = "账号", validate = "is_not_empty", message = "账号不能为空")]
account: String,

#[field(label = "密码", validate = "is_not_empty", message = "密码不能为空")]
password: String,
}

fn is_not_empty(text: &str) -> bool {
!text.is_empty()
}

fn example() {
let args = LoginArgs {
account: "admin".to_string(),
password: "123456".to_string(),
};
// create controls
let controls = LoginArgsControls::new(args);

// reactive type (RwSignal)
let account = controls.account;
let password =controls.password;

// get field label
let label = controls.account.label();

// get field required
let account_required = controls.account.required();

// get controls validate errors
let errors = controls.validate();

// set signal field default value
controls.account.set_default();

// set all field default value
controls.set_default();

// get snapshot args
let args = controls.snapshot();
}