From 82d9a9c84e6b31f2b3d379f4a7da291f9f388018 Mon Sep 17 00:00:00 2001 From: redhoodsu Date: Mon, 29 Jan 2024 13:33:23 +0800 Subject: [PATCH] feat(toolbar): button --- src/setting/story.js | 2 +- src/share/mixin.scss | 14 +++++++------- src/toolbar/index.ts | 19 +++++++++++++++++-- src/toolbar/react.tsx | 38 +++++++++++++++++++++++++++++++++++++- src/toolbar/story.js | 17 ++++++++++++++++- src/toolbar/style.scss | 16 ++++++++++++++++ 6 files changed, 94 insertions(+), 12 deletions(-) diff --git a/src/setting/story.js b/src/setting/story.js index aed007e..da28184 100644 --- a/src/setting/story.js +++ b/src/setting/story.js @@ -79,7 +79,7 @@ const def = story( setting.appendTitle('Element') - setting.appendText( + setting.appendInput( 'searchKeyword', 'div', 'Search Keyword', diff --git a/src/share/mixin.scss b/src/share/mixin.scss index f2da7da..58efe66 100644 --- a/src/share/mixin.scss +++ b/src/share/mixin.scss @@ -247,21 +247,21 @@ @mixin button($dark: false) { @if $dark { - background: $background-color-dark; - border-color: $border-color-dark; + background: $color-bg-container-dark; + border-color: $color-border-dark; &:hover, &:active { background: $darker-background-color-dark; } &:active { - border: 1px solid $primary-color; + border: 1px solid $color-primary; } } @else { -webkit-tap-highlight-color: rgba(0, 0, 0, 0); - background: $background-color; - border: 1px solid $border-color; + background: $color-bg-container; + border: 1px solid $color-border; padding: 2px 8px; - color: $primary-color; + color: $color-primary; font-size: $font-size; border-radius: 2px; &:hover, @@ -269,7 +269,7 @@ background: $darker-background-color; } &:active { - border: 1px solid $primary-color; + border: 1px solid $color-primary; } } } diff --git a/src/toolbar/index.ts b/src/toolbar/index.ts index 36c4ad9..eb09be0 100644 --- a/src/toolbar/index.ts +++ b/src/toolbar/index.ts @@ -38,6 +38,10 @@ export default class Toolbar extends Component { appendText(text: string) { return this.append(new LunaToolbarText(this, text)) } + /** Append button. */ + appendButton(title: string, handler: types.AnyFn) { + return this.append(new LunaToolbarButton(this, title, handler)) + } /** Append html. */ appendHtml(html: string | HTMLElement) { return this.append(new LunaToolbarHtml(this, html)) @@ -85,7 +89,7 @@ export default class Toolbar extends Component { } /** Append text input. */ appendInput(key: string, value: string, placeholder = '') { - return this.append(new ToolbarInput(this, key, value, placeholder)) + return this.append(new LunaToolbarInput(this, key, value, placeholder)) } private append(item: T): T { this.items.push(item) @@ -159,7 +163,7 @@ export class LunaToolbarSelect extends LunaToolbarItem { } } -class ToolbarInput extends LunaToolbarItem { +export class LunaToolbarInput extends LunaToolbarItem { constructor( toolbar: Toolbar, key: string, @@ -193,6 +197,17 @@ export class LunaToolbarText extends LunaToolbarItem { } } +export class LunaToolbarButton extends LunaToolbarItem { + constructor(toolbar: Toolbar, title: string, handler: types.AnyFn) { + super(toolbar, '', '', 'button') + + this.$container.html(``) + + const $button = this.$container.find('button') + $button.on('click', handler) + } +} + export class LunaToolbarHtml extends LunaToolbarItem { constructor(toolbar: Toolbar, html: string | HTMLElement) { super(toolbar, '', '', 'html') diff --git a/src/toolbar/react.tsx b/src/toolbar/react.tsx index 2c27a97..208a177 100644 --- a/src/toolbar/react.tsx +++ b/src/toolbar/react.tsx @@ -13,6 +13,8 @@ import Toolbar, { LunaToolbarText as ToolbarText, LunaToolbarSelect as ToolbarSelect, LunaToolbarHtml as ToolbarHtml, + LunaToolbarInput as ToolbarInput, + LunaToolbarButton as ToolbarButton, LunaToolbarItem, } from './index' import { useForceUpdate } from '../share/hooks' @@ -74,6 +76,28 @@ export const LunaToolbarText: FC = (props) => { return null } +interface IToolbarButtonProps extends IToolbarItemProps { + title: string + onClick: () => void +} + +export const LunaToolbarButton: FC = (props) => { + const toolbarButton = useRef() + + useEffect(() => { + if (props.toolbar) { + toolbarButton.current = props.toolbar.appendButton( + props.title, + props.onClick + ) + } + + return () => toolbarButton.current?.detach() + }, [props.toolbar]) + + return null +} + interface IToolbarSelectProps extends IToolbarItemProps { keyName: string value: string @@ -130,12 +154,24 @@ interface IToolbarInputProps extends IToolbarItemProps { } export const LunaToolbarInput: FC = (props) => { + const toolbarInput = useRef() + useEffect(() => { if (props.toolbar) { - props.toolbar.appendInput(props.keyName, props.value, props.placeholder) + toolbarInput.current = props.toolbar.appendInput( + props.keyName, + props.value, + props.placeholder + ) + setDisabled(toolbarInput.current, props.disabled) } }, [props.toolbar]) + useEffect( + () => setDisabled(toolbarInput.current, props.disabled), + [props.disabled] + ) + return null } diff --git a/src/toolbar/story.js b/src/toolbar/story.js index fd98616..34a3063 100644 --- a/src/toolbar/story.js +++ b/src/toolbar/story.js @@ -8,6 +8,7 @@ import LunaToolbar, { LunaToolbarSeparator, LunaToolbarSpace, LunaToolbarInput, + LunaToolbarButton, LunaToolbarHtml, } from './react' @@ -37,6 +38,11 @@ const def = story( const filter = toolbar.appendInput('filter', '', 'Filter') filter.disable() + + toolbar.appendButton('Trigger', () => { + console.log('trigger') + }) + toolbar.appendSpace() toolbar.appendHtml( 'Loading' @@ -66,7 +72,16 @@ const def = story( }} /> - + + console.log('trigger')} + />