Skip to content

Commit 6220e0e

Browse files
committed
feat: Auto adjust input width by format
1 parent 55cfcb2 commit 6220e0e

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

assets/index.less

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@prefix-cls: rc-picker;
22

33
.@{prefix-cls} {
4-
display: inline;
4+
display: inline-flex;
55

66
&-panel {
77
border: 1px solid #666;
@@ -221,7 +221,12 @@
221221
// ======================== Input =========================
222222
&-input {
223223
position: relative;
224-
display: inline;
224+
display: inline-flex;
225+
width: 100%;
226+
227+
> input {
228+
width: 100%;
229+
}
225230
}
226231

227232
&-clear {

examples/uncontrolled.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ export default () => (
1414
locale={zhCN}
1515
picker="week"
1616
allowClear
17+
style={{ width: 300 }}
1718
/>
19+
<Picker<Moment>
20+
generateConfig={momentGenerateConfig}
21+
locale={zhCN}
22+
picker="week"
23+
allowClear
24+
/>
25+
<button type="button">233</button>
1826
</div>
1927
</div>
2028
);

src/Picker.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { isEqual } from './utils/dateUtil';
2525
import { toArray } from './utils/miscUtil';
2626
import PanelContext, { ContextOperationRefProps } from './PanelContext';
2727
import { PickerMode } from './interface';
28-
import { getDefaultFormat } from './utils/uiUtil';
28+
import { getDefaultFormat, getInputSize } from './utils/uiUtil';
2929

3030
export interface PickerSharedProps<DateType> {
3131
dropdownClassName?: string;
@@ -433,6 +433,7 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
433433
autoFocus={autoFocus}
434434
placeholder={placeholder}
435435
ref={inputRef}
436+
size={getInputSize(picker, formatList[0])}
436437
/>
437438
{suffixNode}
438439
{clearNode}

src/utils/uiUtil.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ export function getDefaultFormat(
147147
return mergedFormat;
148148
}
149149

150+
export function getInputSize(picker: PickerMode | undefined, format: string) {
151+
const defaultSize = picker === 'time' ? 9 : 11;
152+
return Math.max(defaultSize, format.length + 2);
153+
}
154+
150155
// ====================== Mode ======================
151156
const getYearNextMode = (next: PanelMode): PanelMode => {
152157
if (next === 'month' || next === 'date') {

0 commit comments

Comments
 (0)