Skip to content

Commit 12c0df4

Browse files
authored
fix: Add missing size on input (#726)
* chore: add missing size * test: update snapshot * fix: typescript
1 parent 07c3b71 commit 12c0df4

File tree

7 files changed

+40
-15
lines changed

7 files changed

+40
-15
lines changed

docs/examples/debug.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ export default () => {
150150
// Shared
151151
{...sharedLocale}
152152
showTime
153-
// format={(val) => val.format('YYYY-MM-DD')}
153+
format={(val) => val.format('YYYY-MM-DD')}
154+
// format={{
155+
// format: 'YYYY-MM-DD',
156+
// align: true,
157+
// }}
154158
presets={[
155159
{
156160
label: 'Good',

src/PickerInput/Selector/hooks/useInputProps.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default function useInputProps<DateType extends object = any>(
2727
| 'name'
2828
| 'autoComplete'
2929
| 'open'
30+
| 'picker'
3031
> & {
3132
id?: string | string[];
3233
value?: DateType[];
@@ -71,6 +72,8 @@ export default function useInputProps<DateType extends object = any>(
7172
disabled,
7273
activeIndex,
7374
allHelp,
75+
76+
picker,
7477
} = props;
7578

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

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

100+
// ========================= Size =========================
101+
const size = React.useMemo(() => {
102+
const defaultSize = picker === 'time' ? 8 : 10;
103+
const length =
104+
typeof firstFormat === 'function'
105+
? firstFormat(generateConfig.getNow()).length
106+
: firstFormat.length;
107+
return Math.max(defaultSize, length) + 2;
108+
}, [firstFormat, picker, generateConfig]);
109+
97110
// ======================= Validate =======================
98111
const validateFormat = (text: string) => {
99112
for (let i = 0; i < format.length; i += 1) {
@@ -140,6 +153,8 @@ export default function useInputProps<DateType extends object = any>(
140153

141154
autoComplete,
142155

156+
size,
157+
143158
// ============= By Index =============
144159
id: getProp(id),
145160

src/PickerInput/hooks/useFilledProps.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { warning } from 'rc-util';
22
import * as React from 'react';
33
import { fillLocale } from '../../hooks/useLocale';
44
import { getTimeConfig } from '../../hooks/useTimeConfig';
5-
import type { FormatType, InternalMode } from '../../interface';
5+
import type { FormatType, InternalMode, PickerMode } from '../../interface';
66
import { toArray } from '../../utils/miscUtil';
77
import type { RangePickerProps } from '../RangePicker';
88
import { fillClearIcon } from '../Selector/hooks/useClearIcon';
@@ -78,6 +78,7 @@ export default function useFilledProps<
7878
): [
7979
filledProps: Omit<InProps, keyof UpdaterProps | 'showTime' | 'value' | 'defaultValue'> &
8080
UpdaterProps & {
81+
picker: PickerMode;
8182
showTime?: ExcludeBooleanType<InProps['showTime']>;
8283
value?: ToArrayType<InProps['value'], DateType>;
8384
defaultValue?: ToArrayType<InProps['value'], DateType>;

src/interface.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ export interface OpenConfig {
446446

447447
export type OnOpenChange = (open: boolean, config?: OpenConfig) => void;
448448
export interface SelectorProps<DateType = any> extends SharedHTMLAttrs {
449+
picker: PickerMode;
450+
449451
clearIcon?: React.ReactNode;
450452
suffixIcon?: React.ReactNode;
451453
className?: string;

src/utils/uiUtil.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import isVisible from 'rc-util/lib/Dom/isVisible';
12
import KeyCode from 'rc-util/lib/KeyCode';
23
import raf from 'rc-util/lib/raf';
3-
import isVisible from 'rc-util/lib/Dom/isVisible';
4-
import type { GenerateConfig } from '../generate';
54
import type { CustomFormat, PanelMode, PickerMode } from '../interface';
65

76
const scrollIds = new Map<HTMLElement, number>();
@@ -181,17 +180,6 @@ export function getDefaultFormat<DateType>(
181180
return mergedFormat;
182181
}
183182

184-
export function getInputSize<DateType>(
185-
picker: PickerMode | undefined,
186-
format: string | CustomFormat<DateType>,
187-
generateConfig: GenerateConfig<DateType>,
188-
) {
189-
const defaultSize = picker === 'time' ? 8 : 10;
190-
const length =
191-
typeof format === 'function' ? format(generateConfig.getNow()).length : format.length;
192-
return Math.max(defaultSize, length) + 2;
193-
}
194-
195183
// ===================== Window =====================
196184
type ClickEventHandler = (event: MouseEvent) => void;
197185
let globalClickFunc: ClickEventHandler | null = null;

tests/__snapshots__/picker.spec.tsx.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ exports[`Picker.Basic icon 1`] = `
77
<input
88
aria-invalid="false"
99
autocomplete="off"
10+
size="12"
1011
value="1990-09-03"
1112
/>
1213
<span
@@ -35,6 +36,7 @@ exports[`Picker.Basic inputRender 1`] = `
3536
aria-invalid="false"
3637
autocomplete="off"
3738
data-customize="yes"
39+
size="12"
3840
value=""
3941
/>
4042
</div>
@@ -50,6 +52,7 @@ exports[`Picker.Basic panelRender 1`] = `
5052
<input
5153
aria-invalid="false"
5254
autocomplete="off"
55+
size="12"
5356
value=""
5457
/>
5558
</div>
@@ -70,6 +73,7 @@ exports[`Picker.Basic pass data- & aria- & role 1`] = `
7073
autocomplete="off"
7174
data-test="233"
7275
role="search"
76+
size="12"
7377
value=""
7478
/>
7579
</div>
@@ -88,6 +92,7 @@ exports[`Picker.Basic should render correctly in rtl 1`] = `
8892
<input
8993
aria-invalid="false"
9094
autocomplete="off"
95+
size="12"
9196
value=""
9297
/>
9398
</div>

tests/__snapshots__/range.spec.tsx.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ exports[`Picker.Range icon 1`] = `
1212
aria-invalid="false"
1313
autocomplete="off"
1414
date-range="start"
15+
size="12"
1516
value="1990-09-03"
1617
/>
1718
</div>
@@ -27,6 +28,7 @@ exports[`Picker.Range icon 1`] = `
2728
aria-invalid="false"
2829
autocomplete="off"
2930
date-range="end"
31+
size="12"
3032
value="1990-09-03"
3133
/>
3234
</div>
@@ -65,6 +67,7 @@ exports[`Picker.Range onPanelChange is array args should render correctly in rtl
6567
aria-invalid="false"
6668
autocomplete="off"
6769
date-range="start"
70+
size="12"
6871
value=""
6972
/>
7073
</div>
@@ -80,6 +83,7 @@ exports[`Picker.Range onPanelChange is array args should render correctly in rtl
8083
aria-invalid="false"
8184
autocomplete="off"
8285
date-range="end"
86+
size="12"
8387
value=""
8488
/>
8589
</div>
@@ -104,6 +108,7 @@ exports[`Picker.Range panelRender 1`] = `
104108
aria-invalid="false"
105109
autocomplete="off"
106110
date-range="start"
111+
size="12"
107112
value=""
108113
/>
109114
</div>
@@ -119,6 +124,7 @@ exports[`Picker.Range panelRender 1`] = `
119124
aria-invalid="false"
120125
autocomplete="off"
121126
date-range="end"
127+
size="12"
122128
value=""
123129
/>
124130
</div>
@@ -168,6 +174,7 @@ exports[`Picker.Range use dateRender and monthCellRender in date range picker 1`
168174
aria-invalid="false"
169175
autocomplete="off"
170176
date-range="start"
177+
size="12"
171178
value=""
172179
/>
173180
</div>
@@ -183,6 +190,7 @@ exports[`Picker.Range use dateRender and monthCellRender in date range picker 1`
183190
aria-invalid="false"
184191
autocomplete="off"
185192
date-range="end"
193+
size="12"
186194
value=""
187195
/>
188196
</div>
@@ -1211,6 +1219,7 @@ exports[`Picker.Range use dateRender and monthCellRender in month range picker 1
12111219
aria-invalid="false"
12121220
autocomplete="off"
12131221
date-range="start"
1222+
size="12"
12141223
value=""
12151224
/>
12161225
</div>
@@ -1226,6 +1235,7 @@ exports[`Picker.Range use dateRender and monthCellRender in month range picker 1
12261235
aria-invalid="false"
12271236
autocomplete="off"
12281237
date-range="end"
1238+
size="12"
12291239
value=""
12301240
/>
12311241
</div>

0 commit comments

Comments
 (0)