Skip to content

Commit

Permalink
Set clearable for Form date field when it is not required (#1792)
Browse files Browse the repository at this point in the history
Set `clearable` for Form `date` field when it is not required.

---------

Co-authored-by: Ricky James Smith <jamesricky@me.com>
  • Loading branch information
andrearutrecht and jamesricky authored Apr 16, 2024
1 parent faf36f6 commit f8114cd
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-books-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@comet/admin": minor
---

Pass `required` prop to children of `Field` component
7 changes: 7 additions & 0 deletions .changeset/tidy-impalas-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@comet/admin-date-time": major
---

Remove `clearable` prop from `DatePicker`

The clear button will automatically be shown for all optional fields.
7 changes: 4 additions & 3 deletions packages/admin/admin-date-time/src/datePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface DatePickerProps extends Omit<InputWithPopperProps, "children" |
onChange?: (date?: string) => void;
value?: string;
formatDateOptions?: FormatDateOptions;
clearable?: boolean;
required?: boolean;
monthsToShow?: number;
maxDate?: Date;
minDate?: Date;
Expand All @@ -30,7 +30,7 @@ export const DatePicker = (inProps: DatePickerProps) => {
value,
formatDateOptions,
endAdornment,
clearable,
required,
placeholder,
monthsToShow,
minDate = defaultMinDate,
Expand All @@ -54,8 +54,9 @@ export const DatePicker = (inProps: DatePickerProps) => {
{...slotProps?.root}
{...inputWithPopperProps}
readOnly
required={required}
endAdornment={
clearable ? (
!required ? (
<>
<ClearInputAdornment position="end" hasClearableContent={Boolean(value)} onClick={() => onChange && onChange(undefined)} />
{endAdornment}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ export const DateTimePicker = (inProps: DateTimePickerProps) => {
value={value ? getIsoDateString(value) : undefined}
onChange={onChangeDate}
fullWidth
clearable={clearable}
{...slotProps?.datePicker}
/>
</DateFormControl>
Expand Down
3 changes: 2 additions & 1 deletion packages/admin/admin/src/form/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function Field<FieldValue = any, FieldElement extends HTMLElement = HTMLE
meta,
fieldContainerProps,
...rest
}: FieldRenderProps<FieldValue, FieldElement> & { warning?: string; disabled?: boolean }) {
}: FieldRenderProps<FieldValue, FieldElement> & { warning?: string; disabled?: boolean; required?: boolean }) {
function render() {
if (component) {
return React.createElement(component, { ...rest, input, meta });
Expand Down Expand Up @@ -96,6 +96,7 @@ export function Field<FieldValue = any, FieldElement extends HTMLElement = HTMLE
<FinalFormField<FieldValue, FieldElement, FieldValue, FieldRenderProps<FieldValue, FieldElement>>
name={name}
validate={validateError}
required={required}
{...otherProps}
>
{renderField}
Expand Down
2 changes: 1 addition & 1 deletion storybook/src/admin-date-time/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const Story = () => {
<Card>
<CardContent>
<Field name="dateOne" label="Date" fullWidth component={FinalFormDatePicker} />
<Field name="dateTwo" label="Clearable" fullWidth clearable component={FinalFormDatePicker} />
<Field name="dateTwo" label="Required" fullWidth required component={FinalFormDatePicker} />
</CardContent>
</Card>
<pre>{JSON.stringify(values, null, 4)}</pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ storiesOf("stories/form/components/Date & Time Pickers/Date Picker", module)
</Grid>
<Grid item xs={6} md={3}>
<FieldContainer label="Clearable" fullWidth>
<DatePicker fullWidth value={dateThree} onChange={setDateThree} clearable />
<DatePicker fullWidth value={dateThree} onChange={setDateThree} />
</FieldContainer>
</Grid>
<Grid item xs={6} md={3}>
Expand Down

0 comments on commit f8114cd

Please sign in to comment.