Skip to content

Commit

Permalink
chore: disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Nov 3, 2023
1 parent 72394ea commit 1d07921
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 19 deletions.
7 changes: 5 additions & 2 deletions docs/examples/debug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export default () => {
<br />
<RangePicker
{...sharedLocale}
value={[moment('2000-01-01'), null]}
disabled={[true, false]}
suffixIcon="🧶"
// onFocus={() => {
// console.log('🍷 Focus!');
Expand All @@ -51,8 +53,9 @@ export default () => {
format: 'YYYY-MM-DD',
// format: 'YYYY-MM-DD HH:mm:ss.SSS',
// // format: 'YYYYMMDD',
// align: true,
align: true,
}}
// preserveInvalidOnBlur
// showTime={{}}
onChange={(val, text) => {
console.log('🔥 Change:', val, text);
Expand All @@ -61,7 +64,7 @@ export default () => {
console.log('🎉 Calendar Change:', val, text, info);
}}
// preserveInvalidOnBlur
allowEmpty={[false, true]}
// allowEmpty={[false, true]}
onOpenChange={(nextOpen) => {
console.log('🏆 Next Open:', nextOpen);
}}
Expand Down
4 changes: 3 additions & 1 deletion src/NewPicker/PickerInput/RangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export default function Picker<DateType = any>(props: RangePickerProps<DateType>

// =================== Disabled & Empty ===================
const mergedDisabled = separateConfig(disabled, false);
const mergedAllowEmpty = separateConfig(allowEmpty, true);
const mergedAllowEmpty = separateConfig(allowEmpty, false);

// ======================== Value =========================
const [mergedValue, triggerCalendarChange, triggerSubmitChange] = useRangeValue({
Expand Down Expand Up @@ -367,6 +367,8 @@ export default function Picker<DateType = any>(props: RangePickerProps<DateType>
format={formatList}
maskFormat={maskFormat}
onChange={onSelectorChange}
// Disabled
disabled={mergedDisabled}
// Open
open={mergedOpen ? focusedIndex : null}
onOpenChange={onSelectorOpenChange}
Expand Down
7 changes: 7 additions & 0 deletions src/NewPicker/PickerInput/Selector/RangeSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface RangeSelectorProps<DateType = any> extends SelectorProps<DateTy
separator?: React.ReactNode;

value?: [DateType?, DateType?];
disabled: [boolean, boolean];
}

const RangeSelector = React.forwardRef<SelectorRef, RangeSelectorProps>((props, ref) => {
Expand All @@ -32,6 +33,9 @@ const RangeSelector = React.forwardRef<SelectorRef, RangeSelectorProps>((props,
onSubmit,
preserveInvalidOnBlur,

// Disabled
disabled,

// Open
open,

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused variable open.
onOpenChange,
Expand Down Expand Up @@ -92,6 +96,9 @@ const RangeSelector = React.forwardRef<SelectorRef, RangeSelectorProps>((props,
value: valueTexts[index],

active: activeIndex === index,

disabled: disabled[index],

onFocus: (event) => {
onFocus(event, index);

Expand Down
46 changes: 30 additions & 16 deletions src/NewPicker/PickerInput/hooks/useRangeValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default function useRangeValue<DateType = any>(
| 'order'
| 'onCalendarChange'
| 'onChange'
| 'preserveInvalidOnBlur'
> & {
formatList: string[];
focused: boolean;
Expand All @@ -40,6 +41,7 @@ export default function useRangeValue<DateType = any>(
onCalendarChange,
onChange,
focused,
preserveInvalidOnBlur,
} = info;

// ============================ Values ============================
Expand Down Expand Up @@ -103,35 +105,47 @@ export default function useRangeValue<DateType = any>(
// Sync `calendarValue`
triggerCalendarChange(clone);

// Sync state
setSubmitValue(clone);
// Validate check
const startEmpty = !clone[0];
const endEmpty = !clone[1];

// Trigger `onChange` if needed
if (onChange) {
const [isSameSubmitDates] = isSameDates(submitValue, clone);
const validateDateRange =
// Validate start
(!startEmpty || allowEmpty[0]) &&
// Validate end
(!endEmpty || allowEmpty[1]);

const startEmpty = !clone[0];
const endEmpty = !clone[1];
if (validateDateRange) {
// Sync state
setSubmitValue(clone);

if (
!isSameSubmitDates &&
// Validate start
(!startEmpty || allowEmpty[0]) &&
// Validate end
(!endEmpty || allowEmpty[1])
) {
onChange(clone, getDateTexts(clone));
// Trigger `onChange` if needed
if (onChange) {
const [isSameSubmitDates] = isSameDates(submitValue, clone);

if (!isSameSubmitDates && validateDateRange) {
onChange(clone, getDateTexts(clone));
}
}
}

return validateDateRange;
});

// From the 2 active panel finished
const triggerSubmitChange = (nextValue: RangeValueType<DateType>) => {
triggerSubmit(nextValue);
};

useLockEffect(focused, () => {
if (!focused) {
triggerSubmit();
const validatedSubmitValue = triggerSubmit();

// When blur & invalid, restore to empty one
// This is used for typed only one input
if (!validatedSubmitValue && !preserveInvalidOnBlur) {
setMergedValue([]);
}
}
});

Expand Down

0 comments on commit 1d07921

Please sign in to comment.