Skip to content

Commit

Permalink
fix: Add missing size on input (#726)
Browse files Browse the repository at this point in the history
* chore: add missing size

* test: update snapshot

* fix: typescript
  • Loading branch information
zombieJ authored Jan 5, 2024
1 parent 07c3b71 commit 12c0df4
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 15 deletions.
6 changes: 5 additions & 1 deletion docs/examples/debug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ export default () => {
// Shared
{...sharedLocale}
showTime
// format={(val) => val.format('YYYY-MM-DD')}
format={(val) => val.format('YYYY-MM-DD')}
// format={{
// format: 'YYYY-MM-DD',
// align: true,
// }}
presets={[
{
label: 'Good',
Expand Down
15 changes: 15 additions & 0 deletions src/PickerInput/Selector/hooks/useInputProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default function useInputProps<DateType extends object = any>(
| 'name'
| 'autoComplete'
| 'open'
| 'picker'
> & {
id?: string | string[];
value?: DateType[];
Expand Down Expand Up @@ -71,6 +72,8 @@ export default function useInputProps<DateType extends object = any>(
disabled,
activeIndex,
allHelp,

picker,
} = props;

// ======================== Parser ========================
Expand All @@ -94,6 +97,16 @@ export default function useInputProps<DateType extends object = any>(

const valueTexts = React.useMemo(() => value.map(getText), [value, getText]);

// ========================= Size =========================
const size = React.useMemo(() => {
const defaultSize = picker === 'time' ? 8 : 10;
const length =
typeof firstFormat === 'function'
? firstFormat(generateConfig.getNow()).length
: firstFormat.length;
return Math.max(defaultSize, length) + 2;
}, [firstFormat, picker, generateConfig]);

// ======================= Validate =======================
const validateFormat = (text: string) => {
for (let i = 0; i < format.length; i += 1) {
Expand Down Expand Up @@ -140,6 +153,8 @@ export default function useInputProps<DateType extends object = any>(

autoComplete,

size,

// ============= By Index =============
id: getProp(id),

Expand Down
3 changes: 2 additions & 1 deletion src/PickerInput/hooks/useFilledProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { warning } from 'rc-util';
import * as React from 'react';
import { fillLocale } from '../../hooks/useLocale';
import { getTimeConfig } from '../../hooks/useTimeConfig';
import type { FormatType, InternalMode } from '../../interface';
import type { FormatType, InternalMode, PickerMode } from '../../interface';
import { toArray } from '../../utils/miscUtil';
import type { RangePickerProps } from '../RangePicker';
import { fillClearIcon } from '../Selector/hooks/useClearIcon';
Expand Down Expand Up @@ -78,6 +78,7 @@ export default function useFilledProps<
): [
filledProps: Omit<InProps, keyof UpdaterProps | 'showTime' | 'value' | 'defaultValue'> &
UpdaterProps & {
picker: PickerMode;
showTime?: ExcludeBooleanType<InProps['showTime']>;
value?: ToArrayType<InProps['value'], DateType>;
defaultValue?: ToArrayType<InProps['value'], DateType>;
Expand Down
2 changes: 2 additions & 0 deletions src/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ export interface OpenConfig {

export type OnOpenChange = (open: boolean, config?: OpenConfig) => void;
export interface SelectorProps<DateType = any> extends SharedHTMLAttrs {
picker: PickerMode;

clearIcon?: React.ReactNode;
suffixIcon?: React.ReactNode;
className?: string;
Expand Down
14 changes: 1 addition & 13 deletions src/utils/uiUtil.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import isVisible from 'rc-util/lib/Dom/isVisible';
import KeyCode from 'rc-util/lib/KeyCode';
import raf from 'rc-util/lib/raf';
import isVisible from 'rc-util/lib/Dom/isVisible';
import type { GenerateConfig } from '../generate';
import type { CustomFormat, PanelMode, PickerMode } from '../interface';

const scrollIds = new Map<HTMLElement, number>();
Expand Down Expand Up @@ -181,17 +180,6 @@ export function getDefaultFormat<DateType>(
return mergedFormat;
}

export function getInputSize<DateType>(
picker: PickerMode | undefined,
format: string | CustomFormat<DateType>,
generateConfig: GenerateConfig<DateType>,
) {
const defaultSize = picker === 'time' ? 8 : 10;
const length =
typeof format === 'function' ? format(generateConfig.getNow()).length : format.length;
return Math.max(defaultSize, length) + 2;
}

// ===================== Window =====================
type ClickEventHandler = (event: MouseEvent) => void;
let globalClickFunc: ClickEventHandler | null = null;
Expand Down
5 changes: 5 additions & 0 deletions tests/__snapshots__/picker.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ exports[`Picker.Basic icon 1`] = `
<input
aria-invalid="false"
autocomplete="off"
size="12"
value="1990-09-03"
/>
<span
Expand Down Expand Up @@ -35,6 +36,7 @@ exports[`Picker.Basic inputRender 1`] = `
aria-invalid="false"
autocomplete="off"
data-customize="yes"
size="12"
value=""
/>
</div>
Expand All @@ -50,6 +52,7 @@ exports[`Picker.Basic panelRender 1`] = `
<input
aria-invalid="false"
autocomplete="off"
size="12"
value=""
/>
</div>
Expand All @@ -70,6 +73,7 @@ exports[`Picker.Basic pass data- & aria- & role 1`] = `
autocomplete="off"
data-test="233"
role="search"
size="12"
value=""
/>
</div>
Expand All @@ -88,6 +92,7 @@ exports[`Picker.Basic should render correctly in rtl 1`] = `
<input
aria-invalid="false"
autocomplete="off"
size="12"
value=""
/>
</div>
Expand Down
10 changes: 10 additions & 0 deletions tests/__snapshots__/range.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ exports[`Picker.Range icon 1`] = `
aria-invalid="false"
autocomplete="off"
date-range="start"
size="12"
value="1990-09-03"
/>
</div>
Expand All @@ -27,6 +28,7 @@ exports[`Picker.Range icon 1`] = `
aria-invalid="false"
autocomplete="off"
date-range="end"
size="12"
value="1990-09-03"
/>
</div>
Expand Down Expand Up @@ -65,6 +67,7 @@ exports[`Picker.Range onPanelChange is array args should render correctly in rtl
aria-invalid="false"
autocomplete="off"
date-range="start"
size="12"
value=""
/>
</div>
Expand All @@ -80,6 +83,7 @@ exports[`Picker.Range onPanelChange is array args should render correctly in rtl
aria-invalid="false"
autocomplete="off"
date-range="end"
size="12"
value=""
/>
</div>
Expand All @@ -104,6 +108,7 @@ exports[`Picker.Range panelRender 1`] = `
aria-invalid="false"
autocomplete="off"
date-range="start"
size="12"
value=""
/>
</div>
Expand All @@ -119,6 +124,7 @@ exports[`Picker.Range panelRender 1`] = `
aria-invalid="false"
autocomplete="off"
date-range="end"
size="12"
value=""
/>
</div>
Expand Down Expand Up @@ -168,6 +174,7 @@ exports[`Picker.Range use dateRender and monthCellRender in date range picker 1`
aria-invalid="false"
autocomplete="off"
date-range="start"
size="12"
value=""
/>
</div>
Expand All @@ -183,6 +190,7 @@ exports[`Picker.Range use dateRender and monthCellRender in date range picker 1`
aria-invalid="false"
autocomplete="off"
date-range="end"
size="12"
value=""
/>
</div>
Expand Down Expand Up @@ -1211,6 +1219,7 @@ exports[`Picker.Range use dateRender and monthCellRender in month range picker 1
aria-invalid="false"
autocomplete="off"
date-range="start"
size="12"
value=""
/>
</div>
Expand All @@ -1226,6 +1235,7 @@ exports[`Picker.Range use dateRender and monthCellRender in month range picker 1
aria-invalid="false"
autocomplete="off"
date-range="end"
size="12"
value=""
/>
</div>
Expand Down

1 comment on commit 12c0df4

@vercel
Copy link

@vercel vercel bot commented on 12c0df4 Jan 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.