Skip to content

Commit

Permalink
* search-box: add compact style.
Browse files Browse the repository at this point in the history
  • Loading branch information
catouse committed Nov 28, 2023
1 parent 27df706 commit 99457f8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
28 changes: 14 additions & 14 deletions lib/search-box/src/components/search-box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class SearchBox extends Component<SearchBoxOptions, SearchBoxState> {
return this._input.current;
}

#handleClearBtnClick = (event: MouseEvent) => {
_handleClearBtnClick = (event: MouseEvent) => {
const oldValue = this.state.value;
event.stopPropagation();
this.setState({value: ''}, () => {
Expand All @@ -45,31 +45,31 @@ export class SearchBox extends Component<SearchBoxOptions, SearchBoxState> {
});
};

#handleChange = (event: Event) => {
_handleChange = (event: Event) => {
const oldValue = this.state.value;
const value = (event.target as HTMLInputElement).value;
const {onChange} = this.props;
this.setState({value}, () => {
if (!onChange || oldValue === value) {
return;
}
this.#clearTimer();
this._clearTimer();
this._timer = window.setTimeout(() => {
onChange(value, event);
this._timer = 0;
}, this.props.delay || 0);
});
};

#handleFocus = (event: FocusEvent) => {
_handleFocus = (event: FocusEvent) => {
const focus = event.type === 'focus';
this.setState({focus}, () => {
const callback = focus ? this.props.onFocus : this.props.onBlur;
callback?.(event);
});
};

#clearTimer() {
_clearTimer() {
if (this._timer) {
clearTimeout(this._timer);
}
Expand All @@ -81,11 +81,11 @@ export class SearchBox extends Component<SearchBoxOptions, SearchBoxState> {
}

componentWillUnmount(): void {
this.#clearTimer();
this._clearTimer();
}

render(props: RenderableProps<SearchBoxOptions>, state: Readonly<SearchBoxState>) {
const {style, className, rootClass, rootStyle, readonly, disabled, circle, placeholder, mergeIcon, searchIcon, clearIcon, value: controlledValue} = props;
const {style, className, rootClass, rootStyle, readonly, disabled, circle, placeholder, mergeIcon, searchIcon, clearIcon, value: controlledValue, compact} = props;
const {focus, value} = state;
const {id} = this;
const finalValue = controlledValue ?? value;
Expand All @@ -104,7 +104,7 @@ export class SearchBox extends Component<SearchBoxOptions, SearchBoxState> {
<button
type="button"
class="btn ghost size-sm square rounded-full"
onClick={this.#handleClearBtnClick}
onClick={this._handleClearBtnClick}
>
{clearIcon === true ? <span class="close" /> : <Icon icon={clearIcon} />}
</button>
Expand All @@ -121,22 +121,22 @@ export class SearchBox extends Component<SearchBoxOptions, SearchBoxState> {
}

return (
<div class={classes('search-box input-control', rootClass, {focus, empty, 'has-prefix-icon': prefixView, 'has-suffix-icon': suffixView})} style={rootStyle}>
<div class={classes('search-box input-control', rootClass, {focus, empty, compact, 'has-prefix-icon': prefixView, 'has-suffix-icon': suffixView})} style={rootStyle}>
{prefixView}
<input
ref={this._input}
id={id}
type="text"
class={classes('form-control', className, {'rounded-full': circle})}
class={classes('form-control', className, {'rounded-full': circle, 'size-sm': compact})}
style={style}
placeholder={placeholder}
disabled={disabled}
readonly={readonly}
value={finalValue}
onInput={this.#handleChange}
onChange={this.#handleChange}
onFocus={this.#handleFocus}
onBlur={this.#handleFocus}
onInput={this._handleChange}
onChange={this._handleChange}
onFocus={this._handleFocus}
onBlur={this._handleFocus}
/>
{suffixView}
</div>
Expand Down
6 changes: 6 additions & 0 deletions lib/search-box/src/style/search-box.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.search-box.compact.has-prefix-icon {
--input-control-prefix: 24px;
}
.search-box.compact.has-suffix-icon {
--input-control-suffix: 24px;
}
1 change: 1 addition & 0 deletions lib/search-box/src/types/search-box-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {ClassNameLike, IconType, JSX} from '@zui/core';
export type SearchBoxOptions = {
id?: string;
name?: string;
compact?: boolean;
className?: ClassNameLike;
rootClass?: ClassNameLike;
rootStyle?: JSX.CSSProperties;
Expand Down

0 comments on commit 99457f8

Please sign in to comment.