feat: 配置页面组件
This commit is contained in:
13
client/ui/preference/Preference.tsx
Normal file
13
client/ui/preference/Preference.tsx
Normal file
@@ -0,0 +1,13 @@
|
||||
import { $ } from 'mdui/jq'
|
||||
import { Switch } from 'mdui'
|
||||
import React from 'react'
|
||||
import useEventListener from '../useEventListener.ts'
|
||||
|
||||
export default function Preference({ title, icon, disabled, description, ...props } = {
|
||||
disabled: false,
|
||||
}) {
|
||||
return <mdui-list-item disabled={disabled ? true : undefined} rounded icon={icon} {...props}>
|
||||
{title}
|
||||
{description && <span slot="description">{description}</span>}
|
||||
</mdui-list-item>
|
||||
}
|
||||
3
client/ui/preference/PreferenceHeader.tsx
Normal file
3
client/ui/preference/PreferenceHeader.tsx
Normal file
@@ -0,0 +1,3 @@
|
||||
export default function PreferenceHeader({ title }) {
|
||||
return <mdui-list-subheader>{title}</mdui-list-subheader>
|
||||
}
|
||||
8
client/ui/preference/PreferenceLayout.tsx
Normal file
8
client/ui/preference/PreferenceLayout.tsx
Normal file
@@ -0,0 +1,8 @@
|
||||
export default function PreferenceLayout({ children, ...props }) {
|
||||
return <mdui-list style={{
|
||||
marginLeft: '15px',
|
||||
marginRight: '15px',
|
||||
}} {...props}>
|
||||
{children}
|
||||
</mdui-list>
|
||||
}
|
||||
23
client/ui/preference/PreferenceStore.ts
Normal file
23
client/ui/preference/PreferenceStore.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import React from 'react'
|
||||
|
||||
export default class PreferenceStore {
|
||||
constructor() {
|
||||
const _ = React.useState<{ [key: string]: unknown }>({})
|
||||
this.value = _[0]
|
||||
this.setter = _[1]
|
||||
}
|
||||
// 创建一个用于子选项的更新函数
|
||||
updater(key: string) {
|
||||
return (value: unknown) => {
|
||||
const newValue = JSON.parse(JSON.stringify({
|
||||
...this.value,
|
||||
[key]: value,
|
||||
}))
|
||||
this.setter(newValue)
|
||||
this.onUpdate?.(newValue)
|
||||
}
|
||||
}
|
||||
setOnUpdate(onUpdate) {
|
||||
this.onUpdate = onUpdate
|
||||
}
|
||||
}
|
||||
38
client/ui/preference/SelectPreference.tsx
Normal file
38
client/ui/preference/SelectPreference.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
import { $ } from 'mdui/jq'
|
||||
import React from 'react'
|
||||
import { Dropdown } from 'mdui'
|
||||
import useEventListener from '../useEventListener.ts'
|
||||
|
||||
// value as { [id: string]: string }
|
||||
export default function SelectPreference({ title, icon, updater, selections, defaultCheckedId, disabled } = {
|
||||
disabled: false,
|
||||
}) {
|
||||
const [ checkedId, setCheckedId ] = React.useState(defaultCheckedId)
|
||||
|
||||
const dropDownRef = React.useRef<Dropdown>(null)
|
||||
const [isDropDownOpen, setDropDownOpen] = React.useState(false)
|
||||
|
||||
useEventListener(dropDownRef, 'closed', (e) => {
|
||||
setDropDownOpen(false)
|
||||
})
|
||||
|
||||
return <mdui-list-item icon={icon} rounded disabled={disabled ? true : undefined} onClick={() => setDropDownOpen(!isDropDownOpen)}>
|
||||
<mdui-dropdown ref={dropDownRef} trigger="manual" open={isDropDownOpen}>
|
||||
<span slot="trigger">{ title }</span>
|
||||
<mdui-menu onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
setDropDownOpen(false)
|
||||
}}>
|
||||
{
|
||||
Object.keys(selections).map((id) =>
|
||||
<mdui-menu-item selected={checkedId == id ? true : undefined} onClick={() => {
|
||||
setCheckedId(id)
|
||||
updater(id)
|
||||
}}>{selections[id]}</mdui-menu-item>
|
||||
)
|
||||
}
|
||||
</mdui-menu>
|
||||
</mdui-dropdown>
|
||||
<span slot="description">{ selections[checkedId] }</span>
|
||||
</mdui-list-item>
|
||||
}
|
||||
19
client/ui/preference/SwitchPreference.tsx
Normal file
19
client/ui/preference/SwitchPreference.tsx
Normal file
@@ -0,0 +1,19 @@
|
||||
import { $ } from 'mdui/jq'
|
||||
import { Switch } from 'mdui'
|
||||
import React from 'react'
|
||||
import useEventListener from '../useEventListener.ts'
|
||||
|
||||
export default function SwitchPreference({ title, icon, updater, disabled, description } = {
|
||||
disabled: false,
|
||||
}) {
|
||||
const switchRef = React.useRef<Switch>(null)
|
||||
|
||||
return <mdui-list-item disabled={disabled ? true : undefined} rounded icon={icon} onClick={() => {
|
||||
switchRef.current!.checked = !switchRef.current!.checked
|
||||
updater(switchRef.current!.checked)
|
||||
}}>
|
||||
{title}
|
||||
{description && <span slot="description">{description}</span>}
|
||||
<mdui-switch slot="end-icon" checked-icon="" ref={switchRef} onClick={(e) => e.preventDefault()}></mdui-switch>
|
||||
</mdui-list-item>
|
||||
}
|
||||
30
client/ui/preference/TextFieldPreference.tsx
Normal file
30
client/ui/preference/TextFieldPreference.tsx
Normal file
@@ -0,0 +1,30 @@
|
||||
import { $ } from 'mdui/jq'
|
||||
import React from 'react'
|
||||
import { TextField, prompt } from 'mdui'
|
||||
import useEventListener from '../useEventListener.ts'
|
||||
|
||||
export default function TextFieldPreference({ title, icon, description, updater, defaultValue, disabled } = {
|
||||
disabled: false,
|
||||
}) {
|
||||
const [ text, setText ] = React.useState(defaultValue)
|
||||
|
||||
return <mdui-list-item icon={icon} rounded disabled={disabled ? true : undefined} onClick={() => {
|
||||
prompt({
|
||||
headline: title,
|
||||
confirmText: "确定",
|
||||
cancelText: "取消",
|
||||
onConfirm: (value) => {
|
||||
setText(value)
|
||||
updater(value)
|
||||
},
|
||||
onCancel: () => {},
|
||||
textFieldOptions: {
|
||||
label: description,
|
||||
value: text,
|
||||
},
|
||||
})
|
||||
}}>
|
||||
{title}
|
||||
{description && <span slot="description">{description}</span>}
|
||||
</mdui-list-item>
|
||||
}
|
||||
Reference in New Issue
Block a user